Directly on the code:
voidSwapintA[],intMintN) {inttemp =A[m]; A[M]=A[n]; A[n]=temp;}//At the beginning, left is the starting position of the array 0,right is the subscript for the last element of the array (this is 8)voidQuickSort (intA[],intLeft,intRight ) {//from small to large sort {50,10,90,30,70,40,80,60,20} intLow =Left ; intHigh =Right ; intPartition =A[low]; if(left>=Right ) { return; } while(low!=High ) { while(Low//if the right value is greater than partitionhigh--; } if(low<High ) {Swap (A, low, high); } while(Lowpartition) { Low++; } if(low<High ) {Swap (A, low, high); } }//Printarr (a,9);QuickSort (A, left, low-1); QuickSort (A, low+1, right);}
Here I define a sorting algorithm, a switching algorithm.
Don't understand? Looking too complicated? Well, then look down.
First introduce the principle: (Here we follow from small to large sort)
First, you need to find the first or last element as a hub, and then use a hub to divide the array into two parts, larger than the hub on the right side of the hub, less than the hub on the left side of the hub. Give me a chestnut:
{50,30,40,74,23,67,56,68};
In this array we select the first element as the pivot 50, so all we have to do is put the number less than 50 in the array to the left of 50 and the number greater than 50 to the right of 50. But the number on the left (or right) is not necessarily orderly. For example
{30,40,23,50,74,56,67,68}
This is only roughly orderly.
Then we separate the pivot 50 to the left and to the right, which becomes two arrays
{30,40,23} and {74,56,67,68}
Then, follow the above method to find a hub to sort again:
30 as a hub, get the result: {23,30,40} So the left is ordered;
Then do the same for the other array. Until all is orderly.
Now look at the top of the code, it should be a little better understand it.
----------------------------------------teach you a trick----------------------------------------
After you understand the above-mentioned principles, then write your own code. Or simply copy the code above me for a breakpoint analysis so that you can understand it more deeply and write it yourself.
----------------------------------------'s Over----------------------------------------
If there is any doubt in spite of opening, sparing.
If there are any comments or suggestions, despite the proposed.
Thank you.
A quick sort of sorting algorithm