Quick sort detailed summary

Notice! ! ! ! Super detailed quick sort summary~~~~~~,

The idea of ​​quick sorting: Described in human language is to first find a standard for the array (not dead, I take the last number of the array as the standard) and then divide the array into two parts, one part is less than this standard, one part is greater than this standard, then this The new array is three parts: the part smaller than this criterion + the criterion + the part larger than this criterion. Then recursively use the above method for those two parts, then in the end the array is in order~\(≧▽≦)/~

 

step:

      1: Let the last number of the array be used as the standard mark, define p (the starting subscript of the target array of the above method), r (the end subscript of the target array of the above method) p<= (the part smaller than the standard)<=i (i+1)<=(the part greater than the standard)<j Then change the position of the last standard and the number of i+1, then the array is now: the part less than this standard + standard + greater than this standard part of this construct.

     2: Recursively call the part of the array that is smaller than the standard and the part that is greater than the standard, and the p r at this time will change! ! ! ! ! ! Notice! ! So parameters can be used to tell the method what pr is.

    3: Then recursively recursively recursively. . . . . . It's in order at the end~~~~

It doesn't seem to be detailed enough,,,,,, if you don't understand, you can leave a message and poke me, I will work hard to save you~~~~~

 

code show as below:

 1 //快速排序
 2     public static int partition(int[] array,int p,int r){
 3         int i=p-1;
 4         int mark=array[r];
 5         for(int j=p;j<r;j++){
 6             if(array[j]<mark){
 7                 i++;
 8                 int temp=array[i];
 9                 array[i]=array[j];
10                 array[j]=temp;
11             }
12         }
13         int temp=array[i+1];
14         array[i+1]=array[r];
15         array[r]=temp;
16         return i+1;
17     }
18     
19     public static void quickSort(int [] array,int p,int r){
20         int q;
21         if(p<r){
22             q=partition(array,p,r);
23             quickSort(array,p,q-1);
24             quickSort(array,q+1,r);
25         }
26     }
27     

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325182914&siteId=291194637