Sorting in C++ - Bubble Sort

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Bubble Sort

Because of bubble sort's simplicity, it is one of the oldest sorts known to man. It based on the property of a sorted list that any two adjacent elements are in sorted order. In a typical iteration of bubble sort each adjacent pair of elements is compared, starting with the first two elements, then the second and the third elements, and all the way to the final two elements. Each time two elements are compared, if they are already in sorted order, nothing is done to them and the next pair of elements is compared. In the case where the two elements are not in sorted order, the two elements are swapped, putting them in order. Consider a set of data: 5 9 2 8 4 6 3. Bubble sort first compares the first two elements, the 5 and the 6. Because they are already in sorted order, nothing happens and the next pair of numbers, the 6 and the 2 are compared. Because they are not in sorted order, they are swapped and the data becomes: 5 2 9 8 4 6 3. To better understand the "bubbling" nature of the sort, watch how the largest number, 9, "bubbles" to the top in the first iteration of the sort. (5 9) 2 8 4 6 3 --> compare 5 and 9, no swap 5 (9 2) 8 4 6 3 --> compare 9 and 2, swap 5 2 (9 8) 4 6 3 --> compare 9 and 8, swap 5 2 8 (9 4) 6 3 --> compare 9 and 4, swap 5 2 8 4 (9 6) 3 --> compare 9 and 6, swap 5 2 8 4 6 (9 3) --> compare 9 and 3, swap 5 2 8 4 6 3 9 --> first iteration complete The bubble sort got its name because of the way the biggest elements "bubble" to the top. Notice that in the example above, the largest element, the 9, got swapped all the way into its correct position at the end of the list. As demonstrated, this happens because in each comparison, the larger element is always pushed towards its place at the end of the list. In the second iteration, the second-largest element will be bubbled up to its correct place in the same manner:

(5 2) 8 4 6 3 9 --> compare 5 and 2, swap 5 (2 8) 4 6 3 9 --> compare 2 and 8, no swap 5 2 (8 4) 6 3 9 --> compare 8 and 4, swap 5 2 4 (8 6) 3 9 --> compare 8 and 6, swap 5 2 4 6 (8 3) 9 --> compare 8 and 3, swap 5 2 4 6 3 8 9 --> no need to compare last two In the next pass through the list, the 6 would bubble up to its position, then the 5, the 4, the 3, and finally the 2. Here is a complete trace of the bubble sort algorithm on a ten element data set: 8935642170 8935642170 8395642170 8359642170 8356942170 8356492170 8356429170 8356421970 8356421790 8356421709 3856421709 3586421709 3568421709

3564821709 3564281709 3564218709 3564217809 3564217089 3564217089 3564217089 3546217089 3542617089 3542167089 3542167089 3542160789 3542160789 3452160789 3425160789 3421560789 3421560789 3421506789 3421506789 3241506789 3214506789 3214506789 3214056789 2314056789 2134056789 2134056789 2130456789 1230456789 1230456789 1203456789 1203456789 1023456789 0123456789

Sorting Terms
Comparison Function - A function which tells whether one element is greater than, less than, or equal to another. Note that this class of functions is not limited to numbers. For example, the C Standard Library function strcmp() uses a comparison function to compare strings. Sorting Algorithm - An algorithm, or series of steps, that takes an unsorted list and uses a comparison function to put the list into sorted order. Heap - A binary tree in which each node has a greater value than its children.

Bubble Sort Problems

Problem : Trace the operation of bubble sort on the following list: 4, 7, 2, 5, 6 Solution for Problem 1 >> The successive configurations of the list are as follows: 4, 7, 2, 5, 6 4, 7, 2, 5, 6 4, 2, 7, 5, 6 4, 2, 5, 7, 6 4, 2, 5, 6, 7 2, 4, 5, 6, 7

The starting configuration

4 < 7, so no swap is necessary

Swap the 2 and the 7 because 7 > 2

Swap the 5 and the 7 becuase 7 > 5

Swap the 6 and the 7 because 7 > 6. This is the end of the first pass. Notice how the 7 has "bubbled" up to the top of the list. Swap the two and the 4 because 4 > 2. The list is now in sorted order; the algorithm will detect this on the next pass through the list when it makes no swaps.

Problem : What is the worst case scenario for bubble sort, and why? Solution for Problem 2 >> The worst situation for bubble sort is when the list's smallest element is in the last position. In this situation, the smallest element will move down one place on each pass through the list, meaning that the sort will need to make the maximum number of passes through the list, namely n - 1. Problem : What simple modification could be made to the bubble sort algorithm that would make it perform more efficiently on the case described above? Solution for Problem 3 >> The algorithm could be made to start at the end of the list and move towards the front, swapping elements only if the first is greater than the second. In this way, the smallest numbers would bubble down instead of the large numbers bubbling up. Note however that this algorithm has its own worst case scenario that is just as bad as the worst case for the normal bubble sort. It occurs when the list's largest element is in the first position. Problem : What would happen if bubble sort didn't keep track of the number of swaps made on each pass through the list? Solution for Problem 4 >> The algorithm wouldn't know when to terminate as it would have no way of knowing when the list was in sorted order.

You might also like