Bubble Sort Algorithm:
Is a simple sorting algorithm. It works by repeatedly stepping through the array to be sorted, comparing two items at a time, swapping these two items if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which means the array is sorted.
Bubble sort is asymptotically equivalent in running time to insertion sort, but the two algorithms differ greatly in the number of swaps necessary.
;=============================================================================== ; ; Function Name: _BubbleSort() ; Description: Sort a 1 dimensional Array of numbers on a specific index ; using the bubblesort algorithm. ; Parameter(s): $avArray - Array ; $iReverse - Sort ascending when 0 / Sort descending when 1 ; $iStart - Start sorting at this Array entry. ; $iEnd - End sorting at this Array entry. Default UBound($avArray) - 1 ; ; Requirement(s): None ; Return Value(s): On Success - 1 and the sorted array is set ; On Failure - 0 and sets @ERROR = 1 when $avArray is not an array ;=============================================================================== #include <Array.au3> Dim $Array[10] $Array[0] = 500 $Array[1] = 19 $Array[2] = 2 $Array[3] = 7 $Array[4] = 1 $Array[5] = 21 $Array[6] = 17 $Array[7] = 23 $Array[8] = 46 $Array[9] = 3 _ArrayDisplay($Array,"UnSorted" ) _BubbleSort($Array) _ArrayDisplay($Array,"Sort Ascending" ) _BubbleSort($Array,1) _ArrayDisplay($Array,"Sort Decending" ) Func _BubbleSort($avArray, $iReverse = 0, $iStart = 0, $iEnd = 0) If Not IsArray($avArray) Then SetError(1) ; Isn't an array. Return 0 EndIf If $iEnd < 1 Or $iEnd > UBound($avArray) - 1 Then $iEnd = UBound($avArray) - 1 If $iStart < 0 Or $iStart > UBound($avArray) - 1 Then $iStart = 0 If $iReverse <> 1 Then $iReverse = 0 While $iEnd > $iStart For $I = $iStart To $iEnd - 1 If $iReverse = 1 Then If $avArray[$I] < $avArray[$I + 1] Then _ArraySwap($avArray[$I], $avArray[$I + 1]) ElseIf $iReverse = 0 Then If $avArray[$I] > $avArray[$I + 1] Then _ArraySwap($avArray[$I], $avArray[$I + 1]) EndIf Next $iEnd = $iEnd - 1 Wend Return 1 EndFunc
Edited by buzz44, 13 May 2010 - 12:41 PM.





