June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -0,0 +1,2 @@
|
|||
v = [9, 8, 7, 6, 5, 0, 1, 2, 3, 4]
|
||||
@show v select(v, 1:10)
|
||||
36
Task/Quickselect-algorithm/Maple/quickselect-algorithm.maple
Normal file
36
Task/Quickselect-algorithm/Maple/quickselect-algorithm.maple
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
part := proc(arr, left, right, pivot)
|
||||
local val,safe,i:
|
||||
val := arr[pivot]:
|
||||
arr[pivot], arr[right] := arr[right], arr[pivot]:
|
||||
safe := left:
|
||||
for i from left to right do
|
||||
if arr[i] < val then
|
||||
arr[safe], arr[i] := arr[i], arr[safe]:
|
||||
safe := safe + 1:
|
||||
end if:
|
||||
end do:
|
||||
arr[right], arr[safe] := arr[safe], arr[right]:
|
||||
return safe:
|
||||
end proc:
|
||||
|
||||
quickselect := proc(arr,k)
|
||||
local pivot,left,right:
|
||||
left,right := 1,numelems(arr):
|
||||
while(true)do
|
||||
if left = right then return arr[left]: end if:
|
||||
pivot := trunc((left+right)/2);
|
||||
pivot := part(arr, left, right, pivot):
|
||||
if k = pivot then
|
||||
return arr[k]:
|
||||
elif k < pivot then
|
||||
right := pivot-1:
|
||||
else
|
||||
left := pivot+1:
|
||||
end if:
|
||||
end do:
|
||||
end proc:
|
||||
roll := rand(1..20):
|
||||
demo := Array([seq(roll(), i=1..20)]);
|
||||
map(x->printf("%d ", x), demo):
|
||||
print(quickselect(demo,7)):
|
||||
print(quickselect(demo,14)):
|
||||
|
|
@ -2,19 +2,19 @@ aList = [9, 8, 7, 6, 5, 0, 1, 2, 3, 4]
|
|||
see partition(aList, 9, 4, 2) + nl
|
||||
|
||||
func partition list, left, right, pivotIndex
|
||||
pivotValue = list[pivotIndex]
|
||||
temp = list[pivotIndex]
|
||||
list[pivotIndex] = list[right]
|
||||
list[right] = temp
|
||||
storeIndex = left
|
||||
for i = left to right-1
|
||||
if list[i] < pivotValue
|
||||
temp = list[storeIndex]
|
||||
list[storeIndex] = list[i]
|
||||
list[i] = temp
|
||||
storeIndex++ ok
|
||||
temp = list[right]
|
||||
list[right] = list[storeIndex]
|
||||
list[storeIndex] = temp
|
||||
next
|
||||
return storeIndex
|
||||
pivotValue = list[pivotIndex]
|
||||
temp = list[pivotIndex]
|
||||
list[pivotIndex] = list[right]
|
||||
list[right] = temp
|
||||
storeIndex = left
|
||||
for i = left to right-1
|
||||
if list[i] < pivotValue
|
||||
temp = list[storeIndex]
|
||||
list[storeIndex] = list[i]
|
||||
list[i] = temp
|
||||
storeIndex++ ok
|
||||
temp = list[right]
|
||||
list[right] = list[storeIndex]
|
||||
list[storeIndex] = temp
|
||||
next
|
||||
return storeIndex
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue