public class SortUtil {
public static void main(String[] args) {
int[] from = new int[]{25, 12, 34, 56, 10, 3, 77, 88, 50,5};
standardSort1(from, 0, from.length - 1);
Arrays.stream(from).forEach(e-> System.out.print(e+","));
}
/**
* 快速排序
*
* @param list
*/
static void standardSort1(int[] list, int start, int over) {
if (start >= over) {
return;
}
if (list.length == 2 && list[start] > list[over]) {
int tmp = list[over];
list[start] = list[over];
list[over] = tmp;
return;
}
//标杆值
int key = list[start];
//空洞下标
int dump = start;
int i = start;
int j = over;
//从右往左
boolean fromRight = true;
while (i < j) {
if (fromRight) {
if (key > list[j]) {
//将小于值移到数组左边
list[dump] = list[j];
//临时存放key
list[j] = key;
dump = j;
fromRight = false;
++i;
} else {
--j;
}
} else {
if (key < list[i]) {
//将大于key的值移到数组右边
list[dump] = list[i];
//临时存放key
list[i] = key;
dump = i;
--j;
fromRight = true;
} else {
++i;
}
}
}
standardSort1(list,start,i-1);
standardSort1(list,i+1,over);
}
}
|