1 #include<bits/stdc++.h> 2 using namespace std; 3 4 int nums[100000]; 5 6 void insertSort(int l, int r) { 7 for (int i = l + 1; i <= r; i++) { 8 for (int j = i; j >= l && nums[j] < nums[j - 1]; j--) { 9 int temp = nums[j]; nums[j] = nums[j - 1]; nums[j - 1] = temp; 10 } 11 } 12 } 13 14 void sort(int start, int end) { 15 if (start >= end) 16 return; 17 if (end - start <= 10) { 18 insertSort(start, end); 19 return; 20 } 21 int i = start; 22 int j = end; 23 int pivot = rand() % (end - start + 1) + start; 24 int te = nums[pivot]; nums[pivot] = nums[start]; nums[start] = te; 25 bool flag = false; 26 while (i != j) { 27 while (i < j && nums[j] >= te) { 28 if (nums[j] != te) 29 flag = true; 30 j--; 31 } 32 while (i < j && nums[i] <= te) { 33 if (nums[i] != te) 34 flag = true; 35 i++; 36 } 37 if (i < j) { 38 int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; 39 } 40 } 41 nums[start] = nums[j]; nums[j] = te; 42 //若该区段全为相同,则不再继续 43 if (flag) { 44 sort(start, j - 1); 45 sort(j + 1, end); 46 } 47 } 48 49 50 int main() { 51 int n; 52 cin >> n; 53 for (int i = 0; i < n; i++) { 54 cin >> nums[i]; 55 } 56 57 sort(0, n-1); 58 for (int i = 0; i < n; i++) { 59 cout << nums[i]<<" "; 60 } 61 62 return 0; 63 }