Skip to content

Instantly share code, notes, and snippets.

@cnevinc
Created October 15, 2015 03:58
Show Gist options
  • Save cnevinc/83e15bd60e3c05f20877 to your computer and use it in GitHub Desktop.
Save cnevinc/83e15bd60e3c05f20877 to your computer and use it in GitHub Desktop.
// ============== Quick sort start 20151015==============
fun quickSort(a: IntArray, lowOffset: Int, highOffset: Int) {
if (lowOffset > highOffset ) return;
// find pivot
var pivot: Int = a[highOffset]
var nextSmallTail = lowOffset;
// partition
for ( i in lowOffset..highOffset-1){
if ( a[i] <= pivot ){
swap(a,i,nextSmallTail)
nextSmallTail++
}
}
swap(a,nextSmallTail,highOffset)
pivot = nextSmallTail
quickSort(a,lowOffset, pivot-1)
quickSort(a,pivot+1,highOffset)
}
// ============== Quick sort end 20151015==============
fun main(args: Array<String>) {
var array = intArrayOf( 6,5,4,3,2,1)
array.forEach { i -> print(i) }
println("")
quickSort(array,0,array.size()-1)
array.forEach { i -> print(i) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment