all tasks

This commit is contained in:
Ingy döt Net 2013-04-11 01:07:29 -07:00
parent b83f433714
commit 68f8f3e56b
14735 changed files with 178959 additions and 0 deletions

View file

@ -0,0 +1,41 @@
MODE DATA = CHAR;
PROC merge sort = ([]DATA m)[]DATA: (
IF LWB m >= UPB m THEN
m
ELSE
INT middle = ( UPB m + LWB m ) OVER 2;
[]DATA left = merge sort(m[:middle]);
[]DATA right = merge sort(m[middle+1:]);
flex merge(left, right)[AT LWB m]
FI
);
# FLEX version: A demonstration of FLEX for manipulating arrays #
PROC flex merge = ([]DATA in left, in right)[]DATA:(
[UPB in left + UPB in right]DATA result;
FLEX[0]DATA left := in left;
FLEX[0]DATA right := in right;
FOR index TO UPB result DO
# change the direction of this comparison to change the direction of the sort #
IF LWB right > UPB right THEN
result[index:] := left;
stop iteration
ELIF LWB left > UPB left THEN
result[index:] := right;
stop iteration
ELIF left[1] <= right[1] THEN
result[index] := left[1];
left := left[2:]
ELSE
result[index] := right[1];
right := right[2:]
FI
OD;
stop iteration:
result
);
[32]CHAR char array data := "big fjords vex quick waltz nymph";
print((merge sort(char array data), new line));

View file

@ -0,0 +1,38 @@
PROC opt merge sort = ([]REF DATA m)[]REF DATA: (
IF LWB m >= UPB m THEN
m
ELSE
INT middle = ( UPB m + LWB m ) OVER 2;
[]REF DATA left = opt merge sort(m[:middle]);
[]REF DATA right = opt merge sort(m[middle+1:]);
opt merge(left, right)[AT LWB m]
FI
);
PROC opt merge = ([]REF DATA left, right)[]REF DATA:(
[UPB left - LWB left + 1 + UPB right - LWB right + 1]REF DATA result;
INT index left:=LWB left, index right:=LWB right;
FOR index TO UPB result DO
# change the direction of this comparison to change the direction of the sort #
IF index right > UPB right THEN
result[index:] := left[index left:];
stop iteration
ELIF index left > UPB left THEN
result[index:] := right[index right:];
stop iteration
ELIF left[index left] <= right[index right] THEN
result[index] := left[index left]; index left +:= 1
ELSE
result[index] := right[index right]; index right +:= 1
FI
OD;
stop iteration:
result
);
# create an array of pointers to the data being sorted #
[UPB char array data]REF DATA data; FOR i TO UPB char array data DO data[i] := char array data[i] OD;
[]REF CHAR result = opt merge sort(data);
FOR i TO UPB result DO print((result[i])) OD; print(new line)

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
}