C + + enables quick sorting

Source: Internet
Author: User

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

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.