Created
October 15, 2015 03:58
-
-
Save cnevinc/83e15bd60e3c05f20877 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// ============== 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