题解 | #排序#
排序
https://www.nowcoder.com/practice/508f66c6c93d4191ab25151066cb50ef
快速排序 #include<iostream> #include<algorithm> using namespace std; int partition(int arr[],int l,int r) { int m = arr[l]; while (l < r) { while (l < r && m <= arr[r]) r--; arr[l] = arr[r]; while (l < r && arr[l] <= m) l++; arr[r] = arr[l]; } arr[l] = m; return l; } void quicksort(int arr[],int l, int r) { if (l >= r) return ; int m = partition(arr,l, r); quicksort(arr, l, m - 1); quicksort(arr,m + 1,r); } int main() { int n; int arr[101]; scanf("%d", &n); for (int i = 0; i < n; i++) { cin >> arr[i]; } quicksort(arr, 0, n-1); for (int i = 0; i < n; i++) { cout << arr[i] << ' '; } }