Jump to content

Recommended Posts

Posted (edited)

I wrote the classic BubbleSort for AutoIT, arrange one o more numbers contained in elements array's.

If someone wants copy/modify it. B)

Free to make improvements. :angry::whistle:

;===============================================================================
;
; Description:      Evaluates an array(only) with numbers and it's arranged in ascendent or descendent mode.
; Parameter(s):    $arr - Array
;                            $ord_mode - Mode to order the numbers (0 - ascendant, Non-zero for descendent)
; Requirement(s):   None
; Return Value(s):  The same array ordered ascendant or descendent according to specified mode.
;                               If isn't array, this set @error to 1 and returns the input.
; Author(s):       "Josbe" <...>
; Note(s):          - Works with floats as well as integers
;                         - Non-numerics characters, are treated as 0
;
;===============================================================================
Func _BubSortNumber( $arr, $ord_mode )
Local $temp_data = 0
Local $cnt1, $cnt2, $ord_mode, $arr

If IsArray($arr)  then
   For $cnt1 = 0 to Ubound($arr)
     For $cnt2 = 0 to UBound($arr)-2
          If $ord_mode = 0 then
               If ( Number( $arr[$cnt2] ) > Number( $arr[$cnt2+1] ) ) then
                       $temp_data = $arr[ $cnt2 ]
                       $arr[$cnt2]= $arr[$cnt2 + 1]
                       $arr[$cnt2 + 1] = $temp_data
                EndIf
          Else
               If ( Number( $arr[$cnt2] ) < Number( $arr[$cnt2+1]) ) then
                       $temp_data = $arr[ $cnt2 ]
                       $arr[$cnt2]= $arr[$cnt2 + 1]
                       $arr[$cnt2 + 1] = $temp_data
                EndIf
          EndIf
     Next
   Next
Else
  SetError(1)
EndIf
   Return $arr
EndFunc

Suggestions / Comments?

edit: avoiding the spam, I deleted the email.

Edited by Josbe
Posted

A little example using this function:

This show 15 numbers without order (surely), and it's ordered.

#include "_BubSortNums.au3"

Dim $myarr[15]
$nums= ""

For $i=0 to Ubound($myarr)-1
    $myarr[$i]= Random(1,50)
    $nums= $nums & $myarr[$i] & @LF
Next

MsgBox(0, "Disordered numbers (according to Random output)", $nums)

;; Ascendant mode
$myarr2= _BubSortNumber($myarr, 0)

If @error = 0 then
  $nums= ""
  For $i=0 to UBound($myarr2)-1
     $nums= $nums & $myarr2[$i] & @LF
  Next
Endif

MsgBox(0, "Ordered numbers (according to _BubSNumber)", $nums)
exit

:whistle:

  • 4 years later...
Posted (edited)

Nice. Maybe change this to a ByRef function instead of allocating more memory for a temporary array? That should be better for large arrays, and would eliminate the need for a Return array.

EDIT -

This is inherently a slow function, so it should be noted it shouldn't be used on very large arrays. Example with a 720 element array -

_BubSortNumber = 4.80336851471346 sec

_ArraySort = 0.0628780333813376 sec

Also note that _ArraySort sorted this array as strings, so got the sorting order a bit wrong, while yours correctly sorted the array as numbers.

Edited by wraithdu

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...