Quick Sort is an improvement on bubble sort based on the binary idea. The main idea is to establish a base, put the numbers smaller than the base to the left of the base, and put the numbers larger than the base to the right of the base, and then further sort the two parts of the numbers to achieve sorting of the array.
Its advantage is high efficiency, and the average time complexity is O(nlogn). As the name suggests, quick sort is the fastest sorting algorithm and consumes few resources. In the best case, the space complexity is O(logn), and it is divided equally every time. In the case of arrays, the code is relatively simple.
Its disadvantage is that it is unstable. When the initial sequence is ordered or basically ordered, the time complexity drops to O(n^2).
For example:
publicclassMain{//Quick row implementation method publicstaticvoidquickRow(int[]array,intlow,inthigh){inti,j,pivot;//End condition if(low>=high){return;}i=low;j=high;/ /The selected node, the first number of the array selected here is used as the node pivot=array[low];while(i<j){//Look for a number smaller than the node from right to left. At the end of the loop, either it is found or i =jwhile(array[j]>=pivot&&i<j){j--;}//Look for a number larger than the node from left to right. At the end of the loop, either it is found, or i=jwhile(array[i]<=pivot&&i <j){i++;}//If i!=j instructions are found, exchange the two numbers if(i<j){inttemp=array[i];array[i]=array[j];array [j]=temp;}}//i==j A cycle ends, the number of exchange nodes and the number of meeting points array[low]=array[i];array[i]=pivot;//array "divided" "Two halves", then repeat the above operation quickRow(array,low,i-1);quickRow(array,i+1,high);}publicstaticvoidmain(String[]args){int[]array={6,17, 38,59,2,10};intlow=0,high=array.length-1;quickRow(array,low,high);for(inti:array){System.out.println(i);}}}
The running results are as follows:
2610173859