Question

In: Computer Science

The following is the pseudocode of algorithm that searches a sorted array of n items by...

The following is the pseudocode of algorithm that searches a sorted array of n items by dividing it into three sub arrays of almost n/3 items (7pt). Hint: refer Mergesort algorithm)

Input: Sorted array of Keys(A) indexed from 1 to n.
Output: Location, the Location of K in array A (hint: return 0 if K is no in A)
index location3 (int A[1…n ], index L, index H)
{
index m1, m2;
if (L > H) return 0;
else
   {
m1 = L + (H – L)/3;   //one third
m2 = L + 2*(H – L)/3;   //two thirds


      }
}
a)   Complete the pseudocode of the above algorithm which is indicated by blank box
b)   Analyze the worst-case time complexity of your algorithm in(a), and show the results using the asymptotic notation (ϴ)

Solutions

Expert Solution

a. In the algorithm, m1 and m2 gives indices of one third and two-thirds of the algorithm. Since this is a recursive algorithm, at each step we make some comparisons and runs the function again for the particular part of array. More specifically, we need to check whether the element K is greater or less than m1. If it is less than m1, search in the first one third of elements. If it is greater than m1, check whether it is greater or less than m2. If it is less than m2, search between indices m1 and m2, else search above m2. The stopping criteria can be if the element K equals the element at H or L. Hence the pseudocode at the blank box will be:

if A[H] == K, return H

else if A[L] == K, return L

else{

if K<A[m1], index(A[1...n], 0, m1)

else if K<A[m2], index(A[1....n],m1,m2)

else index(A[1....n],m2, n)

}

b. At each step the search space length is divided by 3. Assuming the worst case scenario in which the element is found at the last possible step, the worst case time complexity is O(log3n). Notice that the base of the log here is 3 and not 2.


Related Solutions

Consider the following pseudocode for insertion-sort algorithm. The algorithm sorts an arbitrary array A[0..n − 1]...
Consider the following pseudocode for insertion-sort algorithm. The algorithm sorts an arbitrary array A[0..n − 1] of n elements. void ISORT (dtype A[ ], int n) { int i, j; for i = 1 to n – 1 {     //Insert A[i] into the sorted part A[0..i – 1]     j = i;     while (j > 0 and A[j] < A[j – 1]) {         SWAP (A[j], A[j – 1]);         j = j – 1 }     }...
The following algorithm returns true if the n items in an array are all distinct, i.e.,...
The following algorithm returns true if the n items in an array are all distinct, i.e., if no two items are equal to each other, otherwise it returns false. 1 ALGORITHM UniqueElements(A[1..n]) 2 // Determine whether all the elements in an array are distinct 3 // Input: Array A[1..n] 4 // Output: “true” if all elements of A are distinct, “false” otherwise 5 for i ← 1 to n – 1 do 6 for j ← i + 1 to...
Pseudocode and algorithm of merging k sorted list with running time O(Nlogk).
Pseudocode and algorithm of merging k sorted list with running time O(Nlogk).
Pseudocode and algorithm of finding median of unordered array in linear time.
Pseudocode and algorithm of finding median of unordered array in linear time.
Assume that the incoming array is sorted and adjust the algorithm below to allow for faster...
Assume that the incoming array is sorted and adjust the algorithm below to allow for faster exit if the searched for value is not in the list. void linearsearch (int n, int Arr[], int a, int& index){ index = 1; while (index <= n && Arr[index] != a) index++; if (index > n) index = 0; }
Design an O(n log n) algorithm that takes two arrays A and B (not necessarily sorted)...
Design an O(n log n) algorithm that takes two arrays A and B (not necessarily sorted) of size n of real numbers and a value v. The algorithm returns i and j if there exist i and j such that A[i] + B[j] = v and no otherwise. Describe your algorithm in English and give its time analysis.
The following pseudocode finds the maximum element in an array of size n. Int MAX (int...
The following pseudocode finds the maximum element in an array of size n. Int MAX (int A [ ], int n) { M = A[0]; for i = 1 to n – 1     if (A[i] > M)         M = A[i]        //Update the max return M; } Write a recursive version of this program. Let f(n) be the number of key comparisons performed by this algorithm. Write a recurrence equation for f(n). Prove by induction that the solution of...
for an array A of size N, and A[0] != A[n-1]. devise an efficient algorithm for...
for an array A of size N, and A[0] != A[n-1]. devise an efficient algorithm for find a pair of elements A[i] and A[i+1] such that A[i] != A[i+1]. can you always find such pair and why
Let iqsort(A, 1, n) be an algorithm that sorts an array A with n integers. It...
Let iqsort(A, 1, n) be an algorithm that sorts an array A with n integers. It works as follows: iqsort(A, p, q){ if p ≥ q, return; r=partition(A, p, q); //run quick sort on the low part quicksort(A, p, r − 1); //run insert sort on the high part insertsort(A, r + 1, q); } Compute the best-case, worst-case, and average-case complexities of iqsort.
6. Given the following array of characters: REALITYSHOWShow how this array will be sorted with the...
6. Given the following array of characters: REALITYSHOWShow how this array will be sorted with the help of : (a) insertion sort; (b) selection sort; (c) bubble sort with swaps counting; (d) bubble sort without swaps counting subject design and analysis of algorithms answer should be like eg p u r p l e p r u p l e
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT