package sort;
import org.junit.jupiter.api.Test;
/**
* @Author fandayong001
* @Date 2021-08-04
* @description
*/
public class FastSortMy {
int[] arr = new int[]{4,5,7,2,3,9,8,6,8,5};
@Test
public void test(){
quickSort(arr, 0, arr.length - 1);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
/**
* 快排怎么排的
* @param arr
* @param low
* @param high
*/
public void quickSort(int[] arr,int low,int high){
if(low>high){
return;
}
int i,j,temp,t;
i = low;
j = high;
temp = arr[low];
// 先找出中间的值再去快排左和右
while(i<j){
while(arr[j]>temp&&i<j){
j--;
}
while(arr[i]<=temp&&i<j){
i++;
}
if(i!=j){
t = arr[j];
arr[j] = arr[i];
arr[i] = t;
}
}
arr[low] = arr[i];
arr[i] = temp;
quickSort(arr,low,i-1);
quickSort(arr,i+1,high);
}
}
|