Last active
March 19, 2020 06:52
-
-
Save rezakhmf/520d59b87cad61ea9bc333b061ab83cf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun rezaQuickSort(array: IntArray, left: Int, right: Int) { | |
val pIndex = rezaPartiion(array, left, right) | |
if(left < pIndex - 1) { | |
rezaQuickSort(array, left, pIndex - 1) | |
} | |
if(pIndex < right) { | |
rezaQuickSort(array, pIndex , right) | |
} | |
} | |
fun rezaPartiion(array: IntArray, left: Int, right: Int ): Int { | |
var pivot = array[(right+left)/2] | |
var leftIndex = left | |
var rightIndex = right | |
while (leftIndex <= rightIndex) { | |
while (array[leftIndex] < pivot) { | |
leftIndex++ | |
} | |
while (array[rightIndex] > pivot) { | |
rightIndex-- | |
} | |
if(leftIndex <= rightIndex ) { | |
rezaExchange(array, leftIndex, rightIndex) | |
leftIndex++ | |
rightIndex-- | |
} | |
} | |
rezaExchange(array, leftIndex, right) | |
return leftIndex | |
} | |
fun rezaExchange(array: IntArray, left: Int, right: Int) { | |
val temp = array[left] | |
array[left] = array[right] | |
array[right] = temp | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment