90 likes | 257 Views
Targil 6 Notes. This week: Linear time Sort – continue: Radix Sort Some Cormen Questions Sparse Matrix representation & usage. Bucket sort Counting sort. Radix Sort. Input: d digits numbers, each digit in a range 1 to k.
E N D
Targil 6 Notes • This week: • Linear time Sort – continue: • Radix Sort • Some Cormen Questions • Sparse Matrix representation & usage. • Bucket sort • Counting sort
Radix Sort • Input: d digits numbers, each digit in a range 1 to k. • First intuition: sort from most significant to least significant digits. The problem: many sub piles to keep track of in the recursion done. • Radix sort idea: sort from least significant to most significant digits each time by one digit only. Use stable sort at each time.
Radix Sort - 2 • Usually you use Counting Sort for each digit , so we have O(d(k+n)) running time. • Why does it work? The trick is by using the stable sort. Example: From least significant digit
Radix Sort – 3 • Proof: by Induction on the number of columns (digits) d : Easy to check for n=1. We assume for n and prove for n+1. When the digits are not the same than the stable sort manages these correctly and as these are the most significant digits (the last step of sorting) , all such numbers are ordered. If the digits are equal , than , by the induction assumption , they are already sorted correctly by the less significant digits, and as the sort is stable the proof is complete.
Radix Sort – Pros. & Cons. • As we saw , the radix sort can be very efficient in time, much more than quick sort sometimes (for example – 1 million 64-bit numbers, use 4 16-bit digits, which gives radix sort in 4 passes only, compared to the nlog(n) for average quicksort. • Cons: needs some more memory for the counting Sort, unlike QuickSort which is in place.
Questions • How to sort n integers in the range 1 to n*n in O(n) time? • Suppose you have an array of n data records to sort and that the key of each record has the value 0/1. Give a linear time algorithm for sorting these records in place. • Modify Counting Sort to sort in place in time O(n+k). You can use O(k) additional place. • Can this be applied to radix sort n records with b-bits keys in O(b*n) time?
Sparse Matrices • A sparse matrix: a matrix whose values are mostly zeros. Example: sparse graph represented as a matrix. • Idea: we can use linked lists to represent a matrix (either singly or doubly linked lists), one vector of linked lists for columns and one for rows. • Why 2 vectors? Give ability to traverse the matrix by row or column, more efficient random access (the shorter list of the two)
Sparse Matrices (2) • Cons: • 2-4 pointers for each node. • No O(1) random access. • Benefits: • we save in memory only the relevant values • We can traverse the matrix very efficiently using only the non-zero values(matrix operations) • Note: its far more efficient to use iterator in such cases and not random access by looping on column and row index (i,j) !!
Linked list with no pointers • 2 linked lists in one array, one for the occupied cells and one for the free cells. • Instead of using pointers to the next node, each cell holds the data + the index of the next node in the list. • When adding an object a cell is removed form the free list and it’s index is added to the occupied list. • What is it good for ? Nothing we can think of… but it’s a solution if you don’t have pointers in your language( and there are such languages!) , and it’s a good thing to be able to think of data structures more flexibly.