快排案例
import java.util.ArrayList;
import java.util.Arrays;
/**
* Created by shiqiang on 2017/11/9.
*/
public class FastSort {
/**
* 排序方法
* @param numberList 需要排序数值数组
* @param low 数组最小的索引坐标
* @param height 数组最大的索引坐标
* @return 每次返回中轴的索引
*/
public static int sort(Integer [] numberList, int low, int height)
{
int index = numberList[low];
while (low < height)
{
while (low < height && numberList[height] >= index)
{
height --;
}
numberList[low] = numberList[height];
while (low < height && numberList[low] <= index)
{
low ++;
}
numberList[height] = numberList[low];
}
numberList[low] = index;
return low;
}
/**
* 进行递归调用, 每次获得一个中轴索引middle
* @param list 每次需要处理的数组
* @param low 最小索引
* @param height 最大索引
*/
public static void execute(Integer [] list, int low,int height)
{
if (low < height)
{
int middle = sort(list, low, height);
execute(list, low, middle -1);
execute(list, middle + 1, height);
}
}
public static void main(String [] args)
{
Integer[] list={34,3,53,2,23,14,14,10};
execute(list, 0, list.length - 1);
System.out.println(Arrays.toString(list));
}
}