September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
1
Task/Quickselect-algorithm/00META.yaml
Normal file
1
Task/Quickselect-algorithm/00META.yaml
Normal file
|
|
@ -0,0 +1 @@
|
|||
--- {}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
def quickselect(a, k)
|
||||
arr = a.dup # we will be modifying it
|
||||
loop do
|
||||
pivot = arr.delete_at(rand(arr.size))
|
||||
left, right = arr.partition { |x| x < pivot }
|
||||
if k == left.size
|
||||
return pivot
|
||||
elsif k < left.size
|
||||
arr = left
|
||||
else
|
||||
k = k - left.size - 1
|
||||
arr = right
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
v = [9, 8, 7, 6, 5, 0, 1, 2, 3, 4]
|
||||
p v.each_index.map { |i| quickselect(v, i) }.to_a
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
USING: combinators kernel make math locals prettyprint sequences ;
|
||||
IN: rosetta-code.quickselect
|
||||
|
||||
:: quickselect ( k seq -- n )
|
||||
seq unclip :> ( xs x )
|
||||
xs [ x < ] partition :> ( ys zs )
|
||||
ys length :> l
|
||||
{
|
||||
{ [ k l < ] [ k ys quickselect ] }
|
||||
{ [ k l > ] [ k l - 1 - zs quickselect ] }
|
||||
[ x ]
|
||||
} cond ;
|
||||
|
||||
: quickselect-demo ( -- )
|
||||
{ 9 8 7 6 5 0 1 2 3 4 } dup length <iota> swap
|
||||
[ [ quickselect , ] curry each ] { } make . ;
|
||||
|
||||
MAIN: quickselect-demo
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
Procedure QuickPartition (Array L(1), left, right, pivotIndex)
|
||||
pivotValue = L(pivotIndex)
|
||||
Swap L(pivotIndex) , L(right); Move pivot To End
|
||||
storeIndex = left
|
||||
For i=left To right-1
|
||||
If L(i) < pivotValue
|
||||
Swap L(storeIndex),L(i)
|
||||
storeIndex+1
|
||||
EndIf
|
||||
Next i
|
||||
Swap L(right), L(storeIndex) ; Move pivot To its final place
|
||||
ProcedureReturn storeIndex
|
||||
EndProcedure
|
||||
Procedure QuickSelect(Array L(1), left, right, k)
|
||||
Repeat
|
||||
If left = right:ProcedureReturn L(left):EndIf
|
||||
pivotIndex.i= left; Select pivotIndex between left And right
|
||||
pivotIndex= QuickPartition(L(), left, right, pivotIndex)
|
||||
If k = pivotIndex
|
||||
ProcedureReturn L(k)
|
||||
ElseIf k < pivotIndex
|
||||
right= pivotIndex - 1
|
||||
Else
|
||||
left= pivotIndex + 1
|
||||
EndIf
|
||||
ForEver
|
||||
EndProcedure
|
||||
Dim L.i(9)
|
||||
For i=0 To 9
|
||||
Read L(i)
|
||||
Next i
|
||||
DataSection
|
||||
Data.i 9, 8, 7, 6, 5, 0, 1, 2, 3, 4
|
||||
EndDataSection
|
||||
For i=0 To 9
|
||||
Debug QuickSelect(L(),0,9,i)
|
||||
Next i
|
||||
58
Task/Quickselect-algorithm/Python/quickselect-algorithm-2.py
Normal file
58
Task/Quickselect-algorithm/Python/quickselect-algorithm-2.py
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
'''Quick select'''
|
||||
|
||||
from functools import reduce
|
||||
|
||||
|
||||
# quickselect :: Ord a => Int -> [a] -> a
|
||||
def quickSelect(k):
|
||||
'''The kth smallest element
|
||||
in the unordered list xs.'''
|
||||
def go(k, xs):
|
||||
x = xs[0]
|
||||
|
||||
def ltx(y):
|
||||
return y < x
|
||||
ys, zs = partition(ltx)(xs[1:])
|
||||
n = len(ys)
|
||||
return go(k, ys) if k < n else (
|
||||
go(k - n - 1, zs) if k > n else x
|
||||
)
|
||||
return lambda xs: go(k, xs) if xs else None
|
||||
|
||||
|
||||
# TEST ----------------------------------------------------
|
||||
# main :: IO ()
|
||||
def main():
|
||||
'''Test'''
|
||||
|
||||
v = [9, 8, 7, 6, 5, 0, 1, 2, 3, 4]
|
||||
print(list(map(
|
||||
flip(quickSelect)(v),
|
||||
range(0, len(v))
|
||||
)))
|
||||
|
||||
|
||||
# GENERIC -------------------------------------------------
|
||||
|
||||
|
||||
# flip :: (a -> b -> c) -> b -> a -> c
|
||||
def flip(f):
|
||||
'''The (curried) function f with its
|
||||
arguments reversed.'''
|
||||
return lambda a: lambda b: f(b)(a)
|
||||
|
||||
|
||||
# partition :: (a -> Bool) -> [a] -> ([a], [a])
|
||||
def partition(p):
|
||||
'''The pair of lists of those elements in xs
|
||||
which respectively do, and don't
|
||||
satisfy the predicate p.'''
|
||||
def go(a, x):
|
||||
ts, fs = a
|
||||
return (ts + [x], fs) if p(x) else (ts, fs + [x])
|
||||
return lambda xs: reduce(go, xs, ([], []))
|
||||
|
||||
|
||||
# MAIN ---
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
41
Task/Quickselect-algorithm/VBA/quickselect-algorithm.vba
Normal file
41
Task/Quickselect-algorithm/VBA/quickselect-algorithm.vba
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
Dim s As Variant
|
||||
Private Function quick_select(ByRef s As Variant, k As Integer) As Integer
|
||||
Dim left As Integer, right As Integer, pos As Integer
|
||||
Dim pivotValue As Integer, tmp As Integer
|
||||
left = 1: right = UBound(s)
|
||||
Do While left < right
|
||||
pivotValue = s(k)
|
||||
tmp = s(k)
|
||||
s(k) = s(right)
|
||||
s(right) = tmp
|
||||
pos = left
|
||||
For i = left To right
|
||||
If s(i) < pivotValue Then
|
||||
tmp = s(i)
|
||||
s(i) = s(pos)
|
||||
s(pos) = tmp
|
||||
pos = pos + 1
|
||||
End If
|
||||
Next i
|
||||
tmp = s(right)
|
||||
s(right) = s(pos)
|
||||
s(pos) = tmp
|
||||
If pos = k Then
|
||||
Exit Do
|
||||
End If
|
||||
If pos < k Then
|
||||
left = pos + 1
|
||||
Else
|
||||
right = pos - 1
|
||||
End If
|
||||
Loop
|
||||
quick_select = s(k)
|
||||
End Function
|
||||
Public Sub main()
|
||||
Dim r As Integer, i As Integer
|
||||
s = [{9, 8, 7, 6, 5, 0, 1, 2, 3, 4}]
|
||||
For i = 1 To 10
|
||||
r = quick_select(s, i) 's is ByRef parameter
|
||||
Debug.Print IIf(i < 10, r & ", ", "" & r);
|
||||
Next i
|
||||
End Sub
|
||||
Loading…
Add table
Add a link
Reference in a new issue