tankdthedruid

quickSort.java

Jul 3rd, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1.  
  2. public class quickSort {
  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,13};
  11.  
  12.         System.out.println(" Quick Sort\n\n");
  13.         System.out.println("Values Before the sort:\n");
  14.         for(i = 0; i < array.length; i++)
  15.             System.out.print( array[i]+"  ");
  16.         System.out.println();
  17.         quick_srt(array,0,array.length-1);
  18.         System.out.print("Values after the sort:\n");
  19.         for(i = 0; i <array.length; i++)
  20.             System.out.print(array[i]+"  ");
  21.         System.out.println();
  22.         System.out.println("PAUSE");
  23.     }
  24.  
  25.     public static void quick_srt(int array[],int low, int n)
  26.     {
  27.         int lo = low;
  28.         int hi = n;
  29.         if (lo >= n)
  30.         {
  31.             return;
  32.         }
  33.         int mid = array[(lo + hi) / 2];
  34.         while (lo < hi)
  35.         {
  36.             while (lo<hi && array[lo] < mid)
  37.             {
  38.                 lo++;
  39.             }
  40.             while (lo<hi && array[hi] > mid)
  41.             {
  42.                 hi--;
  43.             }
  44.             if (lo < hi)
  45.             {
  46.                 int T = array[lo];
  47.                 array[lo] = array[hi];
  48.                 array[hi] = T;
  49.             }
  50.         }
  51.         if (hi < lo)
  52.         {
  53.             int T = hi;
  54.             hi = lo;
  55.             lo = T;
  56.         }
  57.         quick_srt(array, low, lo);
  58.         quick_srt(array, lo == low ? lo+1 : lo, n);
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment