2024-02-09 18:50:56 +00:00
---
title: Bubble Sort
TARGET DECK: Obsidian::STEM
FILE TAGS: algorithm::sorting
tags:
- algorithm
- sorting
---
## Overview
Property | Value
----------- | --------
Best Case | $\Omega(n^2)$
Worst Case | $O(n^2)$
Avg. Case | $O(n^2)$
Aux. Memory | $O(1)$
Stable | Yes
Adaptive | Yes
![[bubble-sort.gif]]
2024-02-11 12:33:02 +00:00
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
Describe `BUBBLE_SORT` in a single sentence.
2024-02-11 12:33:02 +00:00
Back: Repeatedly swap the smaller of adjacent records downward.
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-11 12:33:02 +00:00
<!-- ID: 1707589393196 -->
END%%
2024-02-09 18:50:56 +00:00
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
What is `BUBBLE_SORT` 's best case runtime?
2024-02-09 18:50:56 +00:00
Back: $\Omega(n)$
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-09 18:50:56 +00:00
<!-- ID: 1707504634781 -->
END%%
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
How is it `BUBBLE_SORT` achieves best case linear runtime?
2024-02-09 18:50:56 +00:00
Back: By terminating when no swaps occurred on a given iteration.
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-09 18:50:56 +00:00
<!-- ID: 1707504634782 -->
END%%
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
What input value does `BUBBLE_SORT` perform best on?
2024-02-09 18:50:56 +00:00
Back: An already sorted array.
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-09 18:50:56 +00:00
<!-- ID: 1707504634784 -->
END%%
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
What is `BUBBLE_SORT` 's worst case runtime?
2024-02-09 18:50:56 +00:00
Back: $O(n^2)$
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-09 18:50:56 +00:00
<!-- ID: 1707504634785 -->
END%%
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
What input value does `BUBBLE_SORT` perform worst on?
2024-02-09 18:50:56 +00:00
Back: An array in reverse-sorted order.
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-09 18:50:56 +00:00
<!-- ID: 1707504634787 -->
END%%
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
What is `BUBBLE_SORT` 's average case runtime?
2024-02-09 18:50:56 +00:00
Back: $O(n^2)$
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-09 18:50:56 +00:00
<!-- ID: 1707504634788 -->
END%%
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
Is `BUBBLE_SORT` in place?
2024-02-09 18:50:56 +00:00
Back: Yes
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-09 18:50:56 +00:00
<!-- ID: 1707504634789 -->
END%%
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
Is `BUBBLE_SORT` stable?
2024-02-09 18:50:56 +00:00
Back: Yes
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-09 18:50:56 +00:00
<!-- ID: 1707504634791 -->
END%%
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
Is `BUBBLE_SORT` adaptive?
2024-02-09 18:50:56 +00:00
Back: Yes
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-09 18:50:56 +00:00
<!-- ID: 1707504634792 -->
END%%
```c
void swap(int i, int j, int *A) {
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
void bubble_sort(const int n, int A[static n]) {
bool swapped = true;
for (int i = 0; swapped & & i < n - 1 ; + + i ) {
swapped = false;
for (int j = n - 1; j > i; --j) {
if (A[j] < A [ j - 1 ] ) {
swap(j, j - 1, A);
swapped = true;
}
}
}
}
```
%%ANKI
Basic
What sorting algorithm does the following demonstrate?
![[bubble-sort.gif]]
2024-03-06 20:25:34 +00:00
Back: `BUBBLE_SORT`
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-09 18:50:56 +00:00
<!-- ID: 1707504634794 -->
END%%
## Loop Invariant
Consider [[loop-invariant|loop invariant]] $P$ given by
> `A[0..i-1]` is a sorted array of the `i` least elements of `A`.
We prove $P$ maintains the requisite properties:
* Initialization
* When `i = 0` , `A[0..-1]` is an empty array. This trivially satisfies $P$.
* Maintenance
* Suppose $P$ holds for some `0 ≤ i < n - 1` . Then `A[0..i-1]` is a sorted array of the `i` least elements of `A` . Our inner loop now starts at the end of the array and swaps each adjacent pair, putting the smaller of the two closer to position `i` . Repeating this process across all pairs from `n - 1` to `i + 1` ensures `A[i]` is the smallest element of `A[i..n-1]` . Therefore `A[0..i]` is a sorted array of the `i + 1` least elements of `A` . At the end of the iteration, `i` is incremented meaning `A[0..i-1]` still satisfies $P$.
* Termination
* Termination happens when `i = n - 1` . Then $P$ implies `A[0..n-2]` is a sorted array of the `n - 1` least elements of `A` . But then `A[n-1]` must be the greatest element of `A` meaning `A[0..n-1]` , the entire array, is in sorted order.
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
Given array `A[0..n-1]` , what is `BUBBLE_SORT` 's loop invariant?
2024-02-09 18:50:56 +00:00
Back: `A[0..i-1]` is a sorted array of the `i` least elements of `A` .
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-09 18:50:56 +00:00
<!-- ID: 1707504634796 -->
END%%
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
What is initialization of `BUBBLE_SORT` 's loop invariant?
2024-02-09 18:50:56 +00:00
Back: Sorting starts with an empty array which is trivially sorted.
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-09 18:50:56 +00:00
<!-- ID: 1707504634797 -->
END%%
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
What is maintenance of `BUBBLE_SORT` 's loop invariant?
2024-02-09 18:50:56 +00:00
Back: Each iteration puts the next least element into the sorted subarray.
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-09 18:50:56 +00:00
<!-- ID: 1707504634798 -->
END%%
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
How does `BUBBLE_SORT` partition its input array?
2024-02-09 18:50:56 +00:00
Back:
```
[ sorted | unsorted ]
```
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-09 18:50:56 +00:00
<!-- ID: 1707504634800 -->
END%%
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
Which element will `BUBBLE_SORT` move to `sorted` ?
2024-02-09 18:50:56 +00:00
```
[ sorted | unsorted ]
```
Back: The least element in `unsorted` .
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-09 18:50:56 +00:00
<!-- ID: 1707504634801 -->
END%%
%%ANKI
Cloze
2024-03-06 20:25:34 +00:00
Selection sort makes fewer {swaps} than `BUBBLE_SORT` in the average case.
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-09 18:50:56 +00:00
<!-- ID: 1707504634803 -->
END%%
## References
2024-03-19 00:28:34 +00:00
* Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).