Sorting numeric arrays?
Using built-in sorting functions it can take ... no time.
'---Globals
DIM MaxCount AS LONG value 100000
dim MyArray(MaxCount) as long
dim Count as long
dim T1, T2 as double
DIM Message AS STRING
'---Ask if execute or not
Message = "This program will fill an array of " & MaxCount & " elements with random LONG numbers.\n"
Message += "Array will be sorted twice in order to test sorting of sparse and already sorted array.\n\n"
Message += "Please press Yes to go on, NO to Stop\n"
DIM lResult AS LONG = MSGBOX(0, Message, %MB_YESNO, "Continue?")
IF lResult <> %IDYES THEN
STOP
END IF
'---Speed up operations a bit
doevents(off)
'---Init random number generator
randomize
'---Fill array with random numbers
T1 = GetTickCount
for Count = LBound(MyArray) To UBound(MyArray)
MyArray(Count) = rnd(-1000000000, 1000000000)
next
T2 = GetTickCount
'---Do the job
msgbox 0, _
"Time to fill randomly an array of " & ubound(MyArray) & " elements:" & $tab & format$(T2-T1) & " mSecs" & $crlf & _
"Time to sort array " & repeat$(5, $tab) & testsort(MyArray) & " mSecs" & $crlf & _
"Time to re-sort sorted array " & repeat$(4, $tab) & testsort(MyArray) & " mSecs" & $crlf & _
"Time to re-sort sorted array in descending order " & repeat$(2, $tab) & testsort(MyArray, %TRUE) & " mSecs" & $crlf & _
""
'----------------------------------------------------------------------------
'---In case you want to see some output values, just uncomment those lines
'----------------------------------------------------------------------------
'uses "console"
'for count = 1 to 80
' console_write MyArray(Count) & ", "
'next
'console_waitkey
'----------------------------------------------------------------------------
'---Sorting function
'----------------------------------------------------------------------------
Function TestSort(byref v() as long, optional DeScendSorting as long) as long
'---Time measuring vars
local ticka, tickb as double
'---Start timer
ticka = GetTickCount
'---Sort array passed by reference depending on sort order
if DeScendSorting = %TRUE then
array sort v, descend
else
array sort v
end if
'---ENd timer
tickb = GetTickCount
function = tickb - ticka
End Function
Bookmarks