6.2 KiB
title | TARGET DECK | FILE TAGS | tags | ||
---|---|---|---|---|---|
Bubble Sort | Obsidian::STEM | 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 |
%%ANKI
Basic
Describe BUBBLE_SORT
in a single sentence.
Back: Repeatedly swap the smaller of adjacent records downward.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
END%%
%%ANKI
Basic
What is BUBBLE_SORT
's best case runtime?
Back: \Omega(n)
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
END%%
%%ANKI
Basic
How is it BUBBLE_SORT
achieves best case linear runtime?
Back: By terminating when no swaps occurred on a given iteration.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
END%%
%%ANKI
Basic
What input value does BUBBLE_SORT
perform best on?
Back: An already sorted array.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
END%%
%%ANKI
Basic
What is BUBBLE_SORT
's worst case runtime?
Back: O(n^2)
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
END%%
%%ANKI
Basic
What input value does BUBBLE_SORT
perform worst on?
Back: An array in reverse-sorted order.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
END%%
%%ANKI
Basic
What is BUBBLE_SORT
's average case runtime?
Back: O(n^2)
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
END%%
%%ANKI
Basic
Is BUBBLE_SORT
in place?
Back: Yes
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
END%%
%%ANKI
Basic
Is BUBBLE_SORT
stable?
Back: Yes
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
END%%
%%ANKI
Basic
Is BUBBLE_SORT
adaptive?
Back: Yes
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
END%%
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?
!
Back: BUBBLE_SORT
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
END%%
Loop Invariant
Consider loop-invariant P
given by
A[0..i-1]
is a sorted array of thei
least elements ofA
.
We prove P
maintains the requisite properties:
- Initialization
- When
i = 0
,A[0..-1]
is an empty array. This trivially satisfiesP
.
- When
- Maintenance
- Suppose
P
holds for some0 ≤ i < n - 1
. ThenA[0..i-1]
is a sorted array of thei
least elements ofA
. Our inner loop now starts at the end of the array and swaps each adjacent pair, putting the smaller of the two closer to positioni
. Repeating this process across all pairs fromn - 1
toi + 1
ensuresA[i]
is the smallest element ofA[i..n-1]
. ThereforeA[0..i]
is a sorted array of thei + 1
least elements ofA
. At the end of the iteration,i
is incremented meaningA[0..i-1]
still satisfiesP
.
- Suppose
- Termination
- Termination happens when
i = n - 1
. ThenP
impliesA[0..n-2]
is a sorted array of then - 1
least elements ofA
. But thenA[n-1]
must be the greatest element ofA
meaningA[0..n-1]
, the entire array, is in sorted order.
- Termination happens when
%%ANKI
Basic
Given array A[0..n-1]
, what is BUBBLE_SORT
's loop invariant?
Back: A[0..i-1]
is a sorted array of the i
least elements of A
.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
END%%
%%ANKI
Basic
What is initialization of BUBBLE_SORT
's loop invariant?
Back: Sorting starts with an empty array which is trivially sorted.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
END%%
%%ANKI
Basic
What is maintenance of BUBBLE_SORT
's loop invariant?
Back: Each iteration puts the next least element into the sorted subarray.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
END%%
%%ANKI
Basic
How does BUBBLE_SORT
partition its input array?
Back:
[ sorted | unsorted ]
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
END%%
%%ANKI
Basic
Which element will BUBBLE_SORT
move to sorted
?
[ sorted | unsorted ]
Back: The least element in unsorted
.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
END%%
%%ANKI
Cloze
Selection sort makes fewer {swaps} than BUBBLE_SORT
in the average case.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
END%%
References
- Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).