Sorting Array – Bubble Sort

Article Tools

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

image

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
Previous postSort Arrays - Merge Sort Next postFTP your Scripts

Related Posts

Post Your Comment

You must be logged in to post a comment.