SHARE
TWEET

Untitled

a guest Jul 16th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void Merge(int numbers[], int i, int j, int k) {
  2.    int mergedSize = k - i + 1;
  3.    int mergedNumbers[mergedSize];
  4.    int mergePos;
  5.    int leftPos;
  6.    int rightPos;
  7.    
  8.    mergePos = 0;
  9.    leftPos = i;
  10.    rightPos = j + 1;
  11.  
  12.    while (leftPos <= j && rightPos <= k) {
  13.       if (numbers[leftPos] < numbers[rightPos]) {
  14.          mergedNumbers[mergePos].departures = numbers[leftPos].departures;
  15. strcpy( mergedNumbers
  16.          ++leftPos;
  17.       }
  18.       else {
  19.           mergedNumbers[mergePos].departures = numbers[rightPos].departures;
  20.          ++rightPos;
  21.          
  22.       }
  23.       ++mergePos;
  24.    }
  25.    
  26.  
  27.    while (leftPos <= j) {
  28. mergedNumbers[mergePos].departures = numbers[leftPos].departures;
  29.       ++leftPos;
  30.       ++mergePos;
  31.    }
  32.    
  33.    while (rightPos <= k) {
  34.      mergedNumbers[mergePos].departures = numbers[rightPos].departures;
  35.       ++rightPos;
  36.       ++mergePos;
  37.    }
  38.    
  39.    for (mergePos = 0; mergePos < mergedSize; ++mergePos) {
  40.       numbers[i + mergePos] = mergedNumbers[mergePos];
  41.    }
  42. }
  43.  
  44. void mergeSortRecords(int numbers[], int i, int k) {
  45.    int j;
  46.    
  47.    if (i < k) {
  48.       j = (i + k) / 2;  // Find the midpoint in the partition
  49.      
  50.       // Recursively sort left and right partitions
  51.       mergeSortRecords(numbers, i, j);
  52.       mergeSortRecords(numbers, j + 1, k);
  53.      
  54.       // Merge left and right partition in sorted order
  55.       mergeRecords(numbers, i, j, k);
  56.    }
  57. }
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
 
Top