1 //============================================================================2 //Name:c++basic.cpp3 //author:laughing4 //Version:5 //Copyright:all Right Reserved6 //Description: Quick sorting: Using split-treatment method7 //============================================================================8 9#include <iostream>Ten#include <stdlib.h> One#include <time.h> A using namespacestd; - /** - * Quick sorting: Using the Divide and conquer method the */ - intPartitionintArray[],intStartintEnd) {//returns the subscript of a datum - if(Array = =NULL) { -cout <<"array is empty"<<Endl; + Throw Newexception (); - } + int Base= rand ()% (End-start +1);//if the benchmark is a random element, then the number of recursion is variable, that is, time complexity is different! ★ A //int base = 0;//the number of recursion is fixed if the datum is the first element or a fixed element of the array. atcout <<"benchmarks are:"<<Base<<Endl; - intBasic = array[Base];//Basic as a benchmark - while(Start <end) { - while(Start < end && Array[end] >Basic) { -end--; - } in if(Start < end) {//★ -array[Base] =Array[end]; to Base= END;//change the subscript of a datum point + } - while(Start < end && Array[start] <Basic) { thestart++; * } $ if(Start < end) {//★Panax Notoginsengarray[Base] =Array[start]; - Base= start;//change the subscript of a datum point the } + } Aarray[Base] =Basic; the return Base; + } - voidQuickSort (intArray[],intStartintend) { $ if(Start <end) { $ int Base=partition (array, start, end); -QuickSort (Array,0,Base-1); -QuickSort (Array,Base+1, end); the } - }Wuyi intMain () { the //srand ((int) time (0));//Initialize random seed: If not initialized, the random seed of each recursive will not change and the number of recursion is fixed. - intArray[] = {4,3,2,1,9,7,5,8,6 }; Wu intSize =sizeof(array)/sizeof(*array);//Find Array Length -QuickSort (Array,0, Size-1); About for(inti =0; i < size; i++) { $cout << Array[i] <<Endl; - } - return 0; -}
C + + enables quick sorting