voidmerge(int *a, int *tmp, int left, int leftEnd, int rightEnd) { int right = leftEnd; int tmpPos = left; int count = rightEnd - left;
while (left < leftEnd && right < rightEnd) { tmp[tmpPos++] = a[right] < a[left] ? a[right++] : a[left++]; } while (left < leftEnd) { tmp[tmpPos++] = a[left++]; } while (right < rightEnd) { tmp[tmpPos++] = a[right++]; }
while (count--) { --rightEnd; a[rightEnd] = tmp[rightEnd]; } }
voidmergeSort(int *a, int size, constint *sequence) { int tmp[SIZE]; bool success = false;
for (int step = 1; step < size; step *= 2) { int left, mid, right; for (left = 0; left < size - step; left = right) { mid = left + step; right = (mid + step > size ? size : mid + step); merge(a, tmp, left, mid, right); } if (success) { printArr(a, size); return; } if (equal(a, sequence, size)) { printf("Merge Sort\n"); success = true; } } }
intmain(void) { int n; int a[SIZE], b[SIZE]; int sequence[SIZE]; int tmp[SIZE];
scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", a + i); b[i] = a[i]; } for (int i = 0; i < n; ++i) { scanf("%d", sequence + i); } if (!insertSort(a, n, sequence)) { mergeSort(b, n, sequence); }