1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| #ifndef ALGORITHM_MERGE_SORT_H #define ALGORITHM_MERGE_SORT_H
void merge_sort (int array[], int left, int right) { int i, j, k, mid,l; int work[10]; if (left < right) { mid = (left + right)/2; merge_sort(array, left, mid); merge_sort(array, mid+1, right);
for (i = mid; i >= left; i--) { work[i] = array[i]; } for (j = mid+1; j <= right; j++) { work[right-(j-(mid+1))] = array[j]; }
i = left; j = right; for (k = left; k <= right; k++) { if (work[i] < work[j]) { array[k] = work[i++]; }else{ array[k] = work[j--]; } }
for (l = 0; l <= right; l++) { printf("%d ", work[l]); if(l==mid){ printf("| "); } } printf("\n"); }
} #endif
|