Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
33
Task/Quickselect-algorithm/Lua/quickselect-algorithm.lua
Normal file
33
Task/Quickselect-algorithm/Lua/quickselect-algorithm.lua
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
function partition (list, left, right, pivotIndex)
|
||||
local pivotValue = list[pivotIndex]
|
||||
list[pivotIndex], list[right] = list[right], list[pivotIndex]
|
||||
local storeIndex = left
|
||||
for i = left, right do
|
||||
if list[i] < pivotValue then
|
||||
list[storeIndex], list[i] = list[i], list[storeIndex]
|
||||
storeIndex = storeIndex + 1
|
||||
end
|
||||
end
|
||||
list[right], list[storeIndex] = list[storeIndex], list[right]
|
||||
return storeIndex
|
||||
end
|
||||
|
||||
function quickSelect (list, left, right, n)
|
||||
local pivotIndex
|
||||
while 1 do
|
||||
if left == right then return list[left] end
|
||||
pivotIndex = math.random(left, right)
|
||||
pivotIndex = partition(list, left, right, pivotIndex)
|
||||
if n == pivotIndex then
|
||||
return list[n]
|
||||
elseif n < pivotIndex then
|
||||
right = pivotIndex - 1
|
||||
else
|
||||
left = pivotIndex + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
math.randomseed(os.time())
|
||||
local vec = {9, 8, 7, 6, 5, 0, 1, 2, 3, 4}
|
||||
for i = 1, 10 do print(i, quickSelect(vec, 1, #vec, i) .. " ") end
|
||||
Loading…
Add table
Add a link
Reference in a new issue