chelseawangsf
3/6/2018 - 7:21 PM

[MergeSort]Merge Sort #LeetCode #Java #Recursion #Sort

[MergeSort]Merge Sort #LeetCode #Java #Recursion #Sort

public class Solution {
  public int[] mergeSort(int[] array) {
    // Write your solution here
    if (array == null || array.length == 0) {
    	return array;
    }	
    int[] helper = new int[array.length];
    mergeSort(array, helper, 0, array.length - 1);
    return array;
  }
  
  private void mergeSort(int[] array, int[] helper, int left, int right) {
  	if (left >= right) {
      	return;
    }
    int mid = left + (right - left) / 2;
    
    mergeSort(array, helper,  left, mid);
    mergeSort(array, helper, mid + 1, right);
	combine(array, helper, left, mid, right);
    
  }
  
  private void combine(int[] array, int[] helper, int left, int mid, int right) {
  	for (int i = left; i <= right; i++) {
      helper[i] = array[i];
    }
    
    int leftIndex = left;
    int rightIndex = mid + 1;
    
    while (leftIndex <= mid && rightIndex <= right) {
      if (helper[leftIndex] <= helper[rightIndex]) {
      	array[left++] = helper[leftIndex++];
      } else {
      	array[left++] = helper[rightIndex++];
      }
      
    }
    
    while (leftIndex <= mid) {
    	array[left++] = helper[leftIndex++];
    }
  }
}