Unformatted text preview: (b) Give an algorithm for sorting a list of n keys whose member is either 0 or 1 in O(n). (You are allowed to read the list only ONCE.) 3. We are given an n × n array A . A = a 11 a 12 ··· a 1 n a 21 a 22 ··· a 2 n . . . . . . a n 1 a n 2 ··· a nn The elements in each row are sorted, i.e., a i,j ≤ a i,j +1 , for j = 1 , 2 , ··· ,n1 for each row i , where 1 ≤ i ≤ n . The elements in each column are also sorted, i.e., a i,j ≤ a i +1 ,j , for i = 1 , 2 , ··· ,n1 for each column j , where 1 ≤ j ≤ n . Propose a method to ﬁnd the element a ij in A that is equal to a query value x . In other words, your method should report ( i,j ) if x = a ij for some 1 ≤ i,j ≤ n , and (0 , 0), if x is not equal to any element in A . The number of comparisons should be less than or equal to 2 n . 1...
View
Full Document
 Spring '08
 Unkown
 Algorithms, Sort, Heap Construction

Click to edit the document details