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...
Given an array A of n integers, describe an O(n log n) algorithm to decide if...
Given an array A of n integers, describe an O(n log n) algorithm to decide if the elements of A are all distinct. Why does your algorithm run in O(n log n) time?
Problem 1: Given an array A[0 ... n-1], where each element of the array represents a...
Problem 1: Given an array A[0 ... n-1], where each element of the array represents a vote in the election. Assume that each vote is given as integers representing the ID of the chosen candidate. Write the code determining who wins the election. Problem 2: How do we find the number which appeared maximum number of times in an array? ( Use Java and an original code )
Given an array A[0 … n-1], where each element of the array represent a vote in...
Given an array A[0 … n-1], where each element of the array represent a vote in the election. Assume that each vote is given as an integer representing the ID of the chosen candidate. Can you determine who wins the election? What is the complexity of your solution? Hint: it is similar to finding the element that is repeated the maximum number of times.
Using Java, Given an array A[0 ... n-1], where each element of the array represent a...
Using Java, Given an array A[0 ... n-1], where each element of the array represent a vote in the election. Assume that each vote is given as an integer representing the ID of the chosen candidate. Can you determine who wins the election? What is the complexity of your solution? Hint: it is similar to finding the element that is repeated the maximum number of times.
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.
We are given an array of n numbers A in an arbitrary order. Design an algorithm...
We are given an array of n numbers A in an arbitrary order. Design an algorithm to find the largest and second largest number in A using at most 3/2n -2 comparisons. (i) describe the idea behind your algorithm in English (3 points); (ii) provide pseudocode (5 points); (iii) analyze the number of comparisons used in your algorithm (2 points).
You are a given an array of n elements, design an algorithm to find the minimum...
You are a given an array of n elements, design an algorithm to find the minimum element and the 2nd minimum element in the array using as few comparisons as possible. For example, A[] can be [5, 6, 7, 3, 2, 1]. You should output 1 as minimum, 2 as 2nd minimum element in the array, and at the same time, minimize the number of comparisons used. (i) describe the idea behind your algorithm in English (ii) provide java code...
You are a given an array of n elements, design an algorithm to find the minimum...
You are a given an array of n elements, design an algorithm to find the minimum element and the 2nd minimum element in the array using as few comparisons as possible. For example, A[] can be [5, 6, 7, 3, 2, 1]. You should output 1 as minimum, 2 as 2nd minimum element in the array, and at the same time, minimize the number of comparisons used. (i) describe the idea behind your algorithm in English (ii) provide pseudocode (iii)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT