MergeSort.java

  1. package org.drip.zen.algorithm;

  2. public class MergeSort {

  3.     static void SwapLocations (
  4.         double[] numberArray,
  5.         final int location1,
  6.         final int location2)
  7.     {
  8.         double tempNumber = numberArray[location1];
  9.         numberArray[location1] = numberArray[location2];
  10.         numberArray[location2] = tempNumber;
  11.     }

  12.     static void SplitAndMerge (
  13.         double[] numberArray,
  14.         int startLocation,
  15.         int endLocation)
  16.     {
  17.         if (startLocation == endLocation)
  18.         {
  19.             return;
  20.         }

  21.         if (startLocation == endLocation - 1)
  22.         {
  23.             if (numberArray[startLocation] > numberArray[endLocation])
  24.                 SwapLocations (numberArray, startLocation, endLocation);

  25.             return;
  26.         }

  27.         double[] mergedNumberArray = new double[endLocation - startLocation + 1];
  28.         int midLocation = (startLocation + endLocation) / 2;
  29.         int rightStartLocation = midLocation + 1;
  30.         int rightLocation = rightStartLocation;
  31.         int leftStartLocation = startLocation;
  32.         int leftLocation = leftStartLocation;
  33.         int rightEndLocation = endLocation;
  34.         int leftEndLocation = midLocation;
  35.         int mergedArrayLocation = 0;

  36.         SplitAndMerge (numberArray, leftStartLocation, leftEndLocation);

  37.         SplitAndMerge (numberArray, rightStartLocation, rightEndLocation);

  38.         while (leftLocation <= leftEndLocation && rightLocation <= rightEndLocation)
  39.         {
  40.             if (numberArray[leftLocation] < numberArray[rightLocation]) {
  41.                 mergedNumberArray[mergedArrayLocation] = numberArray[leftLocation];
  42.                 mergedArrayLocation = mergedArrayLocation + 1;
  43.                 leftLocation = leftLocation + 1;
  44.             }
  45.             else if (numberArray[leftLocation] > numberArray[rightLocation])
  46.             {
  47.                 mergedNumberArray[mergedArrayLocation] = numberArray[rightLocation];
  48.                 mergedArrayLocation = mergedArrayLocation + 1;
  49.                 rightLocation = rightLocation + 1;
  50.             }
  51.             else
  52.             {
  53.                 mergedNumberArray[mergedArrayLocation] = numberArray[leftLocation];
  54.                 mergedNumberArray[mergedArrayLocation] = numberArray[rightLocation];
  55.                 mergedArrayLocation = mergedArrayLocation + 1;
  56.                 leftLocation = leftLocation + 1;
  57.                 mergedArrayLocation = mergedArrayLocation + 1;
  58.                 rightLocation = rightLocation + 1;
  59.             }
  60.         }

  61.         while (leftLocation <= leftEndLocation)
  62.         {
  63.             mergedNumberArray[mergedArrayLocation] = numberArray[leftLocation];
  64.             mergedArrayLocation = mergedArrayLocation + 1;
  65.             leftLocation = leftLocation + 1;
  66.         }

  67.         while (rightLocation <= rightEndLocation)
  68.         {
  69.             mergedNumberArray[mergedArrayLocation] = numberArray[rightLocation];
  70.             mergedArrayLocation = mergedArrayLocation + 1;
  71.             rightLocation = rightLocation + 1;
  72.         }

  73.         mergedArrayLocation = 0;

  74.         for (int mergedArrayLeftLocation = leftStartLocation; mergedArrayLeftLocation <= leftEndLocation; mergedArrayLeftLocation = mergedArrayLeftLocation + 1)
  75.         {
  76.             numberArray[mergedArrayLeftLocation] = mergedNumberArray[mergedArrayLocation];
  77.             mergedArrayLocation = mergedArrayLocation + 1;
  78.         }

  79.         for (int mergedArrayRightLocation = rightStartLocation; mergedArrayRightLocation <= rightEndLocation; mergedArrayRightLocation = mergedArrayRightLocation + 1)
  80.         {
  81.             numberArray[mergedArrayRightLocation] = mergedNumberArray[mergedArrayLocation];
  82.             mergedArrayLocation = mergedArrayLocation + 1;
  83.         }
  84.     }

  85.     static void SplitAndMerge (
  86.         double[] numberArray)
  87.     {
  88.         SplitAndMerge (numberArray, 0, numberArray.length - 1);
  89.     }

  90.     public static void main (
  91.         String[] input)
  92.     {
  93.         double[] unsortedNumberArray = {6, 1, 5, 7, 9, 2, 8, 4, 3, 12};

  94.         SplitAndMerge (unsortedNumberArray);

  95.         for (int i = 0; i < unsortedNumberArray.length; i = i + 1)
  96.         {
  97.             System.out.println (unsortedNumberArray[i]);
  98.         }
  99.     }
  100. }