Comparison Algorithms Flashcards
What is the best Big-Oh complexity of insertion sort?
O(n)
What is the worst Big-Oh complexity of insertion sort?
O(n^2)
Is insertion in-place?
Insertion sort is in-place.
Best time to use insertion sort?
When there is a small number of elements.
Is insertion sort stable?
Yes
What does it mean for a comparison algorithm to be stable?
The order of equal elements in the original sequence is preserved in the sorted sequence.
How does insertion sort work?
- The array is split into a sorted and unsorted section.
- We iterate through the unsorted part and handle each element
- We check whether the element is smaller than the first rightmost element of the sorted section.
- If smaller then we swap them, this repeats until the end of the sorted section or there is no swap (this is when correct position for element is found)
Is merge sort , divide and conquer based?
Yes
What is the merge sort Big-Oh complexity?
O(nlogn)
How does merge sort work?`
- The algorithm keeps splitting the array using the midpoint, until 1 element ‘arrays’
- The units are compared and sorted upon comparison (first iteration we compare 1 element vs 1, second 2 element vs 2)
- Repeat until the last comparison is on the 2 halves of the array
What is the complexity of the procedure of MERGE (no merge sort)
O(n), as we iterate through 2 sections of array, which is at worst length of array
What is the memory requirement of MERGE?
O(n), as we copy split sections into left and right arrays.
Is in-place implementation of MERGE (and hence merge-sort) possible?
Yes, but stability is lost.
Is merge-sort in place?
No
Some possible improvements for merge sort?
- iterative implementation
- use insertion-sort on small sections of array