Created
March 24, 2015 14:21
-
-
Save volyx/3cfc6116f2145fe56e15 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
def a = [1,5,7,8,45,7,45,34,45,78,9] | |
k = 10 | |
k0 = 0 | |
void printArr(a) { | |
a.each{i -> | |
print i + ', ' | |
} | |
} | |
void sort(a) { | |
sort(a, 0, a.size() - 1) | |
} | |
void sort(a, s, e) { | |
int index = partition(a, s, e) | |
printArr(a); | |
k0++ | |
if (k0 == k) return | |
if (s < index -1 ) | |
sort(a, s, index -1) | |
if (index < e) | |
sort(a, index, e) | |
} | |
def partition(a, s , e) { | |
int p = (s + e) / 2 | |
def i = s; | |
def j = e; | |
def pivot = a[p]; | |
println "pivot=" + pivot | |
while (i<=j) { | |
while (a[i] < pivot) { | |
i++; | |
} | |
while (a[j] > pivot) { | |
j-- | |
} | |
println "i=" + i + " j=" + j | |
def tmp; | |
if (i<=j) { | |
tmp = a[i] | |
a[i] = a[j] | |
a[j] = tmp | |
println "swap " + a[i] + " " + a[j] + " i j " + i + "-" + j | |
i++ | |
j-- | |
} | |
printArr(a); | |
} | |
println "index=" + i | |
return i | |
} | |
printArr(a); | |
println "" | |
sort(a) | |
println "" | |
printArr(a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment