packageCom. Sort;import java.util.Arrays;
public class QuickSort20210429 {
/** * use the highest bit of the data as the middle number and then do a quick sort *@param list
* @param low
* @param high
* @return* /
public static int substring(int[] list,int low,int high){
// Define the starting subscript on the left
int n = low-1;
// Define the starting subscript on the right
int m = high;
// The highest bit is declared as the intermediate value
int center = list[high];
// Start a loop
while(true) {// n < m coordinates loop to find coordinates that are greater than the median from front to back
while (list[++n]<center && n<m){}
// n < m coordinates loop to find the coordinates of values from back to front that are less than the middle number
while (list[--m]>center && n<m){}
if(n<m){
// Start switching places after finding these two numbers.
swap( list , n, m);
}else {
// do not find until n>m completes the loop
break; }}// Place the median where the coordinates meet
swap( list , n, high);
// Returns the index of the median of the currently quicksorted shard data
return n;
}
// Sort methods recursively
public static void sort(int[] list,int low,int high){
if(low<high){
// Start with a sharding to find an intermediate value
int temp = substring(list,low,high);
// If the middle value is found, the sorting on the left and the sorting on the right will recursively be the last data to complete the sorting
sort(list,low,temp-1);
sort(list,temp+1,high); }}/** * switch positions *@param list
* @param indexA
* @param indexB
*/
private static void swap(int[] list ,int indexA,int indexB){
// a/B swap needs to be temporarily stored and then swapped
int temp = list[indexA];
list[indexA]= list[indexB];
list[indexB]=temp;
}
public static void main(String[] args){
int[] arr = {10.7.2.4.7.62.3.4.2.1.8.9.19};
sort(arr, 0, arr.length-1);
Arrays.stream(arr).forEach(one->{
System.out.print(one + ""); }); }}Copy the code