tankdthedruid

mergeSort.java

Jul 3rd, 2012
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | None | 0 0
  1.  
  2. public class mergeSort {
  3.  
  4.     /**
  5.      * @param args
  6.      */
  7.     public static void main(String a[])
  8.     {
  9.         int i;
  10.         int array[] = {12,9,4,99,120,1,3,10};
  11.         System.out.println(" Merge Sort\n\n");
  12.         System.out.println("Values Before the sort:\n");
  13.         for(i = 0; i < array.length; i++)
  14.             System.out.print( array[i]+"  ");
  15.         System.out.println();
  16.         mergeSort_srt(array,0, array.length-1);
  17.         System.out.print("Values after the sort:\n");
  18.         for(i = 0; i <array.length; i++)
  19.             System.out.print(array[i]+"  ");
  20.         System.out.println();
  21.         System.out.println("PAUSE");
  22.     }
  23.  
  24.     public static void mergeSort_srt(int array[],int lo, int n)
  25.     {
  26.         int low = lo;
  27.         int high = n;
  28.         if (low >= high)
  29.         {
  30.             return;
  31.         }
  32.         int middle = (low + high) / 2;
  33.         mergeSort_srt(array, low, middle);
  34.         mergeSort_srt(array, middle + 1, high);
  35.         int end_low = middle;
  36.         int start_high = middle + 1;
  37.         while ((lo <= end_low) && (start_high <= high))
  38.         {
  39.             if (array[low] < array[start_high])
  40.             {
  41.                 low++;
  42.             }
  43.             else
  44.             {
  45.                 int Temp = array[start_high];
  46.                 for (int k = start_high- 1; k >= low; k--)
  47.                 {
  48.                     array[k+1] = array[k];
  49.                 }
  50.                 array[low] = Temp;
  51.                 low++;
  52.                 end_low++;
  53.                 start_high++;
  54.             }
  55.         }
  56.     }  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment