SHARE
TWEET

Untitled

a guest Jul 18th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void merge_sort(MOVIE *movie, unsigned int left, unsigned int right)
  2. {
  3.     unsigned int mid;
  4.  
  5.     if (left >= right)
  6.         return;
  7.  
  8.     mid = (left + right) / 2;
  9.     merge_sort(movie, left, mid);
  10.     merge_sort(movie, mid + 1, right);
  11.  
  12.     merge(movie, left, mid, right);
  13. }
  14.  
  15. void merge(MOVIE *movie, unsigned int left, unsigned int mid, unsigned int right)
  16. {
  17.     MOVIE *temp;
  18.     temp = (MOVIE *)malloc(sizeof(MOVIE) * right);
  19.  
  20.     if (!temp)
  21.     {
  22.         printf("ALLOCATION ERROR IN: merge !!");
  23.         exit(MALLOC_ERROR_CODE);
  24.     }
  25.  
  26.     unsigned int l, m, i;
  27.     l = left;
  28.     m = mid + 1;
  29.  
  30.     for (i = left; l <= mid && m <= right; i++)
  31.     {
  32.         if (movie[l].grade < movie[m].grade)
  33.             temp[i] = movie[l++];
  34.         if (movie[l].grade > movie[m].grade)
  35.             temp[i] = movie[m++];
  36.         else
  37.         {
  38.             if (strcmp(movie[l].name, movie[m].name) < 0)
  39.                 temp[i] = movie[l++];
  40.             else
  41.                 temp[i] = movie[m++];
  42.         }
  43.     }
  44.  
  45.     while (l <= mid)
  46.         temp[i++] = movie[l++];
  47.  
  48.     while (m <= right)
  49.         temp[i++] = movie[m++];
  50.  
  51.     for (i = left; i <= right; i++)
  52.         movie[i] = temp[i];
  53. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top