Question

In: Computer Science

give an algorithm where given as input an array A[1...n] withthe following property. There exists...

give an algorithm where given as input an array A[1...n] with the following property. There exists an index I such that if we append A[1...i−1] after A[i...n], we get an array in sorted increasing order. For simplicity you can assume that n is a power of 2.

Give an efficient algorithm that returns the smallest element in A. Analyze the time complexity of your algorithm.

Hint: you may want to compare A[1] and A[n/2].

Solutions

Expert Solution

If you read this question carefully you will know that this case occurs when you left rotate the array a certain number of times. for example, if you have a sorted array 1 2 3 4 5 6 7 and you circularly rotate the array twice to its left you get the array 3 4 5 6 7 1 2,

considering 0 based indexing, here for i = 5 we have the first part as 3 4 5 6 7 and the second part as 1 2, if we append the first part after the end of the second part you get a sorted array.

A fairly simple algorithm to achieve this is,

Find the minimum element by iterating the entire array once, the index of the minimum element is the value of i that you need. How does this work?
This works because the minimum element should be at the first position, so you make the second array start from this index and hence this value of i is the answer.

The time complexity of this algorithm is O(N) since in the worst case you might need to iterate over the entire array.

No we can modify the classic binary search algorithm to improve the time complexity of the problem. We can achieve the lask in O(logN) using a modified variant of binary search, let us see how.

  • if you notice, the minimum element is the only one which has its previous value greater that its own value. if we are unable to find any such element then the array is already sorted. So our major task ends up at finding te minimum element in the array. We find the middle element by comparing it with the element present at the (middle -1)th index and (middle +1)th index
  • If the element at the middle index is not the minimum element then it is present in either the left half or the right half.
    1. If middle element is smaller than last element, then the minimum element lies in left half.
    2. Otherwise the minimum element lies in right half.

This is the algorithm that would find the value of a suitable i in O(logN) time. I have provided a pseudo code also, have a look.

Here is the algorithm in textual form:

// the function that finds the element with the minimum value.
function findIndexI (int values[], int lo, int hi) :

       // Base condition, if the first element is less than the last
       // element, then it would mean that the values array is already soted
       if a[lo] < a[hi]
            return lo

        // Base condition, if there is only one element
   if (hi == lo) return values[lo]

   // store the middle index
        mid := (lo+hi) / 2
     
   // Condition when element at index (mid+1) is minimum
   if mid < hi && values[mid + 1] < values[mid]
       return values[mid + 1]
  
   // Condition when element at index (mid) is minimum
   if mid > lo && values[mid] < values[mid - 1]
       return values[mid];
  
   // if the mid value is less than the last element
   // the the result is in the left half, otherwise
   // it is in the right half.
   if (values[hi] > values[mid])
       return findIndexI(values, lo, mid - 1)

      else
          return findIndexI(values, mid + 1, hi)


Related Solutions

(a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++....
(a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++. whatDoIDo (array): 1) Build a heap from array (using buildHeap as explained in class), where the heap starts at position array[0]. 2) Starting from j = size of array - 1, as long as j>0: i. Swap the entries array[0] and array[j]. ii. Percolate down array[0], but only within the subarray array[0..j-1]. iii. Decrement j by 1. Provide three input/output examples for duplicate-free arrays...
algorithm binarySearch input bottom, top: a number    a: array a[0] to a[n-1] of numbers    x: a...
algorithm binarySearch input bottom, top: a number    a: array a[0] to a[n-1] of numbers    x: a number output result: true if x in a false otherwise Sideeffect NA Plan if (top < bottom) result := false else // get the middle of the array (refining) middle := (top - bottom)/2 + bottom (refining by +1 or -1 or integer division …) // example what is midpoint between 6 and 8 // (8-6)/2 = 1 we need to add 6 to...
In Liinear-time selection algorithm where the input array of numbers are cut into groups of size...
In Liinear-time selection algorithm where the input array of numbers are cut into groups of size 5. Show that, when the group size is 7, the algorithm still runs in linear time.
5. Design a dynamic programming algorithm to solve the following problem. Input: An array A[1, ....
5. Design a dynamic programming algorithm to solve the following problem. Input: An array A[1, . . . , n] of positive integers, an integer K. Decide: Are there integers in A such that their sum is K. (Return T RUE or F ALSE) Example: The answer is TRUE for the array A = [1, 2, 3] and 5, since 2 + 3 = 5. The answer is FALSE for A = [2, 3, 4] and 8. Note that you...
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.
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 }     }...
Consider the following algorithm. Algorithm Mystery(n) //Input: A nonnegative integer n S ← 0 for i...
Consider the following algorithm. Algorithm Mystery(n) //Input: A nonnegative integer n S ← 0 for i ← 1 to n do S ← S + i * i return S a. What does this algorithm compute? b. What is its basic operation? c. How many times is the basic operation executed? d. What is the efficiency class of this algorithm? e. Suggest an improvement, or a better algorithm altogether, and indicate its efficiency class. If you cannot do it, try...
Give an O(lg n)-time EREW algorithm to perform the prefix computation on an array x[1. ....
Give an O(lg n)-time EREW algorithm to perform the prefix computation on an array x[1. . n]. Do not use pointers, but perform index computations directly.
An array A of n distinct numbers are said to be unimodal if there exists an...
An array A of n distinct numbers are said to be unimodal if there exists an index k, 1 ≤ k ≤ n, such that A[1] < A[2] < · · · < A[k − 1] < A[k] and A[k] > A[k + 1] > · · · > A[n]. In other words, A has a unique peak and are monotone on both sides of the peak. Design an efficient algorithm that finds the peak in a unimodal array of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT