Results 1 to 10 of 13

Thread: Sorting arrays with built-in sorting functions

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,818
    Rep Power
    10

    Sorting arrays with built-in sorting functions

    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
    
    Last edited by ErosOlmi; 21-02-2024 at 21:35.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

Similar Threads

  1. 3d arrays?
    By JosephE in forum thinDebug
    Replies: 2
    Last Post: 08-08-2010, 23:21
  2. Arrays
    By Charles Pegge in forum O2h Compiler
    Replies: 0
    Last Post: 17-03-2009, 16:09

Members who have read this thread: 7

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •