Advertisement
Guest User

Untitled

a guest
Sep 19th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.52 KB | None | 0 0
  1. import scala.util.Random
  2.  
  3. class QuickSort { // main entry of sort algorithm
  4.  
  5.   //partial insert sort
  6.  
  7.   def insertsort(array: Array[Int], lo: Int, hi: Int): Unit = {
  8.     var i: Int = lo
  9.     while (i <= hi) {
  10.       var j: Int = i
  11.       while (j > lo) {
  12.         if (array(j) < array(j - 1)) swap(array, j, j - 1)
  13.         j -= 1
  14.       }
  15.       i += 1
  16.     }
  17.   }
  18.  
  19.   private val cutoff = 6
  20.  
  21.   def quicksort(array: Array[Int]): Unit = {
  22.     shuffling(array)
  23.     sort(array, 0, array.length - 1)
  24.   }
  25.  
  26.   // shuffling for performance guarantee
  27.   private def shuffling(array: Array[Int]): Unit = {
  28.     var i: Int = 0
  29.     while (i < array.length) {
  30.       val r: Random = new Random
  31.       val j: Int = r.nextInt(i + 1)
  32.       if (i != j) swap(array, i, j)
  33.       i += 1
  34.     }
  35.   }
  36.  
  37.   // recursive sort// recursive sort
  38.  
  39.   private def sort(array: Array[Int], low: Int, high: Int): Unit = { // improvement #1
  40.     if (high <= low + cutoff) {
  41.       insertsort(array, low, high)
  42.       return
  43.     }
  44.     // improvement #2
  45.     median3(array, low, high)
  46.     //    ------trational partition-----
  47.     //      // get pivot
  48.     //      int pivot = partition(array, low, high);
  49.     //      // recursive sort
  50.     //      sort(array, low, pivot - 1);
  51.     //      sort(array, pivot + 1, high);
  52.     //    ------3 way partition version--------
  53.     val pivots: Array[Int] = partition3(array, low, high)
  54.     val lt: Int = pivots(0)
  55.     val gt: Int = pivots(1)
  56.     sort(array, low, lt - 1)
  57.     sort(array, gt + 1, high)
  58.   }
  59.  
  60.   // partition// partition
  61.  
  62.   private def partition(array: Array[Int], low: Int, high: Int) = {
  63.     val pivotValue: Int = array(low)
  64.     var i: Int = low
  65.     var j: Int = high + 1
  66.  
  67.     var outerCtrl: Boolean = true
  68.  
  69.     while (outerCtrl) {
  70.       var greaterCtrl = true
  71.       //find the first one greater than pivotValue (left-to-right scan)
  72.       i += 1
  73.       while ( greaterCtrl && (array(i) < pivotValue)) if (i == high) greaterCtrl = false
  74.  
  75.       var lessCtrl = true
  76.       //find the first one less than pivotValue (right-to-left scan)
  77.       j -= 1
  78.       while (lessCtrl && (array(j) > pivotValue)) if (j == low) lessCtrl = false
  79.  
  80.       if (i >= j) outerCtrl = false else swap(array, i, j) //swap them to keep order
  81.     }
  82.     //put pivot to the right place, j will remember the location for pivot
  83.     swap(array, low, j)
  84.     //return the position of pivot
  85.     j
  86.   }
  87.  
  88.   private def partition3(array: Array[Int], lo: Int, hi: Int): Array[Int] = {
  89.     //compare with normal partition//compare with normal partition
  90.  
  91.     //we need two more variables: lt, gt
  92.     //lt: the first element with the pivot value
  93.     //the last element with pivot value
  94.  
  95.     var lt: Int = lo
  96.     var gt: Int = hi
  97.     val cmp: Int = array(lo)
  98.     var i: Int = lo
  99.  
  100.     var ctrl: Boolean = true
  101.     while (ctrl) {
  102.       if (array(i) < cmp) {
  103.         swap(array, i, lt)
  104.         i += 1
  105.         lt += 1
  106.       } else if (array(i) > cmp) {
  107.         swap(array, i, gt)
  108.         gt -= 1
  109.       } else i += 1
  110.  
  111.       if(i > gt) ctrl = false
  112.     }
  113.  
  114.     Array[Int](lt, gt)
  115.   }
  116.  
  117.  
  118.   // swap
  119.   private def swap(array: Array[Int], index1: Int, index2: Int): Unit = {
  120.     val swap: Int = array(index2)
  121.     array(index2) = array(index1)
  122.     array(index1) = swap
  123.   }
  124.  
  125.   def median3(array: Array[Int], lo: Int, hi: Int): Unit = {
  126.     val mid = lo + (hi - lo) / 2
  127.     if (array(lo) > array(hi)) swap(array, lo, hi)
  128.     if (array(mid) > array(hi)) swap(array, mid, hi)
  129.     if (array(mid) < array(lo)) swap(array, lo, mid)
  130.     swap(array, mid, lo)
  131.   }
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement