Question

6. Let T(1..n] be a sorted array of distinct integers, some of which may be negative. Give an algorithm that can find an inde

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ALGORITHM:-

  1. Compare mid with the middle element.
  2. If mid matches with middle element, we return the mid index.
  3. Else If mid is greater than the mid element, then mid can only lie in right half subarray after the mid element. So we recur for right half.
  4. Else (mid is smaller) recur for the left half.

Calculating Time complexity:

  • Hence, the time complexity of Binary Search is

    log2 (n)

    • Let say the iteration in Binary Search terminates after k iterations. In the above example, it terminates after 3 iterations, so here k = 3
    • At each iteration, the array is divided by half. So let’s say the length of array at any iteration is n
    • At Iteration 1,
      Length of array = n
    • At Iteration 2,
      Length of array = n2
    • At Iteration 3,
      Length of array = (n2)2 = n22
    • Therefore, after Iteration k,
      Length of array = n2k
    • Also, we know that after
      After k divisions, the length of array becomes 1
    • Therefore
      Length of array = n2k = 1
      => n = 2k
      
    • Applying log function on both sides:
      => log2 (n) = log2 (2k)
      => log2 (n) = k log2 (2)
      
    • As (loga (a) = 1)
      Therefore,
      => k = log2 (n)
      

PROGRAM:-

// C++ program to check fixed point
// in an array using binary search
#include <iostream>
using namespace std;

int binarySearch(int arr[], int low, int high)
{
   if(high >= low)
   {
       int mid = (low + high)/2; /*low + (high - low)/2;*/
       if(mid == arr[mid])
           return mid;
       if(mid > arr[mid])
           return binarySearch(arr, (mid + 1), high);
       else
           return binarySearch(arr, low, (mid -1));
   }

   /* Return -1 if there is no Fixed Point */
   return -1;
}

/* Driver code */
int main()
{
   int arr[10] = {-10, -1, 0, 3, 10, 11, 30, 50, 100};
   int n = sizeof(arr)/sizeof(arr[0]);
   cout<<"Fixed Point is "<< binarySearch(arr, 0, n-1);
   return 0;
}


OUTPUT:-

Fixed Point is 3
Add a comment
Know the answer?
Add Answer to:
6. Let T(1..n] be a sorted array of distinct integers, some of which may be negative....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. (16 pts.) Sorted Array Given a sorted array A of n (possibly negative) distinct integers,...

    1. (16 pts.) Sorted Array Given a sorted array A of n (possibly negative) distinct integers, you want to find out whether there is an index i for which Al = i. Give a divide-and-conquer algorithm that runs in time O(log n). Provide only the main idea and the runtime analysis.

  • 1. Design and write a Divide& Conquer algorithm that, given an array A of n distinct...

    1. Design and write a Divide& Conquer algorithm that, given an array A of n distinct integers which is already sorted into ascending order, will find if there is some i such that Ali] in worst-case 0(log n) time.

  • Suppose that we are given a sorted array of distinct integers A[1, ......,  n] and we want...

    Suppose that we are given a sorted array of distinct integers A[1, ......,  n] and we want to decide whether there is an index i for which A[i] = i. Describe an efficient divide-and-conquer algorithm that solves this problem and explain the time complexity. 1. Describe the steps of your algorithm in plain English. 2. Write a recurrence equation for the runtime complexity. 3. Solve the equation by the master theorem.

  • Let A = [A[1], A[2],…..,A[n]] be an array of n distinct integers. For 1 <= j...

    Let A = [A[1], A[2],…..,A[n]] be an array of n distinct integers. For 1 <= j <= n, the index j is a happy index if A[i] < A[j] for all 1 <= i < j. Describe an O(n)- time algorithm that finds all the happy indices in the array A. Partial credit will be given for an O(n log(n))-time algorithm and a minimal credit will be given for an O(n^2) –time algorithm. What is the running time of your...

  • An array A of n integers is called semi-sorted if it is increasing until some index...

    An array A of n integers is called semi-sorted if it is increasing until some index and then decreasing afterwards. In other words, there is some index 1 ≤ p ≤ n such that: • A[i] < A[i + 1] for 1 ≤ i < p, and • A[i] > A[i + 1] for p ≤ i < n. Note that in this case, A[p] is the maximum element of A. Give an algorithm with running time O(logn) that finds...

  • The input is an array of N integers ( sorted ) and an integer X. The...

    The input is an array of N integers ( sorted ) and an integer X. The algorithm returns true if X is in the array and false if X is not in the array. Describe an algorithm that solves the problem with a worst case of O(log N) time.

  • Suppose you are given an array A holding n distinct integers (negative values are allowed) in...

    Suppose you are given an array A holding n distinct integers (negative values are allowed) in sorted order; in other words, A[i] < A[i + 1] for each i ∈ [0, n − 2]. We say the ith element is self referential if A[i] = i. Design an O(log n) time algorithm to determine if there is a self referencial element in the array. Your solution must include a) Statement of your algorithm in plain English. (Pseudo-code is optional.) b)...

  • Design and analysis of algorithms Type in answer Problem 5. Given a sorted array of distinct...

    Design and analysis of algorithms Type in answer Problem 5. Given a sorted array of distinct integers A[1- -n], you want to find out whether there is an index I for which Ai-i. Give a divide-and-conquer algorithm that runs in time O(log n)

  • 1. Consider an array of n distinct values in which the first n − 1 values are sorted, and the las...

    1. Consider an array of n distinct values in which the first n − 1 values are sorted, and the last element is not. (It could be smaller than the first element, larger than element n − 1, or anywhere in between.) Give the worst-case number of comparisons that Insertion Sort will perform in this scenario. You can give your answer in terms of big-Theta if you wish to ignore low-order terms and constants.

  • 1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array...

    1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT