Recap
Class Materialβ
- You can find the source PDF document here.
Merge Sortβ
Why is Merge Sort ?
Bubble Sortβ
The number of comparisons decreases with each iteration as the largest elements "bubble" to the end of the list. Because of this, the runtime complexity of bubble sort should surely be less than ?
However, despite this, bubble sort's worst-case time complexity remains , and here's why.
Breakdown of Bubble Sort's Behavior:β
- In bubble sort, for each pass through the array, adjacent elements are compared and swapped if necessary. After each pass, the largest unsorted element is placed at its correct position at the end of the list.
- After the first pass, the largest element is in place, meaning we don't need to compare that element in the next pass.
- As you observed, the number of comparisons decreases with each pass:
- First pass: comparisons
- Second pass: comparisons
- And so on, until the last pass requires just 1 comparison.
Total Number of Comparisons:β
The total number of comparisons made across all passes can be calculated by summing up the comparisons for each pass:
See the summation formula below. This sum simplifies to , which is still when considering asymptotic complexity.
Binary Searchβ
How fast can a given integer be found in a sorted array of integers?
Answer: . Using binary search on a sorted array, the search scope reduces by half of the previous search scope with each pass. It's the inverse of , which is
- In a linked list, even with sorted elements, the search time is .
- Ths is because linked lists have no constant access time.
Subsetsβ
How many subsets are there in a set of elements?
Answer: . Use bit-strings to solve this problem (1 = element included in the set, 0 = not).
If I want to count number of subsets with a given number of elements (e.g. count number of subsets with exactly 3 elements), use .
Summationβ
Using geometry:β
The total number of squares in the region highlighted in black is what we're interested in counting. That is, .
Using some 10-year-old's sorcery:β
Graphsβ
How many edges can a simple graph with n vertices can have?
Using the summation formula:β
Using combinatorics:β
In a simple graph with vertices, each pair of distinct vertices can be connected by at most one edge. This means the maximum number of edges occurs when every vertex is connected to every other vertex.
The total number of ways to choose 2 vertices from vertices (i.e., the number of pairs of vertices) is given by the binomial coefficient:
Thus, the maximum number of edges in a simple graph with vertices is .