Merge sort with array of structs

  • Thread starter Firestrider
  • Start date
  • Tags
    Array Sort
In summary: When I look at the code, it looks like the first_name member is actually a pointer to a char. When I try to copy that pointer, I get the error "incompatible types in assignment".
  • #1
Firestrider
104
0

Homework Statement



I need to be able to copy full structs in a temporary array based on a value in that struct (an integer), but I'm not sure exactly how to go about it. I have commented where I need to copy over structs.

Also in the other code block, what is the problem here? It doesn't seem to reconize the string.

Homework Equations



N/A

The Attempt at a Solution



Code:
void Merge(struct student_birthday birthdays[], int start, int middle, int end) 
{
   int* temp, i, length, count1, count2, mc;
  
   length = end - start + 1;
   temp = calloc(length, sizeof(struct student_birthday));

   count1 = start;
   count2 = middle;
  
   mc = 0;

   while ((count1 < middle) && (count2 <= end)) 
   {
      if (birthdays[count1].ymd < birthdays[count2].ymd) 
      {
         temp[mc] = birthdays[count1].ymd; // structs not values!
         count1++;
         mc++;
      }
    
      else 
      {
         temp[mc] = birthdays[count2].ymd; // structs not values!
         count2++;
         mc++;
      }
   }

   if (count1 < middle) 
   {
      for (i = mc; i < length; i++) 
      {
         temp[i] = birthdays[count1].ymd; // structs not values!
         count1++;
      }
   }
  
   else if (count2 <= end) 
   {
      for (i = mc; i < length; i++) 
      {
         temp[i] = birthdays[count2].ymd; // structs not values!
         count2++;
      }
   }

   for (i = start; i <= end; i++)
      birthdays[i].ymd = temp[i - start]; // structs not values!

   free(temp);
}

Code:
int MonthToInt(char month[9]);

struct student_birthday
{
   char month[9];
};

int main(void)
{
   m_int = MonthToInt(birthdays[j].month);
}

int MonthToInt(char month[9])
{
    int m_int = 0;
    
    if (month == "JANUARY") m_int = 1; 
    else if (month == "FEBRUARY") m_int = 2;
    else if (month == "MARCH") m_int = 3;
    else if (month == "APRIL") m_int = 4;
    else if (month == "MAY") m_int = 5;
    else if (month == "JUNE") m_int = 6;
    else if (month == "JULY") m_int = 7;
    else if (month == "AUGUST") m_int = 8;
    else if (month == "SEPTEMBER") m_int = 9;
    else if (month == "OCTOBER") m_int = 10;
    else if (month == "NOVEMBER") m_int = 11;
    else if (month == "DECEMBER") m_int = 12;
       
    return m_int;
}
 
Physics news on Phys.org
  • #2
It seems I have to use the strcmp function for the second code block.

Does anyone know why this statement will not work:

Code:
struct student_birthday* temp;
int i, length, count1, count2, mc;

length = end - start + 1;
temp = calloc(length, sizeof(struct student_birthday));

temp[mc]->first_name = birthdays[count1].first_name;

It says "invalid type argument of `->' ". I just want to copy each individual piece of the struct one by one, or if possible the whole struct...

Any help is appreciated!
 
  • #3
Firestrider said:
It says "invalid type argument of `->' ".
That's because temp[mc] isn't a pointer. You wanted `.'.
 
  • #4
Firestrider: For your first code block, would this work? temp[mc] = birthdays[count1];
 
  • #5
Hi, and thanks for your help.

When I do this:

Code:
temp[mc].first_name = birthdays[count1].first_name;

I get the error "incompatible types in assignment"

This is the function:

Code:
void Merge(struct student_birthday birthdays[], int start, int middle, int end)
 
  • #6
nvn said:
Firestrider: For your first code block, would this work? temp[mc] = birthdays[count1];

That works when I change temp to a pointer to a struct, but I get weird values when accessing pieces of the struct. It probably has something do with how memory is allocated.

Does anyone know why I get that error from my previous post? I'm running into this a lot and I'm not sure why I get it.
 
  • #7
Firestrider said:
Code:
temp[mc].first_name = birthdays[count1].first_name;

If first_name is supposed to be an array of char, then remember that you can't copy C-style arrays by assignment.

But... I don't see any member called "first_name" in struct student_birthday.
 

Related to Merge sort with array of structs

1. What is merge sort?

Merge sort is a sorting algorithm that uses the divide and conquer approach to sort an array of elements. It works by breaking down the array into smaller subarrays, sorting them, and then merging them back together in the correct order.

2. How does merge sort work with an array of structs?

In merge sort, the array of structs is divided into smaller subarrays based on a chosen pivot element. These subarrays are then sorted recursively using the same pivot element until they are sorted. Finally, the sorted subarrays are merged back together to create a sorted array of structs.

3. What is the time complexity of merge sort with an array of structs?

The time complexity of merge sort with an array of structs is O(nlogn), where n is the number of elements in the array. This is because the algorithm divides the array into smaller subarrays, which reduces the number of comparisons needed to sort the array.

4. Is merge sort stable when working with an array of structs?

Yes, merge sort is a stable sorting algorithm even when working with an array of structs. This means that elements with equal values will maintain their relative positions in the sorted array.

5. What are the advantages of using merge sort with an array of structs?

One advantage is that merge sort has a relatively fast worst-case time complexity compared to other sorting algorithms. It is also a stable sorting algorithm, making it a good choice for sorting structured data such as an array of structs.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
Back
Top