Bubsort Function

Recommended Posts

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

If someone wants copy/modify it.

Free to make improvements.

```;===============================================================================
;
; 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```

edit: avoiding the spam, I deleted the email.

Edited by Josbe

Share on other sites

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```

Share on other sites

Nice. Thank you I've been looking for this sort of thing.

[size="10"]Pure Au3 crypt funcs(I'm currently also working on making a dll from this)[/size][Y] Be more active in the community[Y] Get 200 posts[N] Get 300 posts[N] Make a Topic in the example scripts forum with at least 50 replies.People who currently hate me:ValikSmOke_N

Share on other sites

Just tested. Nice work!

When the words fail... music speaks

Share on other sites

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

Register a new account