C + + Quick sort (random value element method)

Source: Internet
Author: User
#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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.