merge sort

merge sort 


public class MergeSort{
  public int[] mergeSort(int[] array){
    if(array == null){
      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);
    merge(array, helper, left, mid, right);
  }
  
  private void merge(int[] array, int[] helper, int left, int mid, int right){
    // copy the content to helper array and we will merge from the helper array
    for(int i = 0; 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++];
      }
    }
    
    // if we still have some elements at left side , we need to copy them 
    while(leftIndex <= mid){
      array[left++] = helper[leftIndex++];
    }
    
    // if there are some elements at right side, we do not need to copy them
    // because they are already in their positions
  }
  
  public static void main(String[] args){
    MergeSort solution = new MergeSort();
    
    // test cases to cover all possible situations 
    int[] array = null;
    array = solution.mergeSort(array);
    System.out.println(Arrays.toString(array));
    
    array = new int[0];
    array = solution.mergeSort(array);
    System.out.println(Arrays.toString(array));
    
    
    array = new int[]{4, 3, 2, 1};
    array = solution.mergeSort(array);
    System.out.println(Arrays.toString(array));
  }
}

 

posted on 2018-09-20 18:05  猪猪&#128055;  阅读(141)  评论(0)    收藏  举报

导航