Merge Sort
Posted codingEskimo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Merge Sort相关的知识,希望对你有一定的参考价值。
public class Solution { /** * @param A an integer array * @return void */ public void sortIntegers2(int[] A) { // Write your code here if (A == null || A.length == 0) { return; } int[] tmp = new int[A.length]; mergeSort(A, 0, A.length - 1, tmp); } private void mergeSort(int[] A, int start, int end, int[] tmp){ if (start >= end) { return; } int mid = start + (end - start) / 2; mergeSort(A, start, mid, tmp); mergeSort(A, mid + 1, end, tmp); merge(A, start, mid, end, tmp); } private void merge(int[] A, int start, int mid, int end, int[] tmp) { int left = start; int right = mid + 1; int index = start; while (left <= mid && right <= end) { if (A[left] < A[right]) { tmp[index++] = A[left++]; } else { tmp[index++] = A[right++]; } } while (left <= mid) { tmp[index++] = A[left++]; } while (right <= end) { tmp[index++] = A[right++]; } for (int i = start; i <= end; i++) { A[i] = tmp[i]; } } }
以上是关于Merge Sort的主要内容,如果未能解决你的问题,请参考以下文章