Bubble Sort
Bubble Sort is a simple, inefficient sorting algorithm used to sort lists. It is generally one of the first algorithms taught in computer science courses because it is a good algorithm to learn to build intuition about sorting. While sorting is a simple concept, it is a basic principle used in complex computer programs such as file search, data compression, and path finding. Running time is an important thing to consider when selecting a sorting algorithm since efficiency is often thought of in terms of speed. Bubble Sort has an average and worstcase running time of \(O(n^2)\), so in most cases, a faster algorithm is more desirable. \(\hspace{4cm}\)
Sorting
An algorithm that maps the following input/output pair is called a sorting algorithm:
Input: An array, \(A\), that contains \(n\) orderable elements: \(A[0,1,...,n1]\).
Output: A sorted permutation of \(A\), called \(B\), such that \(B[0] \leq B[1] \leq ... \leq B[n1].\)
Here is what it means for an array to be sorted.
An array \(<a_n>\) is sorted if and only if for all \(i<j\), \(a_i \leq a_j.\ _\square\)
In other words, a sorted array is an array that is in a particular order. For example, \([a,b,c,d]\) is sorted alphabetically, \([1,2,3,4,5]\) is a list of integers sorted in increasing order, and \([5,4,3,2,1]\) is a list of integers sorted in decreasing order.
By convention, empty arrays and singleton arrays (arrays consisting of only one element) are always sorted. This is a key point for the base case of many sorting algorithms.
Bubble Sort
The Bubble Sort algorithm compares each pair of elements in an array and swaps them if they are out of order until the entire array is sorted. For each element in the list, the algorithm compares every pair of elements.
The Bubble Sort algorithm is as follows:
 Compare \(A[0]\) and \(A[1]\). If \(A[0]\) is bigger than \(A[1]\), swap the elements.
 Move to the next element, \(A[1]\) (which might now contain the result of a swap from the previous step), and compare it with \(A[2]\). If \(A[1]\) is bigger than \(A[2]\), swap the elements. Do this for every pair of elements until the end of the list.
 Do steps 1 and 2 \(n\) number of times.
The animation below illustrates Bubble Sort:
\(\hspace{4cm}\)
(Image from: Wikipedia)Sort the array \(A=[7,3,1,4,2]\) using the Bubble Sort algorithm. Show all of the steps that the algorithm takes.
Here is pseudocode describing the algorithm:
1 2 3 4 5 6 

This is how the code works:
First, it goes from \(j=1\) to \(j=N1\) comparing each element of the list with the next (i.e, the \((j+1)^\text{th}\) element). If the \(j^\text{th}\) element is bigger than the next one, they change places, and so on. This way, in the first iteration, the element with the greatest value goes to the last position (i.e. goes to \(\text{a[N]}\)). Doing the same, in the second iteration of the loop, \(j\) goes from \(j=1\) to \(j=N2,\) and the element of the second greatest value goes to one position before the last element (i.e. it goes to \(\text{a[N1]}\)). The program does this process until the array is sorted.
The pseudocode above sorts the list in an increasing order. What would you modify to make your program sort the elements in decreasing order?
You could simply change the third line of the pseudocode: instead of using \(\text{"if a[j]>a[j+1],"}\) you should use \(\text{"if a[j]<a[j+1]"}.\)
Implementing Bubble Sort
Here is one way to implement Bubble Sort in Python. There are other ways to implement the algorithm, but all implementations stem from the same ideas. Bubble Sort can be used to sort any orderable list.
1 2 3 4 5 6 7 

Complexity of Bubble Sort
To calculate the complexity of the Bubble Sort algorithm, it is useful to determine how many comparisons each loop performs. For each element in the array, Bubble Sort does \(n1\) comparisons. In BigO notation, Bubble Sort performs \(O(n)\) comparisons. Because the array contains \(n\) elements, it has an \(O(n)\) number of elements. In other words, Bubble Sort performs \(O(n)\) operations on an \(O(n)\) number of elements, leading to a total running time of \(O(n^2)\).
Another way to analyze the complexity of Bubble Sort is by determining the recurrence relation that represents it.
When \(i=1,\) no comparisons are made by the program. When \(i=2,\) one comparison is made by the program. When \(i=3,\) two comparisons are made, and so on. Thus, we can conclude that when \(i=m,\) \(m1\) comparisons are made. Hence, in an array of length \(n\) it does \(1+2+3+4+\cdots+(n2)+(n1)\) comparisons.
Note that: \[ \sum_{q=1}^{p} q = \frac{p(p+1)}{2}. \]
Using the previous formula to calculate \(1+2+3+4 \cdots +(n2)+(n1),\) it follows that
\[ \frac{(n1)(n1+1)}{2}=\frac{n(n1)}{2}. \]
Use the Master Theorem to solve this recurrence for the running time. As expected, the algorithm's complexity is \( O(n^2).\)
Note: \( O(n)\) is the bestcase running time for Bubble Sort. It is possible to modify Bubble Sort to keep track of the number of swaps it performs. If an array is already in sorted order, and Bubble Sort makes no swaps, the algorithm can terminate after one pass. With this modification, if Bubble Sort encounters a list that is already sorted, it will finish in \( O(n)\) time.
Though Bubble Sort is simple and easy to implement, it is highly impractical for solving most problems due to its slow running time. It has an average and worstcase running time of \( O(n^2)\), and can only run in its bestcase running time of \( O(n)\) when the input list is already sorted.
Bubble sort is a stable sort with a space complexity of \(O(1)\).