6.5 The Bubble SortA simple solution to the sorting problem is the bubble sort algorithm, which re-arranges the values by iterating over the sequence multiple times, causing larger values to bubble to the top or end of the list. To illustrate how the bubble sort algorithm works, suppose we have four playing cards that we want to order from smallest to largest face value. We start by laying the cards out face up on a table as shown here: The algorithm requires multiple passes over the cards, with each pass starting at the first card and ending one card earlier than on the previous iteration. During each pass, the cards in the first and second positions are compared. If the first is larger than the second, the two cards are swapped. Next, the cards in positions two and three are compared. If the first one is larger than the second, they are swapped. Otherwise, we leave them as they were. This process continues for each successive pair of cards until the card with the largest face value is positioned at the end. The next two passes over the cards are illustrated below. In the second pass the card with the second largest face value is positioned in the next-to-last position. In the third and final pass, the first two cards will be positioned correctly. ImplementationA Python implementation of the bubble sort algorithm is provided below. Program Listing
Figure 6.5.1 illustrates the swaps performed during the first pass of the algorithm when applied to an array containing 11 integer values. Figure 6.5.2 shows the ordering of the values within the array after each iteration of the outer loop. EfficiencyThe efficiency of the bubble sort algorithm only depends on the number of keys in the array and is independent of the specific values and the initial arrangement of those values. To determine the efficiency, we must determine the total number of iterations performed by the inner loop for a sequence containing values. The outer loop is executed times since the algorithm makes passes over the sequence. The number of iterations for the inner loop is not fixed, but depends on the current iteration of the outer loop. On the first pass over the sequence, the inner loop executes times; on the second pass, times; on the third, times, and so on until it executes once on the last pass. The total number of iterations for the inner loop will be the sum of the first integers, which equals
bubble sort worst-case:
resulting in a run time of . Bubble sort is considered one of the most inefficient sorting algorithms due to the total number of swaps required. Given an array of keys in reverse order, a swap is performed for every iteration of the inner loop, which can be costly in practice. The bubble sort algorithm as implemented above always performs iterations of the inner loop. But what if the sequence is already in sorted order? In this case, there would be no need to sort the sequence. But our implementation still performs all iterations because it has no way of knowing the sequence is already sorted. The bubble sort algorithm can be improved by having it terminate early and not require it to perform all iterations when the sequence is in sorted order. We can determine the sequence is in sorted order when no swaps are performed by the
|