Josbe Posted March 2, 2004 Posted March 2, 2004 (edited) 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. expandcollapse popup;=============================================================================== ; ; 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 EndFuncSuggestions / Comments?edit: avoiding the spam, I deleted the email. Edited September 9, 2008 by Josbe AUTOIT > AutoIt docs / Beta folder - AutoIt latest beta
Josbe Posted March 2, 2004 Author Posted March 2, 2004 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 AUTOIT > AutoIt docs / Beta folder - AutoIt latest beta
mmavipc Posted July 15, 2008 Posted July 15, 2008 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
wraithdu Posted July 16, 2008 Posted July 16, 2008 (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 July 16, 2008 by wraithdu
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now