Skip to content

Instantly share code, notes, and snippets.

@error454
Created February 11, 2014 02:45
Show Gist options
  • Save error454/8928424 to your computer and use it in GitHub Desktop.
Save error454/8928424 to your computer and use it in GitHub Desktop.
Stonescript Quicksort
-- 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