#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int arr[N],tmp[N];
void guibin_sort(int arr[], int l, int r)
{
if (l >= r)return;
int mid = l + r >> 1;
guibin_sort(arr, l, mid);
guibin_sort(arr, mid + 1, r);
int k = 0,i=l,j=mid+1;
while (i <= mid && j <= r)
{
if (arr[i] < arr[j])tmp[k++] = arr[i++];
else
tmp[k++] = arr[j++];
}
while (i <= mid)tmp[k++] = arr[i++];
while (j <= r)tmp[k++] = arr[j++];
for (i = l, j = 0; i <= r; i++, j++)arr[i] = tmp[j];
}
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)scanf("%d", &arr[i]);
guibin_sort(arr, 0, n - 1);
for (int i = 0; i < n; i++)printf("%d ", arr[i]);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include <bits/stdc++.h>
using namespace std;
void merge(int a[], int start, int mid, int end)
{
int* tmp = (int*)malloc((end - start + 1) * sizeof(int));
int i = start;
int j = mid + 1;
int k = 0;
while (i <= mid && j <= end)
{
if (a[i] <= a[j])
tmp[k++] = a[i++];
else
tmp[k++] = a[j++];
}
while (i <= mid)
tmp[k++] = a[i++];
while (j <= end)
tmp[k++] = a[j++];
for (i = 0; i < k; i++)
a[start + i] = tmp[i];
free(tmp);
}
void groups(int arr[], int sz, int n)
{
int i = 0;
for (i = 0; i + 2 * n - 1 < sz; i += (2 * n))
{
merge(arr, i, i + n - 1, i + 2 * n - 1);
}
if (i + n - 1 < sz - 1)
{
merge(arr, i, i + n - 1, sz - 1);
}
}
void a_sort(int arr[], int sz)
{
int n;
if (arr == NULL || sz <= 0)
return;
for (n = 1; n < sz; n = n * 2)
groups(arr, sz, n);
}
int main()
{
//输入一组数
int n = 0;
scanf("%d", &n);
int arr[100000] = { 0 };
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
//排序
a_sort(arr, n);
//输出
for (int j = 0; j < n; j++)
{
printf("%d ", arr[j]);
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
void merge(int a[], int start, int mid, int end)
{
int* tmp = (int*)malloc((end - start + 99) * sizeof(int));
int i = start;
int j = mid + 1;
int k = 0;
int* ps = NULL, * point = NULL;
while (i <= mid && j <= end)
{
(a[i] < a[j]) ? (ps = &i) : (ps = &j);
tmp[k++] = a[*ps];
(*ps)++;
}
(i > mid) ? (ps = &j, point = &end) : (ps = &i, point = &mid);
while (*ps <= *point)
{
tmp[k++] = a[*ps];
(*ps)++;
}
for (i = 0; i < k; i++)
a[start + i] = tmp[i];
free(tmp);
}
void groups(int arr[], int sz, int n)
{
int i = 0;
for (i = 0; i + 2 * n - 1 < sz; i += (2 * n))
{
merge(arr, i, i + n - 1, i + 2 * n - 1);
}
if (i + n - 1 < sz - 1)
{
merge(arr, i, i + n - 1, sz - 1);
}
}
void a_sort(int arr[], int sz)
{
int n;
if (arr == NULL || sz <= 0)
return;
for (n = 1; n < sz; n = n * 2)
groups(arr, sz, n);
}
int main()
{
//输入一组数
int n = 0;
scanf("%d", &n);
int arr[100000] = { 0 };
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
//排序
a_sort(arr, n);
//输出
for (int j = 0; j < n; j++)
{
printf("%d ", arr[j]);
}
return 0;
}
三者都行