快速排序

来源:互联网 发布:苹果手机下载不了淘宝 编辑:程序博客网 时间:2024/06/05 11:01
快速排序
public class Test {public static void main(String[] args) {int[] a = { 12, 3, 4, 5, 2, 65, 43, 23 };fastsort(a, 0, a.length - 1);for(int i =0 ; i<a.length;i++)System.out.print(a[i]+",");System.out.println();}private static void fastsort(int[] a, int low, int high) {if (low >= high)return;int loc = loc(a, low, high);fastsort(a, low, loc-1);fastsort(a, loc + 1, high);}private static int loc(int[] a, int low, int high) {int flag = a[low];while (low < high) {while (low < high && a[high] > flag) {high--;}if (low < high) {a[low++] = a[high];}while (low < high && a[low] <= flag) {low++;}if (low < high) {a[high--] = a[low];}a[low] = flag;}return low;}}

0 0