Running Time:: T (P) O (1) + T (P - 1)
Running Time:: T (P) O (1) + T (P - 1)
Sol: (a)
Algo:
EXTRACT-MIN(A)
1. k A[1,1]
2. A[1.1] ∞
3. MAKE-YOUNG-TABLEAU(A,1,1)
4. Return k
MAKE-YOUNG-TABLEAU(A, x, y)
1. Small_x x
2. Small_y y
3. If x+1≤m and A[x,y] > A[x+1,y]
4. Then small_x x+1
5. Small_y y
6. If y+1 ≤ n and A[small_x,small_y] > A[x,y+1]
7. Then small_x x
8. Small_y y+1
9. If small_x ≠ x OR small_y ≠ y
10. Then swap A[x,y] A[small_x,small_y]
11. MAKE-YOUNG-TABLEAU(A, small_x, small_y)
Running Time:
The running time of MAKE-YOUNG-TABLEAU is O(m + n) because MAKE-YOUNG-
TABLEAU swaps A [x ,y] with either A [x+ 1, y] or Y [x, y + 1] and recursively MAKE-
YOUNG-TABLEAU the tableau at the corresponding entry. Therefore, with every recursive
step, x+y is incremented by 1. However, x +y cannot be more than m + n and this gives the
time bound of O(m+ n). More precisely, we can say that YOUNGIFYing a tableau of size m x n
will recursively MAKE-YOUNG-TABLEAU a tableau of size (m- 1)xn or of size mx(n-1).
Algo:
c)
2 3 5 14
4 6 9 16
7 8 12 ∞
∞ ∞ ∞ ∞
Inserting value 10
2 3 5 14 2 3 5 14
4 6 9 16 4 6 9 16
7 8 12 ∞ 7 8 12 10
∞ ∞ ∞ 10 ∞ ∞ ∞ ∞
2 3 5 14 2 3 5 10
4 6 9 10 4 6 9 14
7 8 12 16 7 8 12 16
∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
EXTARCT-MIN
2 3 5 10 ∞ 3 5 10
4 6 9 14 4 6 9 14
7 8 12 16 7 8 12 16
∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
∞ 3 5 10 3 ∞ 5 10
4 6 9 14 4 6 9 14
7 8 12 16 7 8 12 16
∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
3 5 ∞ 10 3 5 9 10
4 6 9 14 4 6 ∞ 14
7 8 12 16 7 8 12 16
∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
3 5 9 10 3 5 9 10
4 6 12 14 4 6 12 14
7 8 ∞ 16 7 8 16 ∞
∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
Q_02:
RANDOMIZED-SELECT (A, p, r, i)
1. For j 1 to length[A]
2. If p = r
3. Then return A[p]
4. q RANDOMIZED-PARTITION(A,p,r)
5. If i= q
6. Then return A[q]
7. Else if i> q
8. Then p q+1
9. Else
10. r q-1
Q_03:
Sol:
a).
n
Maximum Number of inversions: C2 =n(n-1)/2
Order of list: Descending ordered list
Because in the descending ordered list for every i<k ai > ak
Therefore every pair is an inversion.
b).
1.Insertion Sort:
In K-sorted array the maximum number of inversions are nk/2.
Therefore using insertion sort the outer loop will run n-times and the inner loop
will run k-times at maximum for each iteration of outer loop.
Therefore the Running time of insertion sort on k-sorted array is
T=O(nk).
ii. Merge-Sort:
In case of Merge-sort the Running time will be O(nlog n) irrespective of the order
of array(whether k-sorted or not).Because it divides the input array into two parts
recursively and then merges in a sorted order.
iii. Quick-Sort:
In case of Quick sort the worst case of k-sorted array is that it divides the array
into two parts containing n-k and k elements recursively considering the n-kth as
pivot. Therefore the recursion of quick sort in this case becomes
T(n)=T(n-k)+Ө(n)
Bonus Part:
This Algorithm is based on heap concepts.We have a separate area of length k to hold the heap.
We arrange copies of the first k elements in the heap. The idea is for each successive index,
i=0,...,n-k-1, we write the top of the heap into array[i] and add array[i+k] to the heap, all of this
being done in such a manner that the heap property is preserved.
The way to do this is to remove the top element, sift up, put the new entry into the newly vacated
bottom row position, and then sift it up into place. When there are no more new elements we sift
out the remaining elements using the standard heapsort. This also is a n*log(k) algorithm.
NUST-SEECS
ASSIGNMENT : 02
Submitted By:
Asif Ali 05
Section: BIT-10B
Date: 05/04/2010