-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.lua
More file actions
35 lines (29 loc) · 847 Bytes
/
Copy pathQuickSort.lua
File metadata and controls
35 lines (29 loc) · 847 Bytes
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
-----------------------------------------------
-- [FILE] quickSort.lua
-- [DATE] 2016-07-04
-- [CODE] BY gaofeng
-- [MARK] NONE
-----------------------------------------------
local function _quickSort(tmp, arr, low, high, sortFunc)
if low >= high then return end
local value = arr[low];
local j = low;
for i = low + 1, high do
if(sortFunc(arr[i], value))then
-- print("---", arr[i], arr[j], j, value);
j = j + 1
swap(arr, i, j)
end
end
local mid = j
swap(arr, low, j)
_quickSort(tmp, arr, low, mid - 1, sortFunc)
_quickSort(tmp, arr, mid + 1, high, sortFunc)
end
local function quickSort(arr, sortFunc)
sortFunc = sortFunc or defaultSortFunc
local tmp = {}
local len = #arr
_quickSort(tmp, arr, 1, len, sortFunc)
end
return quickSort