{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

t3 - between ` and u(both inclusive That is ﬁnd the...

This preview shows page 1. Sign up to view the full content.

CS3230 Tutorial 3 1. Draw a decision tree to show that 4 numbers can be sorted using at most 5 comparisons. 2. Given an array A of integers and a number k , give an efficient algorithm to decide if there exist two elements A [ i ] and A [ j ], i = j , such that A [ i ] + A [ j ] = k . Give time complexity bound of your algorithm. 3. Consider the following modification of the partition algorithm done in class. Show that it works correctly. Partition( A, i, j ) Assumption: i < j 1. Let m = i + 1, n = j ; 2. While m n , do { 3. While A [ m ] < A [ i ] and m n do { m = m + 1 } 4. While A [ n ] A [ i ] and n m do { n = n - 1 } 5. If n > m , then swap( A [ m ], A [ n ]). } 6. swap( A [ i ] , A [ n ]) 7. Return n . End 4. Given as input a sorted array A , containing n elements, and two numbers and u (where u ). Give an algorithm to find how many numbers are there in the array which are
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: between ` and u (both inclusive). That is, ﬁnd the number of x in the array A such that ` ≤ x ≤ u . What is the time complexity of your algorithm. 5. Suppose we are given an array of n integers between 1 to m (both inclusive). Preprocess the array such that one can answer the following query in constant time: How many numbers are there in the array which are between ` and u (both inclusive), where 1 ≤ ` ≤ u ≤ m . What is the time complexity of your preprocessing algorithm? Try to make it linear in m and n . 1...
View Full Document

{[ snackBarMessage ]}

What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern