快速排序
package src;
public class QuickSort {
public static void main(String[] args) {
int[] array =new int[]{6,5,4,6,9,8};
quickSort(array,0,array.length);
for (int num : array) {
System.out.println(num);
}
}
private static void quickSort(int[] array, int left, int right) {
if(left < right){
int poix =getPoix(array, left, right);
quickSort(array, left, poix-1);
quickSort(array, poix+1, right);
}
}
private static int getPoix(int[] array, int left, int right) {
int poix = left;
int index = left +1;
for (int i = left; i < right; i++) {
if(array[i] < array[poix]) {
swap(array, index, i);
index++;
}
}
swap(array,poix,index -1);
return index -1;
}
private static void swap(int[] array, int index, int i) {
int temp = array[index];
array[index] = array[i];
array[i] = temp;
}
}