Algorithm 12: Sorting A List in Ascending Order Using The Bubble Sort Algorithm
Algorithm 12: Sorting A List in Ascending Order Using The Bubble Sort Algorithm
Bubble sort algorithm sorts a given unsorted list of items in ascending order.
The identifiers needed for the algorithm are listed below
Identifier Explanation
MyArray [0…7] One-dimension array to store seven (8) numbers
MaxIndex The last index number of array elements
n The number of elements to compare in each pass
pass Control variable for outer loop
j Control variable for inner loop
temp Variable for temporary storage while swapping array elements
Examine the following table. (Note that each pass represents the status of the array after the
completion of the inner for loop, except for pass 0, which represents the array as it was passed to
the function for sorting)
8 6 10 3 1 2 5 4 } pass 0
6 8 3 1 2 5 4 10 } pass 1
6 3 1 2 5 4 8 10 } pass 2
3 1 2 5 4 6 8 10 } pass 3
1 2 3 4 5 6 8 10 } pass 4
1 2 3 4 5 6 8 10 } pass 5
1 2 3 4 5 6 8 10 } pass 6
1 2 3 4 5 6 8 10 } pass 7
The above tabulated clearly depicts how each bubble sort works. Note that each pass results in one
number being bubbled to the end of the list.