标签:
算法概述:
public class quick { public int[] a = { 6, 7, 2, 5, 1, 9, 4, 8, 3 }; public void Sort() { Sort(a, 0, a.Length - 1); } public void Sort(int[] a, int low, int high) { if (high <= low) return; int mid = splitSort(a, low, high); Sort(a, low, mid - 1); Sort(a, mid + 1, high); } public int splitSort(int[] a, int low, int high) { int v = a[low]; int i = low; int j = high + 1; while (true) { while (a[++i] < v && i < high); while (a[--j] > v && j > 0) ; if (i >= j) break; int temp = a[i]; a[i] = a[j]; a[j] = temp; } int temp1 = a[low]; a[low] = a[j]; a[j] = temp1; return j; } }
sort([]) -> []; sort(List) when is_list(List) -> sort2(List). sort2([]) -> []; sort2([Value | List]) -> {LeftList, RightList} = spilt_sort(List, Value, [], []), sort2(LeftList) ++ [Value]++ sort2(RightList). spilt_sort([], _Value, LeftAcc, RightAcc) -> {LeftAcc, RightAcc}; spilt_sort([H | List], Value, LeftAcc, RightAcc) when H > Value -> spilt_sort(List, Value, LeftAcc, [H | RightAcc]); spilt_sort([H | List], Value, LeftAcc, RightAcc) -> spilt_sort(List, Value, [H | LeftAcc], RightAcc).
标签:
原文地址:http://www.cnblogs.com/beibeiblogs/p/5254136.html