Advertisement
Psycho_Coder

Merge Sort in Java

Oct 10th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public class MergeSort {
  4.  
  5.     /**
  6.      * Merges two arrays into one.
  7.      *
  8.      * @param array
  9.      *            array that contains the two arrays.
  10.      * @param temp
  11.      *            temporary array that we will work with
  12.      * @param low
  13.      *            lowest index value in the array
  14.      * @param mid
  15.      *            middle index value in the array (represents break between low
  16.      *            and high values)
  17.      * @param high
  18.      *            highest index value in the array
  19.      */
  20.     private static void merge(int[] array, int[] temp, int low, int mid,
  21.             int high) {
  22.         int i = low;
  23.         int j = mid;
  24.  
  25.         for (int k = low; k < high; k++) {
  26.             if (i == mid)
  27.                 temp[k] = array[j++]; // if low-mid array is empty
  28.             else if (j == high)
  29.                 temp[k] = array[i++]; // if mid-high array is empty
  30.             else if (array[j] > array[i])
  31.                 temp[k] = array[i++]; // i is lower so we put it in the array
  32.                                         // first
  33.             else
  34.                 temp[k] = array[j++]; // j is lower or equal to i, so we put it
  35.                                         // in
  36.                                         // the array first.
  37.         }
  38.  
  39.         // now we need to copy back temp array to its place in array array
  40.         for (int k = low; k < high; k++)
  41.             array[k] = temp[k]; // we are copying only the numbers we just
  42.                                 // merged.
  43.  
  44.     }
  45.  
  46.     private static void mergeSort(int[] array, int low, int high) {
  47.         if (high - low == 1)
  48.             return; // only one element in the array, return.
  49.         int mid = low + (high - low) / 2; // find middle
  50.         mergeSort(array, low, mid); // sort first part
  51.         mergeSort(array, mid, high); // sort second part
  52.         merge(array, new int[array.length], low, mid, high); // merge both parts
  53.     }
  54.  
  55.     /**
  56.      * Sorts the given list. (Not in place) Worst Case Performance O(nlogn) Best
  57.      * Case Performance O(nlogn) Average Case Performance O(nlogn)
  58.      *
  59.      * @param array
  60.      *            list of int array.
  61.      */
  62.     public static void sortNumbers(int[] array) {
  63.         mergeSort(array, 0, array.length);
  64.         System.out.println(" " + Arrays.toString(array));
  65.     }
  66.  
  67.     /**
  68.      * Sample Test Run
  69.      */
  70.     public static void main(String[] args) {
  71.         int[] list = { 156, 344, 54, 546, 767, 23, 34, 64, 234, 654, 234, 65,
  72.                 234, 65, 87, 3, 5, 76, 24, 2, 3, 7, 9, 5, 34, 32, 4525, 345, 0 };
  73.         sortNumbers(list);
  74.  
  75.     }
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement