Data structure-sort-quick row

Source: Internet
Author: User

Quick Sort

First, quickly sort the steps:

  • First select the axis value
  • Divides the content to be sorted into two parts, the left is less than or equal to the axis value, the right is greater than the axis value
  • Then repeat the above steps to the left and right until the entire sequence is ordered
  • Directly on the code here first write the divided code
  • One division here is that the first number is the axis value, and we can use the last or the middle one.
  • #include <iostream>#include<vector>using namespacestd;//an array without a staging unit v1inttemp1[]={ -, -, -, $,98, -, at, the, -, -};vector<int>v1 (Begin (Temp1), End (Temp1));//Print ArrayInlinevoidPRINTV (Constvector<int> &v) {     for(auto a:v) cout<<a<<" "; cout<<Endl;}//First Division//Quick Sort First DivisionintPartition (vector<int> &v,intFirstintend) {    intI=First ; intj=end;    Printv (v); cout<<"at this time i="<<i<<"at this time j="<<j<<"  ";  while(i<j) { while(i<j&&v[i]<V[j]) {J--; cout<<"at this time i="<<i<<"at this time j="<<j<<Endl;} if(I&LT;J)//if i<j exchange ij{cout<<"Exchange"<<"  "; inttemp=V[j]; V[J]=V[i]; V[i]=temp; I++;            Printv (v); cout<<"at this time i="<<i<<"at this time j="<<j<<"  "; } cout<<Endl;  while(i<j&&v[i]<=V[j]) {i++; cout<<"at this time i="<<i<<"at this time j="<<j<<Endl;} if(i<j) {cout<<"Exchange"<<"  "; inttemp=V[j]; V[J]=V[i]; V[i]=temp; J--;            Printv (v); cout<<"at this time i="<<i<<"at this time j="<<j<<"  ";    }} printv (v); returni;}//Here we first call a division understanding how to divideintMainintargcChar*argv[]) {Partition (v1,0, V1.size ()-1); return 0;}

    Then run the analysis

  

    • First take 59 as the axis value, then because the left is smaller than 59, the right is greater than 59
    • 59 and 28 comparison found 59 greater than 28 swap
    • Continue traversing to the left after swapping. So i++, traversing to 98 found 59 small, exchanged
    • Then the left side must be less than 59, but the right side is not sure, start traversing J
    • J=8 found that 13 is less than 59 then swapped again, then the right must be greater than 59, left indeterminate, continue to traverse I
    • I=7 59<83 Exchange
    • This is i=j not meet i<j conditions jump out of the loop
    • At this point 59 is the axis value, the left is less than 59 to the right is greater than

And then there's the complete, recursive, fast-line.

First on the code

#include <iostream>#include<vector>using namespacestd;//an array without a staging unit v1inttemp1[]={ -, -, -, $,98, -, at, the, -, -};vector<int>v1 (Begin (Temp1), End (Temp1));//Print ArrayInlinevoidPRINTV (Constvector<int> &4) {     for(auto a:v) cout<<a<<" "; cout<<Endl;}//Quick Sort One DivisionintPartition (vector<int> &v,intFirstintend) {    intI=First ; intj=end;    Printv (v); cout<<"at this time i="<<i<<"at this time j="<<j<<"  ";  while(i<j) { while(i<j&&v[i]<V[j]) {J--; cout<<"at this time i="<<i<<"at this time j="<<j<<Endl;} if(I&LT;J)//if i<j exchange ij{cout<<"Exchange"<<"  "; inttemp=V[j]; V[J]=V[i]; V[i]=temp; I++;            Printv (v); cout<<"at this time i="<<i<<"at this time j="<<j<<"  "; } cout<<Endl;  while(i<j&&v[i]<=V[j]) {i++; cout<<"at this time i="<<i<<"at this time j="<<j<<Endl;} if(i<j) {cout<<"Exchange"<<"  "; inttemp=V[j]; V[J]=V[i]; V[i]=temp; J--;            Printv (v); cout<<"at this time i="<<i<<"at this time j="<<j<<"  ";    }} printv (v); cout<<"once part ends"<<Endl; returni;}//Quick LinevoidQuickSort (vector<int> &v,intFirstintend) {    if(first<end) {        intpivot=Partition (v,first,end); QuickSort (V,first,pivot-1);; QuickSort (V,pivot+1, end); }}intMainintargcChar*argv[]) {QuickSort (v1,0, V1.size ()-1); return 0;}
    • Let's take a look at the quicksort function, and when the partition function returns I,i is just 59 of the position
    • We now take 59 as the dividing line, divided into the left and right sides, respectively, to continue to divide, until the number of each recursive First-end range =1 jumped
    • Look at the following recursion the first time after the partition call ends, the argument behind Quicksort is the position of the v,,first,i-1 For example, the first recursion is v[0] to 59 minus 1.
    • Then the back is 59+1 to V.end ();
    • We run the program to see
    • This is nonstop to the left left hand side of the recursion

    • It's nonstop to the right right hand recursion. Recursion soon ended because the numbers on the right were few.

    

    

Data structure-sort-quick row

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.