Created
July 21, 2014 12:15
-
-
Save jonathanmarvens/10a0ed30d730c71d058e 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
def quickSort(xs: Array[Int]) { | |
def sort1(l: Int, r: Int) { | |
val pivot = xs((l + r) / 2) | |
var i = l; | |
var j = r; | |
while (i <= j) { | |
while (xs(i) < pivot) { | |
i += 1 | |
} | |
while (xs(j) > pivot) { | |
j -= 1 | |
} | |
if (i <= j) { | |
swap(i, j) | |
i += 1 | |
j -= 1 | |
} | |
} | |
if (l < j) { | |
sort1(l, j) | |
} | |
if (j < r) { | |
sort1(i, r) | |
} | |
} | |
def swap(i: Int, j: Int) { | |
val t = xs(i) | |
xs(i) = xs(j) | |
xs(j) = t | |
} | |
sort1(0, (xs.length - 1)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment