1. 普通快速排序
1 #include "pch.h" 2 #include <iostream> 3 using namespace std; 4 int a[100],n; 5 void swap(int i, int j) 6 { 7 int t = a[j]; 8 a[j] = a[i]; 9 a[i] = t; 10 } 11 void quicksort(int l,int r) 12 { 13 if (l > r) 14 return; 15 int i, j; 16 i = l, j = r; 17 while (i != j) 18 { 19 20 while (i < j && a[j] >= a[l]) 21 { 22 j--; 23 } 24 while (i < j && a[i] <= a[l]) 25 { 26 i++; 27 } 28 if (i >= j) 29 break; 30 swap(i, j); 31 } 32 swap(i, l); 33 quicksort(l,i - 1); 34 quicksort(i + 1, r); 35 } 36 int main() 37 { 38 cin >> n; 39 for (int i = 0; i < n; i++) 40 { 41 cin >> a[i]; 42 } 43 quicksort(0, n - 1); 44 for (int i = 0; i < n; i++) 45 { 46 cout << a[i] << " "; 47 } 48 system("pause"); 49 return 0; 50 }
2.