Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
146 views

Algorithm 12: Sorting A List in Ascending Order Using The Bubble Sort Algorithm

The bubble sort algorithm sorts an unsorted list of numbers in ascending order by repeatedly swapping adjacent elements that are in the wrong order. It does this by making multiple passes through the list, comparing adjacent elements and swapping them if they are in the wrong order until no swaps are needed, indicating the list is fully sorted. The algorithm uses variables like an array to store the numbers, indexes to track positions, and a temporary variable to swap elements as it makes multiple passes through the list until it is fully sorted.

Uploaded by

Agrippa Mungazi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
146 views

Algorithm 12: Sorting A List in Ascending Order Using The Bubble Sort Algorithm

The bubble sort algorithm sorts an unsorted list of numbers in ascending order by repeatedly swapping adjacent elements that are in the wrong order. It does this by making multiple passes through the list, comparing adjacent elements and swapping them if they are in the wrong order until no swaps are needed, indicating the list is fully sorted. The algorithm uses variables like an array to store the numbers, indexes to track positions, and a temporary variable to swap elements as it makes multiple passes through the list until it is fully sorted.

Uploaded by

Agrippa Mungazi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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

The algorithm in pseudocode is


n = MaxIndex – 1
For pass = 0 to MaxIndex – 1
for j = 0 to n
if MyArray(j) > MyArray(j+1) then
temp = MyArray(j)
MyArray(j) = MyArray(j+1)
MyArray(j+1) = temp
End if
Next j
n=n–1 //this restricts the algorithm to deal only with array elements not in
//correct positions in the inner loop
Next pass

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.

You might also like