This preview shows pages 1–4. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: McGill University COMP251: Assignment 3 Solution Question 1 Idea : A number x in { , 1 ,...,n 3 1 } can be expressed in the form a 2 n 2 + a 1 n + a where 0 a 2 ,a 1 ,a n 1. More precisely, a 2 = x n 2 ,a 1 = x n a 2 n,a = x n ( a 2 n + a 1 ) In other words, a 2 ,a 1 ,a are the digits of x written in base n : x = a 2 a 1 a n . Two numbers written in base n are compared just as in base 10: Suppose that x = a 2 a 1 a n and y = b 2 b 1 b n then x > y if and only if a 2 > b 2 or a 2 = b 2 and a 1 > b 1 or a 2 = b 2 and a 1 = b 1 and a > b We can now use Radix sorting. Here the range of each digit is k = n , and each number has 3 digits. The running time for Radix sorting is ( d ( n + k )) = ( n ) ( d is the number of digits, here d = 3). The algorithm : The algorithm Somelinearsort below takes as input an array X of integers between 0 and n 3 1, sorts it in nondecreasing order, and store the result in X . The algorithm consists of 3 main parts: Preparing for radixsort (lines 1 to 9): here we compute the base n representation for each element in X . Each X [ i ] is represented as row A [ i ] in a 2dimensional array A . Radix sort the base n representations (lines 10 to 37): There are 3 digits (thus the forloop on line 11). Now convert the sorted base n representations back and store in X (lines 3841). Somelinearsort( X ) 1. % initialization 2. n length ( X ) 3. A : an 2dimensional array of size n 3 4. % first convert the numbers into base n and store the results in A . Suppose X [ i ] = ( a 2 a 1 a ) n , then A [ i ][2] = a 2 ,A [ i ][1] = a 1 ,A [ i ][0] = a 5. for i from 1 to n do 6. A [ i ][2] X [ i ] n 2 7. A [ i ][1] X [ i ] n nA [ i ][2] 1 8. A [ i ][0] X [ i ] n ( nA [ i ][2] + A [ i ][1]) 9. end for 10. % now radix sort 11. for digit from 0 to 2 do % counting sort on the digitth digit 12. % initializes counting array C : 13. for j from 0 to n 1 do 14. C [ j ] 15. end for 16. % next make each C [ j ] be the number of i such that A [ i ][ digit ] = j : 17. for i from 1 to n do 18. C [ A [ i ][ digit ]] C [ A [ i ][ digit ]] + 1 19. end for 20. % sum up: each C [ j ] will be the number of i such that A [ i ][ digit ] j : 21. for j from 1 to n 1 do 22. C [ j ] C [ j ] + C [ j 1] 23. end for 24. % now put each row A [ i ] into the right place (in B ): 25. for i from n down to 1 do 26. B [ C [ A [ i ]][0]] A [ i ][0] 27. B [ C [ A [ i ]][1]] A [ i ][1] 28. B [ C [ A [ i ]][2]] A [ i ][2] 29. C [ A [ i ]] C [ A [ i ]] 1 30. end for 31. % copy the array B back to A : 32. for i from 1 to n do 33. A [ i ][0] B [ i ][0] 34. A [ i ][1] B [ i ][1] 35. A [ i ][2] B [ i ][2] 2 36. end for 37. end for 38. % now get back the values from the base n representations and output in X 39. for i from 1 to n do 40. X [ i ] n 2 A [ i ][2] + nA [ i ][1] + A [ i ][0] 41. end for41....
View
Full
Document
This note was uploaded on 11/05/2009 for the course COMP COMP 251 taught by Professor Phuongnguyen during the Fall '09 term at McGill.
 Fall '09
 PhuongNguyen
 Algorithms, Data Structures

Click to edit the document details