Week 4 Flashcards
what is a bucket sort?
- it uses keys as indices of a bucket B that has entries within an integer range [0,1,…N-1]
- so it has size |N|
- inially all items from the input sequence are moved to appropriate buckets, so an item with key K is moved to bucket B[k]
- Then move all items back into the sequence according to their order of appearance in consecutive buckets B[0], B[1],… B[N-1]
What is the time complexity of a bucket sort?
- we have a for loop to run through every element which is O(n)
- and then for each of the buckets we remove every item so this is O(N+n)
What is a radix sort?
- A radix sort uses bucket sorts with columns
- Can use it to order/compare words or fixed length numbers
- Can start from either the most or least significant bit, going column by column
- so on the first column, it compares the first bit of each item by doing a bucket sort, it then moves onto the next column if the items are equal
What is the time complexity of a radix sort
d is the number of columns it has to perform a bucket sort on, so the time complexity = O(d*(N+n))
How do we perform a radix sort on words of uneven length?
We make the words equal length by introducing a dummy character, that doesn’t belong to the alphabet, repeatedly for each extra character to make them equal length, then we can compare them.
- we class the dummy character as smaller than any letter in the alphabet (since it’s an empty space) so at_ _ will always be smaller than ata_
When is a sorting algorithm described as stable?
If it is order preserving
- so if there’s a tie on the ith column, then the original order of the two elements remains unchanged
-
Why does choosing to go from left to right (MSB) or right to left (LSB) make a difference in Radix sorting?
Depending on what you’re sorting, it could mean it sorts it into the wrong order
- for example since we read words from left to right, we have to compare them using the leftmost or most significant bit
- but since we ready binary numbers from the left, we need to compare them using the rightmost bit or least significant bit
- otherwise the comparison would be wrong!
If we have integers of 6 digits and 4 digits long, and we perform a radix sort, what is the value of d?
- the value of d = 6, since we have to make the numbers the same length (by padding the 4 digit number with two dummy characters), so we have to compare 6 columns
What type of algorithm is a merge sort?
A divide and conquer algorithm
What type of algorithm is a quicksort?
- It’s similar to a divide and conquer accept that it doesn’t divide the problem into equal size subproblems
- with the randomised pivot, it aims to divide them into roughly equal sized problems
What is the general outline for using the divide and conquer method?
- divide: if the input is small, directly solve (use a base case), otherwise divide input into smaller disjoint sets
- recur: recursively solve the associated subproblems (that use disjoint sets)
- conquer: take solutions of subproblems and merge them into a solution for the original problem
How do we analyse the running time of a divide and conquer algorithm?
we use a recurrence relation T(n) which denotes running time based on input size n
What is the recurrence relation we use to characterise running time of a merge sort?
T(n) = {c if n < 2, 2T(n/2)+cn if otherwise}
- since if the list is smaller than 2, we know if the item is found or not, else wise keep dividing the list cn and doing the comparison - n
What is a recurrence relation?
An equation that defines a sequence based on a rule that gives the next term as a function of the previous terms
What is the recurrence relation for a binary search?
- T(n) = {c if n < 2, T(n/2) + c}
where c is the time to find the midpoint of the list and do the comparison - each time we halve our data we add a constant c
- if we only have one element in the list it takes constant time to check if it’s the item we want