Created
February 11, 2014 02:45
-
-
Save error454/8928424 to your computer and use it in GitHub Desktop.
Stonescript Quicksort
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
-- Adapted from http://rosettacode.org/wiki/Sorting_algorithms/Quicksort#Lua | |
function quicksort (t, start, endi) | |
start = start or 0 | |
endi = endi or table.getSize ( t ) - 1 | |
if(endi - start < 1) then return t end | |
local pivot = start | |
for i = start + 1, endi do | |
if table.getAt ( t, i ) <= table.getAt ( t, pivot ) then | |
local temp = table.getAt ( t, pivot + 1 ) | |
table.setAt ( t, pivot + 1, table.getAt ( t, pivot ) ) | |
if(i == pivot + 1) then | |
table.setAt ( t, pivot, temp ) | |
else | |
table.setAt ( t, pivot, table.getAt ( t, i ) ) | |
table.setAt ( t, i, temp ) | |
end | |
pivot = pivot + 1 | |
end | |
end | |
t = quicksort(t, start, pivot - 1) | |
return quicksort(t, pivot + 1, endi) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment