Quicksort------代码之美
#include<iostream> #include<cstdlib> #include<time.h> using namespace std; void swap(const int a,const int b,int x[]) { int temp = x[a]; x[a] = x[b]; x[b] = temp; } void quicksort(int l, int u, int x[])//1.386nlgn 实际是二分搜索树 { int i, m; if (l >= u)return; swap(l, (rand() % (u - l + 1) + l),x); m = l; for (i = l + 1; i <= u; i++) if (x[i] < x[l]) swap(++m, i, x); swap(l, m, x); quicksort(l, m - 1, x); quicksort(m + 1, u, x); } int main() { srand(time(0)); int x[100]; int p = 10; for (int i = 0; i < p; i++) cin >> x[i]; quicksort(0, p-1, x); for (int i = 0; i <p; i++) cout << x[i] << endl; }