September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,15 @@
fun mergeSort(m):
return m if m.size <= 1
let middle = m.size / 2
let left = merge(m[:middle])
let right = merge(m[middle:]);
fun merge(left, right):
let result = []
while |left or right|.!empty:
result ++ (left.shift() if |left <= right|.first ...
else right.shift())
result ++ left ++ right
let arr = [7, 6, 5, 9, 8, 4, 3, 1, 2, 0]
print mergeSort arr

View file

@ -0,0 +1,63 @@
#NoEnv
Test := []
Loop 100 {
Random n, 0, 999
Test.Insert(n)
}
Result := MergeSort(Test)
Loop % Result.MaxIndex() {
MsgBox, 1, , % Result[A_Index]
IfMsgBox Cancel
Break
}
Return
/*
Function MergeSort
Sorts an array by first recursively splitting it down to its
individual elements and then merging those elements in their
correct order.
Parameters
Array The array to be sorted
Returns
The sorted array
*/
MergeSort(Array)
{
; Return single element arrays
If (! Array.HasKey(2))
Return Array
; Split array into Left and Right halfs
Left := [], Right := [], Middle := Array.MaxIndex() // 2
Loop % Middle
Right.Insert(Array.Remove(Middle-- + 1)), Left.Insert(Array.Remove(1))
If (Array.MaxIndex())
Right.Insert(Array.Remove(1))
Left := MergeSort(Left), Right := MergeSort(Right)
; If all the Right values are greater than all the
; Left values, just append Right at the end of Left.
If (Left[Left.MaxIndex()] <= Right[1]) {
Loop % Right.MaxIndex()
Left.Insert(Right.Remove(1))
Return Left
}
; Loop until one of the arrays is empty
While(Left.MaxIndex() and Right.MaxIndex())
Left[1] <= Right[1] ? Array.Insert(Left.Remove(1))
: Array.Insert(Right.Remove(1))
Loop % Left.MaxIndex()
Array.Insert(Left.Remove(1))
Loop % Right.MaxIndex()
Array.Insert(Right.Remove(1))
Return Array
}