SparkNotes: Free Study Guides No Fear Shakespeare: The Bard made easy SparkCharts: Just the facts TestPrep: SAT, ACT, and more 101s: College texts condensed Subject Finder: Browse by subject SparkCollege: Get in! SparkLife: 100% study-free home_bottom home_top BN_link
 
Bubble Sort
 
 
Sorting Terms
 
 
The Bubble Sort Algorithm
 
 
Bubble Sort Problems
 
 
 
 
◄ PREVIOUS
Sorting Terms
NEXT ►
Bubble Sort Problems
 
 

Bubble Sort

 
 

The Bubble Sort Algorithm

 
The algorithm for bubble sort requires a pair of nested loops. The outer loop must iterate once for each element in the data set (of size n) while the inner loop iterates n times the first time it is entered, n-1 times the second, and so on. Consider the purpose of each loop. As explained above, bubble sort is structured so that on each pass through the list the next largest element of the data is moved to its proper place. Therefore, to get all n elements in their correct places, the outer loop must be executed n times.
 
The inner loop is executed on each iteration of the outer loop. Its purpose is to put the next largest element is being put into place. The inner loop therefore does the comparing and swapping of adjacent elements. To determine the complexity of this loop, we calculate the number of comparisons that have to be made. On the first iteration of the outer loop, while trying to place the largest element, there have to be n - 1 comparisons: the first comparison is made between the first and second elements, the second is made between the second and third elements, and so on until the n-1th comparison is made between the n-1th and the nth element. On the second iteration of the outer loop, there is no need to compare the against the last element of the list, because it was put in the correct place on the previous pass. Therefore, the second iteration requires only n-2 comparisons. This pattern continues until the second-to-last iteration of the outer loop when only the first two elements of the list are unsorted; clearly in this case, only one comparison is necessary. The total number of comparisons, therefore, is (n - 1) + (n - 2)...(2) + (1) = n(n - 1)/2 or O(n2).
 
The best case for bubble sort occurs when the list is already sorted or nearly sorted. In the case where the list is already sorted, bubble sort will terminate after the first iteration, since no swaps were made. Any time that a pass is made through the list and no swaps were made, it is certain that the list is sorted. Bubble sort is also efficient when one random element needs to be sorted into a sorted list, provided that new element is placed at the beginning and not at the end. When placed at the beginning, it will simply bubble up to the correct place, and the second iteration through the list will generate 0 swaps, ending the sort. Recall that if the random element is placed at the end, bubble sort loses its efficiency because each element greater than it must bubble all the way up to the top.
 
The absolute worst case for bubble sort is when the smallest element of the list is at the large end. Because in each iteration only the largest unsorted element gets put in its proper location, when the smallest element is at the end, it will have to be swapped each time through the list, and it wont get to the front of the list until all n iterations have occurred. In this worst case, it take n iterations of n/2 swaps so the order is, again, n2.
 
Best Case: n Average Case: n2 Worst Case: n2
 
 
 
Help | Feedback | Make a request | Report an error | Send to a friend

◄ PREVIOUS
Sorting Terms
NEXT ►
Bubble Sort Problems
 
 
 
C++ Programming message board
Ask a question or post an answer on the community boards.
 
Writing Help
A blog about grammar, writing, and your papers.
 
 
 
Can't face the work right now? Waste a few minutes with us.
Life
It's already July
Better get cracking on that summer fling
 
Life
"You look tired"
Translation: "You look absolutely horrible."
 
Books
James pulls the old mom's-voice-playing-on-a-VHS trick
And Dan can't believe Bella falls for it
 
 
Study Guides
Learn more about the subject you're studying with these related SparkNotes.
  • Insertion Sort
  • Select Sort
  • Merge Sort
  • Quick Sort
  • Heaps / Heap Sort
  • Review of Sorting
  •  
    Contact Us | Privacy Policy | Terms and Conditions | About | Sitemap
    ©2009 SparkNotes LLC, All Rights Reserved.