package com.yu.datastructure.sort;
//核心思路 挖坑思路 以数组第一个元素为标志 一趟排序后 左边都比他小 右边都比他大
public class FastSort {
public static void sort(int[] a , int start, int end){
if(start >= end)
return;
//标志元素
int first = a[start];
//坑下表
int insertIndex = start;
int i = start+1, j= end;
while(i<=j){
//从高位找 小于坑元素 的地方 重设坑位置
if( j > insertIndex){
if(a[j] < first){
a[insertIndex] = a[j];
insertIndex = j;
}
j--;
}
//从低位找 大于坑元素 的地方 重设坑位置
if( i< insertIndex){
if(a[i] > first){
a[insertIndex] = a[i];
insertIndex = i;
}
i++;
}
}
//把标志元素 放回数组
a[insertIndex] = first;
//递归左边
sort(a,start,insertIndex -1);
//递归右边
sort(a,insertIndex+1,end);
}
public static void main(String[] args) {
int[] a = new int[]{7,4,6,6,5,4,0,9,9};
sort(a,0,a.length-1);
for(int i : a){
System.out.println(i);
}
}
}