forked from kennyledet/Algorithm-Implementations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.lua
More file actions
40 lines (37 loc) · 1.3 KB
/
Copy pathquick_sort.lua
File metadata and controls
40 lines (37 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
-- (In-place) Quicksort implementation
-- See : http://en.wikipedia.org/wiki/Quicksort#In-place_version
-- Partitions a portion of a list
local function partition(list, comp, left, right, pivot_index)
local pivot_value = list[pivot_index]
list[pivot_index], list[right] = list[right], list[pivot_index]
local store_index = left
for i = left, right-1 do
if comp(list[i], pivot_value) then
list[i], list[store_index] = list[store_index], list[i]
store_index = store_index + 1
end
end
list[store_index], list[right] = list[right], list[store_index]
return store_index
end
-- Performs in-place Quicksort
local function quicksort(list, comp, left, right)
left, right = left or 1, right or #list
if left < right then
local pivot_index = math.random(left, right)
local new_pivot_index = partition(list, comp, left, right, pivot_index)
quicksort(list, comp, left, new_pivot_index - 1)
quicksort(list, comp, new_pivot_index + 1, right)
end
return list
end
-- Quicksort function wrapper, to shadow access
-- to left and right bounds
-- list: a list to be ordered, in-place
-- comp: (optional) a comparison function
-- defaults to function(a, b) return a < b end
-- returns: the passed-in list, sorted
return function (list, comp)
comp = comp or function(a, b) return a < b end
return quicksort(list, comp)
end