Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.64 KB | None | 0 0
  1. package ru.brainsofter.lernkotlin
  2.  
  3. internal class ArrayIns(max: Int) {
  4.     private val theArray: LongArray
  5.     private var nElems: Int = 0
  6.  
  7.     init {
  8.         theArray = LongArray(max)
  9.         nElems = 0
  10.     }
  11.  
  12.     fun insert(value: Long) {
  13.         theArray[nElems] = value
  14.         nElems++
  15.     }
  16.  
  17.     fun display(){
  18.         print("A=")
  19.         for (j in 0..nElems - 1) print(theArray[j].toString() + " ")
  20.         println("")
  21.     }
  22.  
  23.     fun quickSort() {
  24.         recQuickSort(0, nElems - 1)
  25.     }
  26.  
  27.     fun recQuickSort(left: Int, right: Int) {
  28.         val size = right-left+1
  29.         if(size <= 3) manualSort(left, right)
  30.         else{
  31.             val median = medianOf3(left,right)
  32.             val partition = partitionIt(left,right,median)
  33.             recQuickSort(left, partition-1)
  34.             recQuickSort(partition+1, right)
  35.         }
  36.     }
  37.  
  38.     fun partitionIt(left: Int, right: Int, pivot: Long): Int {
  39.         var leftPtr = left
  40.         var rightPtr = right - 1
  41.         while (true) {
  42.             while (theArray[++leftPtr] < pivot){}
  43.             while (theArray[--rightPtr] > pivot){}
  44.             if (leftPtr >= rightPtr) break
  45.             else swap(leftPtr, rightPtr)
  46.         }
  47.         swap(leftPtr, right)
  48.         return leftPtr
  49.     }
  50.  
  51.     fun medianOf3(left: Int, right: Int):Long{
  52.         val center = (left+right)/2
  53.         if (theArray[left] > theArray[center]) swap(left,center)
  54.         if (theArray[left] > theArray[right]) swap(left,right)
  55.         if (theArray[center] > theArray[right]) swap(center,right)
  56.         swap(center,right-1)
  57.         return theArray[right-1]
  58.     }
  59.  
  60.     fun manualSort(left: Int,right: Int){
  61.         val size = right-left+1
  62.         if (size <= 1) return
  63.         if (size == 2){
  64.             if (theArray[left] > theArray[right]) swap(left,right)
  65.             return
  66.         }else{
  67.             if (theArray[left] > theArray[right-1]) swap(left,right-1)
  68.             if (theArray[left] > theArray[right]) swap(left, right)
  69.             if (theArray[right-1] > theArray[right]) swap(right-1,right)
  70.         }
  71.     }
  72.  
  73.     fun swap(dex1: Int, dex2: Int){
  74.         val temp = theArray[dex1]
  75.         theArray[dex1] = theArray[dex2]
  76.         theArray[dex2] = temp
  77.     }
  78. }
  79.  
  80. internal object QuickSort1App {
  81.     @JvmStatic fun main(args: Array<String>) {
  82.         val maxSize = 2
  83.         val arr: ArrayIns = ArrayIns(2)
  84.         for (j in 0..maxSize - 1) arr.insert((java.lang.Math.random() * 99).toInt().toLong())
  85.  
  86.         arr.display()
  87.         arr.quickSort()
  88.         arr.display()
  89.  
  90.         //TODO Решить вопрос с повторяющимися элементами
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement