for i = n-1 downto 1 do for j = 1 to i do
if A[j] > A[j+1] then swap(A[j],A[j+1])
Let A and B be two sorted arrays, each with n elements. You have to write an efficient algorithm which finds out if A and B have any elements in common.
For example if A = [4, 7, 12, 15], B = [2,4,6,23000] the answer should be yes (since 4 is an element in common), if A = [4, 7, 900], B = [2,3, 28] the answer should be no. In terms of efficiency, the faster your algorithm is, the better.
You will get substantial partial credit if your algorithm is correct and works in time smaller than O(n2) but bigger than O(n).
1 a b k; each range query asks how many A[i]’s are in the range a . . . b, i.e., how many numbers from A lie between a and b. For example, if k = 10, and the array A has seven elements 5, 9, 3, 5, 10, 6, 1, 7 the range query [2, 7] asks how many numbers are there in the array A between 2 and 7, so the answer should be 5 (because the numbers 3,5,5,6,7 from A lie in the range [2, 7]). You want to do this efficiently. In order to do this, you first do some preprocessing, which is a one-time operation i.e. you are willing to pay the one-time relatively expensive cost of doing preprocessing before starting to answer the range queries, in order to save time with the many range queries which will follow. Your algorithm should work within the following time bounds:
Each range query should be answered in O(1) (i.e. constant) time i.e. the amount of time to answer the range query [a, b] will be a small number which will in no way depend on n,k,A,a,b or the number of elements which actually lie in the range [a, b].
Hint: Think about counting sort discussed in class and in the textbook. You will need to keep the auxillary array C[1..k] we used for counting sort where C[j] represents the number of elements from A which are less than or equal to j. In the above example, C will be 1 since there is one element (i.e. 1) in A which is 2; C will be 6 since there are 6 elements (i.e. 1,3,5,5,6,7) which are 7. In class we saw how to fill C in time O(n + k) (this is the preprocessing step), and once the array C is available, you should show how to use it to answer range queries in O(1) time.
In this part you have to come up with an algorithm to answer a more complicated range query, namely, given an array A as above, a range query [a, b] now requires you to actually list all of the A[i]’s in the range a . . . b. Using the same array A as above, the range query [2, 7] asks which numbers from A are there in the array A are between 2 and 7, so the answer should be 3,5,5,6,7 (it doesn’t matter in what order the numbers are outputted).
Your algorithm should work within the following time bounds:
The one time preprocessing step should take O(n + k) time (the preprocessing here will be different from that in the first part).
Given a range query [a, b], the time taken to answer this query should not depend on k or on n or on the quantity b a; instead, the query should be answered in time O(t) where t is the number of elements in the output (the number of A[i]’s in the range [a, b]). For example, if we consider two range queries, [a1, b1] and [a2, b2], [a1, b1] leads to an output of 500 elements, and [a2, b2] leads to an output of 100 elements, then answering [a1, b1] should take approximately 5 times as much time as answering [a2, b2]. Or consider another example, where k = 106, n = 105, a = 2000, b = 3000, and there are only three elements in the range [2000, 3000]. If you look at every entry in C between 2000 and 3000 i.e. you look at C, C, C, . . . , C, you are looking at b − a = 1001 entries which is too expensive since t = 3 is much smaller than b − 1 i.e. there are only three elements in the range [2000, 3000].
Hint: You will need to keep one or more auxillary arrays; what these are going to be, you need to figure out. You should show how to fill these auxillary array(s) in time O(n + k) (this is the preprocessing step), and once these auxillary array(s) are available, you have to figure out how they can be used to quickly answer the range queries.
Extra Credit Problem 1: Implement (i.e. program) insertion sort, merge sort and UNH sort, run your programs on different random inputs (i.e. the inputs are generated using a random number generator) with 10,000 numbers, and compare the three algorithms on how much time they take. Hand in a hard copy of your code, a hardcopy of some sample runs and the timing analysis; the timing analysis (how much time each of the sorting programs actually takes) should be presented in an easy to understand way. A soft copy of your program should also be submitted on blackboard.
Extra Credit Problem 2: Implement (program) your algorithm to find out if there is an index k such that T [k] = k (problem 4), and show how it works on some examples. Hand in a hard copy of your code and a hardcopy of some sample runs. A soft copy of your program should also be submitted on blackboard.
Extra Credit Problem 3: Implement (program) your algorithm for the first range query problem (problem 5), and show how it works on some examples. Hand in a hard copy of your code and a hardcopy of some sample runs. A soft copy of your program should also be submitted on blackboard.
with this assignment on algorithms design and analysis.
DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma
Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t
Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th
1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of
1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of