1. Algorithm concept.
Quicksort is an improvement on bubble sort. Proposed by CAR Hoare in 1962.
2. Algorithmic thinking.
Divide the data to be sorted into two independent parts through one-pass sorting. All the data in one part is smaller than all the data in the other part. Then use this method to quickly sort the two parts of the data respectively. The entire sorting process can Proceed recursively, so that the entire data becomes an ordered sequence.
3. Implement ideas.
① Use the first keyword K 1 as the control word, divide [K 1 ,K 2 ,…,K n ] into two sub-areas, so that all keywords in the left area are less than or equal to K 1 and all keywords in the right area are greater than or equal to K 1, and finally the control word is located in the appropriate position between the two sub-areas. The data in the subarea is still in an unordered state.
② Treat the left area as a whole and process it with the steps of ①, and perform the same processing on the right area. (i.e. recursion)
③ Repeat steps ① and ② until the left area is processed.
public static void quickSortByMid(int[] a, int low, int high) { if (low >= high) return; // Split int pivot = a[low]; // Base value int i = low, j = high; while (i < j) { while (i < j && a[j] >= pivot) --j; a[i]=a[j]; while (i < j && a[i] <= pivot) + +i; a[j]=a[i]; } a[i]=pivot; quickSortByMid(a, low, i-1); quickSortByMid(a, i+1, high); }Schematic diagram of quick sort algorithm:
The above is the entire content of this article. I hope it will be helpful for everyone to learn the Java quick sort algorithm.