Advertisement
Giftednarwhals

hw # 17

Dec 6th, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1. // Name                 :       Kyle Blanchard
  2. // Due                  :       12/5/2014
  3. // Class                :       CSCI-401
  4. // Assignment           :   Improve quick sort
  5. // Contact              :   Kwblanchard@student.stcc.edu
  6.  
  7.  
  8. //package csci401;
  9.  
  10. public class medianQuickSort {
  11.    
  12.       public static void quickSort(int[] list) {
  13.         quickSort(list, 0, list.length - 1);
  14.       }
  15.  
  16.       private static void quickSort(int[] list, int first, int last) {
  17.         if (last > first) {
  18.           int pivotIndex = partition(list, first, last);
  19.           quickSort(list, first, pivotIndex - 1);
  20.           quickSort(list, pivotIndex + 1, last);
  21.         }
  22.       }
  23.  
  24.       /** Partition the array list[first..last] */
  25.       private static int partition(int[] list, int first, int last) {
  26.         int pivot = findMedian(list, first, last);
  27.         int low = first + 1; // Index for forward search
  28.         int high = last; // Index for backward search
  29.  
  30.         while (high > low) {
  31.           // Search forward from left
  32.           while (low <= high && list[low] <= pivot)
  33.             low++;
  34.  
  35.           // Search backward from right
  36.           while (low <= high && list[high] > pivot)
  37.             high--;
  38.  
  39.           // Swap two elements in the list
  40.           if (high > low) {
  41.             int temp = list[high];
  42.             list[high] = list[low];
  43.             list[low] = temp;
  44.           }
  45.         }
  46.  
  47.         while (high > first && list[high] >= pivot)
  48.           high--;
  49.  
  50.         // Swap pivot with list[high]
  51.         if (pivot > list[high]) {
  52.           list[first] = list[high];
  53.           list[high] = pivot;
  54.           return high;
  55.         }
  56.         else {
  57.           return first;
  58.         }
  59.       }
  60.  
  61.       // New improved median based quick sort
  62.       public static int findMedian(int[]list, int first, int last){
  63.           // finds the middle number
  64.           int middle = (first+last)/2;
  65.           // if the first element is bigger than the last switch the elements
  66.            if (list[first]>list[last])
  67.                   switchElements(list, first, last);
  68.           // if the first element is bigger than the middle switch the elements
  69.           if (list[first]>list[middle])
  70.                   switchElements(list, first, middle);
  71.           // if the middle element is bigger than the last switch the elements
  72.           if (list[middle]>list[last])
  73.                   switchElements(list, middle, last);
  74.           // the three elements should now be organized from greatest to least
  75.          
  76.           // this puts the pivot as the first element and returns it
  77.           switchElements(list, middle, first);
  78.           return list[first];
  79.       }
  80.       // Switches the elements
  81.       public static void switchElements(int[] list, int x, int y) {
  82.           int temp = list[x];
  83.           list[x] = list[y];
  84.           list[y] = temp;
  85.     }
  86.      
  87.      
  88.       /** A test method */
  89.       public static void main(String[] args) {
  90.         int[] list = {2, 3, 2, 5, 6, 1, -2, 3, 14, 12};
  91.         quickSort(list);
  92.         for (int i = 0; i < list.length; i++)
  93.           System.out.print(list[i] + " ");
  94.       }
  95.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement