Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. You are given the following sequence of numbers 45 60 0 10 90 2580 How many c

ID: 3593022 • Letter: 1

Question

1. You are given the following sequence of numbers 45 60 0 10 90 2580 How many comparisons and exchange (or swap) operations are executed by each of the following sorting algorithms applied to this input? .Selection sort (5 points) · Insertion sort (5 points) Heap sort (5 points) Standard quick sort (5 points) You are given a sequence of 1,000,000 integers taking values between 0 and 1,000. Which sorting algorithm is optimal to sort the sequence: an optimal comparison-based sorting algorithm? Or radix sort? Prove your answer. (7 points) What if the integers range between 0 and 1,000,000? (3 points) 2. 3. Let X[1.n] and Y[1...n] be two arrays, each containing n numbers already in sorted order. Give an O(log n) time algorithm to find the median of all 2n elements in arrays X and Y. Write the pseudocode and comment it (5 points). Show how the algorithm works on the following arrays (5 points): X: 10 20 304050 Y: 35 456080100

Explanation / Answer

Hi, Please find my answer for Q3.

Please repost other in separate post.


Algorithm:

Let ar1 and ar2 be the input arrays.

Algorithm:

1) Calculate the medians m1 and m2 of the input arrays ar1[]
and ar2[] respectively.
2) If m1 and m2 both are equal then we are done.
return m1 (or m2)
3) If m1 is greater than m2, then median is present in one
of the below two subarrays.
a) From first element of ar1 to m1 (ar1[0...|_n/2_|])
b) From m2 to last element of ar2 (ar2[|_n/2_|...n-1])
4) If m2 is greater than m1, then median is present in one   
of the below two subarrays.
a) From m1 to last element of ar1 (ar1[|_n/2_|...n-1])
b) From first element of ar2 to m2 (ar2[0...|_n/2_|])
5) Repeat the above process until size of both the subarrays
becomes 2.
6) If size of the two arrays is 2 then use below formula to get
the median.
Median = (max(ar1[0], ar2[0]) + min(ar1[1], ar2[1]))/2

Time Complexity: O(logn)


Example:

ar1[] = {10, 20, 30, 40, 50}
ar2[] = {35, 45, 60, 80, 100}
For above two arrays m1 = 30 and m2 = 60

For the above ar1[] and ar2[], m1 is smaller than m2. So median is present in one of the following two subarrays.

[30, 40, 50] and [35, 45, 60]
Let us repeat the process for above two subarrays:

m1 = 40 m2 = 45.
m1 is smaller than m2. So the subarrays become

[40, 50] and [35, 45]
Now size is 2, so median = (max(ar1[0], ar2[0]) + min(ar1[1], ar2[1]))/2
= (max(40, 35) + min(50, 45))/2
= (40 + 45)/2
= 42.5