#include <iostream> #include <stdlib.h>using namespace std;int sum (int a,int b) {return (rand ()% (b-a) +a+1);} void grial (int a[],int x,int y) {if (x>=y) return; int i=x;int j=y;int temp;int b=sum (I,J);//Find random values. int Key=a[b];while (i& LT;J) {while (A[i]<key) i++;//finds the first number larger than key while (A[j]>key) j--;//finds the first number smaller than key if (i<=j) {temp=a[i];a[i]=a[j]; a[j]=temp;i++;j--;}} When the i<j, the smaller than the key and larger than key exchange, until i>j, determined the median, less than or equal to J of the array divided to the left of the smaller group, greater than or equal to the division to the right of the larger group,//The array is divided into 2 parts, and then respectively recursive comparison. finally when x< Y jumps out of recursion. Grial (A,X,J); Grial (a,i,y);} int main () {int a[]={2,3,1,94,53,3,0}; Grial (a,0,6); for (int i=0;i<7;i++) {cout<<a[i]<< " ";} Cout<<endl;return 0;}
C + + Quick sort (random value element method)