Non - Comparison Sorting Algorithms Flashcards
What are the 2 non-comparison sorting algorithms we have studied?
- Counting sort
- Radix sort
What is the Big-Oh complexity of counting sort?
O(n)
Requirement of counting sort?
integers in the range of 0 to k. Where k is the size of the structure holding the counts.
Is counting sort stable?
Yes
Is counting sort in place?
No
What is the memory requirement of counting sort?
Linear O(n)
Is counting sort used within radix sort?
Yes
How does counting sort work?
- Counting sort is a non-comparison sorting algorithm, which works on the basis of counting occurrences of each element and placing the count in a structure (call it C).
- Once the count of the occurrences has been determined, we aim to calculate the cumulative sum within that same structure.
- With this now we can place each value of A (call an individual value As) iterating from the end , in a new array at the index C[As] -1. (occurrence count is updated respectively, so we -1 to C[As] as one occurance has been covered).
- This is repeated until all elements from the original array are accounted for (C is full of 0s and end of original array has been reached).
In practice when is counting sort used?
When k = O(n)
What is the requirement for radix sort?
That all numbers to be sorted have the same number of digits
Is radix sort counting sort based?
Yes
How does radix sort work?
- Radix sort is another non-comparison sorting algorithm, which sorts individual digits of the number taking into account only smaller digits (e.g. for sorting tens we take into account the previously sorted units), so that any collisions can be solved by looking at the lower digits sorted beforehand. The sorting happens via counting sort.
- This is repeated until all digits are accounted for, resulting in a final sorted sequence.
What is the Big-Oh complexity of radix sort?
O(n), as the amount of digits is linear.
Can counting sort be replaced with another sort within radix ?
Yes, any stable sort algorithm.
What do we exploit with non-comparison algorithms?
The input properties.