Guest User

Untitled

a guest
May 23rd, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1. private static void quickSort(int[] array, int lowIndex, int highIndex) //A quicksort for only a specified range of a given array - recursable
  2.   {
  3.      //First of all - stopping conditions
  4.      if (lowIndex + 1 >= highIndex) //If there are two members
  5.      {
  6.         if (array[lowIndex] > array[highIndex]) //If they aren't sorted
  7.             swap(array,lowIndex,highIndex); //Swap them
  8.         return; //A presto, we're sorted on this levl
  9.      }
  10.        
  11.      //Else - meaning more than 2 members
  12.      //First, we need to partition the array in it's bounds.
  13.      int mIndex = partition (array, lowIndex, highIndex);  //This partitions the array into members smaller and bigger than the pivot (which is found in-method). It then gets the index of the median.
  14.      
  15.      //Now we have to recurse this on each of the sub-arrays (partitions)  
  16.      quickSort(array, lowIndex, mIndex - 1); //Quicksorts the subarray between the lowest index given and the median.
  17.      quickSort(array, mIndex + 1, highIndex); //Quicksorts the subarray between the median and the highest index.        
  18.     }
  19.  
  20.   private static int partition(int[] array, int lowIndex, int highIndex)      //This partitions the array into members smaller and bigger than the pivot.
  21.   {
  22.       int midIndex = ((lowIndex + highIndex) / 2); //sets the middle index
  23.       int pivot = findMedian(array, lowIndex + 1, highIndex, midIndex); //Finds the index of the approximate median member and uses it as pivot
  24.       swap(array, lowIndex, pivot); //Swaps the first element of the given bounds with the pivot
  25.       int medianIndex = partition (array, lowIndex + 1, highIndex);
  26.       swap(array, lowIndex, pivot);
  27.       return pivot;
  28.   }      
  29.  
  30.  
  31.  private static int findMedian(int[] array, int lowIndex, int highIndex, int midIndex)
  32.  {
  33.      //This method compares three members of the array and returns the index of the median amongst them (value-wise).
  34.      
  35.      if (array[lowIndex] <= array[highIndex]) // L <= H (low < high)    
  36.         if (array[highIndex] <= array[midIndex]) // L <= H <= M == H
  37.             return highIndex;
  38.         else if (array[lowIndex] <= array[midIndex]) // L <= M <= H == M
  39.             return  midIndex;
  40.         else // M <= L <= H == L - only remaining option
  41.             return lowIndex;
  42.      else  //L > H
  43.         if (array[lowIndex] <= array[midIndex]) // H < L <= M == L
  44.             return lowIndex;
  45.         else if (array[highIndex] <= array[midIndex]) //H <= M < L
  46.             return midIndex;
  47.         else // M <= H < L == M, only remaining option
  48.             return highIndex;
  49.  }
  50.  
  51.  private static void swap(int[] array, int n, int m)
  52.  {
  53.      //This swaps two members of a given array
  54.      int tmp = array[n];
  55.      array[n] = array[m];
  56.      array[m] = tmp;    
  57.  }
Add Comment
Please, Sign In to add comment