Sorting Array – Bubble Sort
Posted by admin - Apr 2, 2008 Arrays, Articles, QTips 0 0 Views : 462 Receive Updates For This Category
Article Tools
- Print this page
- Add Comment
- Send to Friend
- Last Updated on :
Jul 16, 2011
Bubble sort is a simple sorting algorithm. It works by repeatedly stepping through the list to be sorted, comparing two items at a time and swapping them if they are in the wrong order.
The pass through the list is repeated until no swaps are needed, which means the list is sorted. The algorithm gets its name from the way smaller elements “bubble” to the top (i.e. the beginning) of the list via the swaps. (Another opinion: it gets its name from the way greater elements “bubble” to the end.)
Because it only uses comparisons to operate on elements, it is a comparison sort.
This is the easiest comparison sort to implement.
Bubble Sort Animation
Bubble Sort efficiency
Public Sub bubbleSort( ByRef arr() )
Dim i, j, tmp
Services.StartTransaction "BubbleSort"
For i = ( UBound( arr ) - 1 ) to 0 Step -1
For j = 0 to i
If arr( j ) > arr( j + 1 ) Then
tmp = arr( j + 1 )
arr( j + 1 ) = arr( j )
arr( j ) = tmp
End If
Next
Next
Services.EndTransaction "BubbleSort"
End Sub


