ALGORITHM:-
Calculating Time complexity:
Hence, the time complexity of Binary Search is
log2 (n)
Length of array = n
Length of array = n⁄2
Length of array = (n⁄2)⁄2 = n⁄22
Length of array = n⁄2k
After k divisions, the length of array becomes 1
Length of array = n⁄2k = 1 => n = 2k
=> log2 (n) = log2 (2k) => log2 (n) = k log2 (2)
=> 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
6. Let T(1..n] be a sorted array of distinct integers, some of which may be negative....
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 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 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 <= 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 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 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 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 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 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 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"...