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 42 43 44 45 46 47 48
| #include <iostream> #include <stdio.h> #include <cstdio> #include <vector> #include <algorithm> #include "dbg.h" using namespace std;
void merge(int A[], int low, int mid, int high) { int i, j, k; int B[high] = {0}; for (k = low; k <= high; k++) B[k] = A[k]; for (i = low, j = mid + 1, k = i; i <= mid && j <= high; k++) { if (B[i] <= B[j]) A[k] = B[i++]; else A[k] = B[j++]; }
while (i <= mid) A[k++] = B[i++]; while (j <= high) A[k++] = B[j++]; }
void mergeSort(int A[], int low, int high) { if (low < high) { int mid = low + (high - low) / 2; mergeSort(A, low, mid); mergeSort(A, mid + 1, high); merge(A, low, mid, high); } }
int main(int argc, char *argv[]) { int R[] = {1, 3, 2, 3, 53, 532, 52, 5, 67, 68}; dbg(R); int len = sizeof(R) / sizeof(R[0]); mergeSort(R, 0, len - 1); dbg(R); return 0; }
|