#include #define N 100000 int find_smallest(); void merge(); void copy(); /* Sort a sequence of n numbers in S */ void sort(int S[], int n) { int m; int i; if (n == 1) return; i = find_smallest(S, n); // Swap S[0] and S[i] m = S[0]; S[0] = S[i]; S[i] = m; sort(S+1, n-1); } /* Finds the smallest number is the sequence stored * in S and returns the index of the smallest number. * The size of S is n. */ int find_smallest(int S[], int n) { int t; t = 0; for (int i = 1; i < n; i++) if (S[i] < S[t]) // S[i] is smaller t = i; return t; } /* Sort a sequence of n numbers in S using merge-sort algorithm. */ void merge_sort(int S[], int n) { int T[N]; // temporary storage int m; if (n == 1) // sequence already sorted return; m = n / 2; merge_sort(S, m); // sort first m elements merge_sort(S+m, n - m); // sort last n-m elements merge(S, m, S+m, n-m, T); // merge the two sequences in T copy(T, n, S); // copy T to S } /* Merges the two sorted sequences S1 and S2 and stores in T */ void merge(int S1[], int n1, int S2[], int n2, int T[]) { if (n1 == 0) { // first seqence empty; copy S2 to T copy(S2, n2, T); return; } if (n2 == 0) { // second sequence empty; copy S1 to T copy(S1, n1, T); return; } if (S1[0] < S2[0]) { // S1[0] is the smallest number T[0] = S1[0]; // copy smallest number to T[0] merge(S1+1, n1-1, S2, n2, T+1); // merge the rest } else { // S2[0] is the smallest number T[0] = S2[0]; // copy smallest number to T[0] merge(S1, n1, S2+1, n2-1, T+1); // merge the rest } } /* Copies the sequence T of size n to S */ void copy(int T[], int n, int S[]) { for (int i = 0; i < n; i++) S[i] = T[i]; } int main() { int S[N]; int n; // Read a sequence scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", S+i); merge_sort(S, n); // sort it // outout sorted sequence for (int i = 0; i < n; i++) printf("%d ", S[i]); printf("\n"); }