public class Merge { int[] a = { 12, 454, 1, 44, 33, 0, 85, 41, 87, 26, 6, 7, 80, 32, 12, 15 }; public void mergeSort() { int[] temp = new int[a.Length]; splitMergeSort(a, temp, 0, a.Length - 1); } public void splitMergeSort(int[] a, int[] temp, int left, int right) { if (left < right) { int mid = (left + right) / 2; splitMergeSort(a, temp, left, mid); splitMergeSort(a, temp, mid + 1, right); mergeSortArr(a, temp, left, mid, right); } } public void mergeSortArr(int[] a, int[] temp, int left, int mid, int right) { int k = 0; int i = left; int j = mid + 1; while (i <= mid && j <= right) // 左右两个子数组都有元素时,从小到大复制到临时数组 { if(a[i] <= a[j]) { temp[k++] = a[i++]; } else { temp[k++] = a[j++]; } } while (i <= mid) // 只有左子数组有元素时,将元素全部复制到临时数组 { temp[k++] = a[i++]; } while (right >= j) // 只有右子数组有元素时,将元素全部复制到临时数组 { temp[k++] = a[j++]; } for (int t = 0; t < k; t++) { a[left + t] = temp[t]; } } }
merge_sort([]) -> []; merge_sort([H]) -> [H]; merge_sort(List) -> Mid = erlang:length(List) div 2, {Left, Right} = lists:split(Mid, List), L = merge_sort(Left), R = merge_sort(Right), merge(L, R, []). merge([], [], Acc) -> lists:reverse(Acc); merge([], [H2 | L2], Acc) -> merge([], L2, [H2 | Acc]); merge([H1 | L1], [], Acc) -> merge(L1, [], [H1 | Acc]); merge([H1 | L1], [H2 | L2], Acc) when H1 =< H2 -> merge(L1, [H2 | L2], [H1 | Acc]); merge([H1 | L1], [H2 | L2], Acc) -> merge([H1 | L1], L2, [H2 | Acc]).
test_merge_sort(List) -> Tuple = erlang:list_to_tuple(List), merge_sort(Tuple, 1, length(List), Tuple). merge_sort(Tuple, Left, Right, TupleAcc) when Left < Right -> Mid = (Left + Right) div 2, Tuple1 = merge_sort(Tuple, Left, Mid, TupleAcc), Tuple2 = merge_sort(Tuple1, Mid + 1, Right, TupleAcc), merge(Tuple2, Left, Mid, Right, Left, Mid + 1, []); merge_sort(Tuple, _Left, _Right, _TupleAcc) -> Tuple. %% 将两个子序列元素进行比较,从小到大插入到临时列表中 merge(Tuple, Left, Mid, Right, LeftAcc, MidAcc, TempAcc) when LeftAcc =< Mid andalso MidAcc =< Right -> LeftValue = element(LeftAcc, Tuple), RightValue = element(MidAcc, Tuple), case LeftValue =< RightValue of true -> merge(Tuple, Left, Mid, Right, LeftAcc + 1, MidAcc, [LeftValue | TempAcc]); _ -> merge(Tuple, Left, Mid, Right, LeftAcc, MidAcc + 1, [RightValue | TempAcc]) end; merge(Tuple, Left, Mid, Right, LeftAcc, MidAcc, TempAcc) when LeftAcc =< Mid -> LeftValue = element(LeftAcc, Tuple), merge(Tuple, Left, Mid, Right, LeftAcc + 1, MidAcc, [LeftValue | TempAcc]); merge(Tuple, Left, Mid, Right, LeftAcc, MidAcc, TempAcc) when MidAcc =< Right -> RightValue = element(MidAcc, Tuple), merge(Tuple, Left, Mid, Right, LeftAcc, MidAcc + 1, [RightValue | TempAcc]); merge(Tuple, Left, _Mid, _Right, _LeftAcc, _MidAcc, TempAcc) -> copy_merge(lists:reverse(TempAcc), Left, Tuple). %% 将临时列表中的元素复制回tuple copy_merge([H | TempAcc], Left, Tuple) -> NewTuple = erlang:setelement(Left, Tuple, H), copy_merge(TempAcc, Left + 1, NewTuple); copy_merge([], _Left, Tuple) -> Tuple.