package com.abin.lee.sort;
public class QuickSort {
public static void quick(int[] number) {
sort(number, 0, number.length - 1);
}
private static void sort(int[] number, int left, int right) {
if (left < right) {
int i = left;
int j = right + 1;
while (true) {
// 向右找
while (i + 1 < number.length && number[++i] < number[left]);
// 向左找
while (j - 1 > -1 && number[--j] > number[left]);
if (i >= j)
break;
swap(number, i, j);
}
swap(number, left, j);
sort(number, left, j - 1);
// 對左邊進行遞迴
sort(number, j + 1, right);
// 對右邊進行遞迴
}
}
private static void swap(int[] number, int i, int j) {
int t = number[i];
number[i] = number[j];
number[j] = t;
}
public static void main(String[] args) {
int[] num=new int[]{1,8,4,2,7,5,6,3};
quick(num);
for(int i=0;i<num.length;i++){
System.out.println(num[i]);
}
}
}