1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| #include<iostream> using namespace std;
void Quicksort(int a[], int left, int right) { if (left < right) { int temp = a[left]; int i = left; int j = right; while (i < j) { while (i<j && a[j] > temp) j--; a[i] = a[j];
while (i < j && a[i] <= temp) i++; a[j] = a[i]; } a[i] = temp;
Quicksort(a, left, i - 1); Quicksort(a, i + 1, right); } } int main() { int n[]={5,4,3,2,7,8,1,9,6}; for (int i = 0; i < 9; i++) cout <<n[i]<<' '; cout<<endl; Quicksort(n, 0, 8); for (int i = 0; i < 9; i++) cout<< n[i]<<' '; return 0; }
|