/** * Created by huazi on 16/8/8. */ public class KSPX { public static void main(String[] args) { int Array[] = {1, 22, 5, 7, 14, 8}; quick(Array); for (int i = 0; i <Array.length ; i++) { System.out.println(Array[i]); } } public static int getMiddle(int arr[], int low, int high) { int tmp = arr[low]; //数组的第一个作为中轴 while (low < high) { while (low < high && arr[high] > tmp) { high--; } arr[low] = arr[high]; //比中轴小的记录移到低端 while (low < high && arr[low] < tmp) { low++; } arr[high] = arr[low]; //比中轴大的记录移到高端 } arr[low] = tmp; //中轴记录到尾 return low; } public static void _quickSort(int[] list, int low, int high) { if (low < high) { int middle = getMiddle(list, low, high); //将list数组进行一分为二 _quickSort(list, low, middle - 1); //对低字表进行递归排序 _quickSort(list, middle + 1, high); //对高字表进行递归排序 } } public static void quick(int[] str) { if (str.length > 0) { //查看数组是否为空 _quickSort(str, 0, str.length - 1); } } }
转载于:https://my.oschina.net/u/2523402/blog/729575