Algorithms Flashcards
What does it mean for an algorithm to be “stable”, as opposed to “non-stable”?
A stable algorithm is one that preserves the relative order of the items with equal sort keys.
For example, let’s say we wanted to sort a list of strings, and we have two strings that start with s.
A stable algorithm is one that, no matter how many times the algorithm is repeated, those two strings will always be in the same order.
Stable algorithms tend to be more efficient, and make it easier to implement multikey sorts.
What is the general process for a selection sort algorithm?
We first go to the end of the list, then find the largest element.
That largest element is then put at the end of the list. After this, we call selection sort again on the rest of the list, until we reach the base case.
What is the order of a selection sort algorithm?
O(n²).
What are the benefits and downsides of selection sort?
It is simple and can easily be implemented in linked lists.
However, it is inefficient for large input sets, as it runs at an order of O(n²).
What is the general process for a insertion sort algorithm?
A pointer x, at the end of the list, and y, directly before it, create a “partition” at the end. With each loop, our y pointer decreases by one, letting a new element into the partition.
We then check if this new element is sorted. If it is not, we move it slowly along the partition until it is.
Repeat this until the list is sorted.
What does it mean for an algorithm to be “in-place”?
An in-place algorithm is one that does not create a new object to store the sorted data.
What is the order of an insertion sort algorithm?
O(n²).
What is the primary use of an insertion sort algorithm?
Insertion sort is typically used when we want a solution that is not in-place - that is, a solution that creates a new list at the end.
Note that insertion sort can be done in-place, but is most commonly used for the above.
What is the general process for a bubble sort algorithm?
We go through the list backwards.
For each element in the list, if that element is greater than the element before it, swap them.
Keep looping until everything is sorted.
What is the order of a bubble sort algorithm?
O(n²).
What is meant by a “divide-and-conquer” algorithm?
A divide and conquer algorithm splits a problem into many smaller subtasks, and then combines their solution into a solution for the larger instance.
What is the general method of a Quicksort algorithm?
We pick one item from the list (e.g. list[i]), at any point, and call it the pivot. Commonly, we pick the middle element.
We then reorganise the list so that all smaller elements are placed before the pivot, and all larger elements are placed after the pivot.
We then call Quicksort again to solve the partitions. We can also use another sorting algorithm on these partitions if we want it to be simpler.
What is the master theorem for divide and conquer algorithms?
The master theorem is a theorem that is always correct for determining the order of a D&C algorithm.
It states that:
T(n) = O(nᵈ) if a < bᵈ
T(n) = O(nᵈ log n) if a = bᵈ
T(n) = O(nˡᵒᵍᵇ ᵃ) if a > bᵈ
Where a is the number of subproblems created at each stage…
b is the divisor of the size of the array…
and d is the number of subproblems created at each recursive stage.
What is the order of a Quicksort algorithm?
O(n log n).
What is the general method of a Mergesort algorithm?
We recursively break the given input array by half each time until the array cannot be split anymore.
When this is done, and we have many arrays consisting of one element, we recursively merge each “pair” of arrays by choosing the smallest element and putting it into the front of a new array.
When we have done this to every pair of arrays, we have a sorted list.