快速排序(quickSort)(Java语言)
1 package t0505; 2 import java.util.Arrays; 3 4 /** 5 * @author LU 6 * 7 * 2021年5月5日 8 */ 9 public class QuickSort1 { 10 public static void swap(int [] data ,int i, int j){ 11 int temp= data[i]; 12 data [i]=data[j]; 13 data[j]=temp;
14
15 } 16 public static void subSort(int [] data,int start ,int end){ 17 if(start<end){ 18 int base=data[start]; 19 int low = start; 20 int high = end+1; 21 while(true){ 22 while(low<end && data[++low]-base<=0) 23 ; 24 while(high>start && data[--high]-base>=0) 25 ; 26 if(low<high){ 27 swap(data, low, high); 28 }else{ 29 break; 30 } 31 32 } 33 swap(data, start, high); 34 subSort(data, start, high-1); 35 subSort(data, high+1, end); 36 } 37 } 38 public static void quickSort(int []data){ 39 subSort(data, 0,data.length-1 ); 40 } 41 public static void main(String [] args){ 42 int arr[]={5,1,72,3,26,-5,16,9,10,-99,-51,52,103,0,-18}; 43 System.out.println("排序前:"); 44 System.out.println(Arrays.toString(arr)); 45 quickSort(arr); 46 System.out.println("排序后:"); 47 System.out.println(Arrays.toString(arr)); 48 49 } 50 51 }