#include <iostream>
using namespace std;
int a[10000], use[10000];
void merge(int arr[], int left, int mid, int right, int temp[]) {
int i = left;
int j = mid + 1;
int t = left;
while (i <= mid && j <= right) {
if (arr[i] <= arr[j]) {
temp[t++] = arr[i++];
} else {
temp[t++] = arr[j++];
}
}
while (i <= mid) {
temp[t++] = arr[i++];
}
while (j <= right) {
temp[t++] = arr[j++];
}
t = left;
while (left <= right) {
arr[left++] = temp[t++];
}
}
void mergeSort(int arr[], int left, int right, int temp[]) {
if (left < right) {
int mid = (left + right) / 2;
mergeSort(arr, left, mid, temp);
mergeSort(arr, mid + 1, right, temp);
merge(arr, left, mid, right, temp);
}
}
int main() {
int n;
int nn = 0;
while (cin >> n, n) {
a[nn++] = n;
}
mergeSort(a, 0, nn - 1, use);
for (int i = 0; i < nn; i++) {
cout << a[i] << " ";
}
return 0;
}
归并排序
最新推荐文章于 2024-08-12 19:32:44 发布