String Sorts Flashcards
1
Q
How does key index counting work?
A
- Compute frequency counts using a count array that is of size R + 1, using the key itself as an index. Therefore count[a[i] + 1]++
- Create indices for distribution by cumulating, for r = 0 -> R, count[r+1] = count[r]. This tells us how many keys appear before r, and thus gives the index of r at a[]
- Go through whole given aray. Access the count array using the char at the given array, add it to the index = count in the aux array and increment the count of the character
- Copy aux over to original array
2
Q
How does LSD String sort work?
A
Consider characters from right to left
Perform key-indexed counting on each char of the strings
Each string needs to be the same length
3
Q
How does MSD work?
A
Create count array of size R + 2 and set values to 0
1. Count frequencies of first character
2. Transform the counts into indices
3. Distribute back
2. Sort subarrays corresponding to each character excluding first. Therefore sort(array, distributed index for character, lo + count[r+1] - 1, d + 1)
4
Q
What is the running time for LSD and MSD?
A
LSD: NW
MSD: N and Nw
5
Q
Where is the sweet spot for LSD and MSD?
A
LSD: short fixed-length strings
MSD: Random strings