Created
December 29, 2016 06:06
-
-
Save vkorobkov/27d94cd45c69f1032d7fcb7fe0540dc9 to your computer and use it in GitHub Desktop.
Easy quick sort function writtern in Groovy
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(List list, beg = 0, end = list.size() - 1) { | |
if (list.size() < 2) { | |
return | |
} | |
def beg0 = beg | |
def end0 = end | |
def marker = list[beg] | |
while(true) { | |
while (list[beg] < marker) beg++ | |
while (list[end] > marker) end-- | |
if (beg < end) { | |
def t = list[beg] | |
list[beg] = list[end] | |
list[end] = t | |
beg++ | |
end-- | |
} | |
else { | |
break | |
} | |
} | |
if (beg0 < end) quickSort(list, beg0, end) | |
if (end + 1 < end0) quickSort(list, end + 1, end0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment