Explanation of the Above code:
In the above algorithm, we are searching a number.
Actually it is a Binary search algorithm
-------------------------------------------------------------------------------------------------------------------------------------------
let's we have the following array:
array = [4, 5,7, 17 , 20] --Sorted Array
Here is the following parameter passed :
index low --> index 0 of the array
index high --> last index of the array i.e. index 4
S[] --> this is the array
number x --> number we are searching let's suppose 17
---------------------------------------------------------------------------------------------------------------------------------
Now , we check if index low is less than high, we calculate mid point
mid = (low + high ) / 2
mid = (0 + 4 ) / 2 = 2
mid = 2
S[mid] = 7
Now , we check if S[mid] == x, we get the result
Otherwise, if x is less than S[mid], then we pass the array from start the one less to the mid
else, if x is greater than S[mid[, then we pass the array from mid+1 to the end.
Just we do it recursively, untill we get the number x in array or the index low becomes greater than high
Divide and Conquer & Algorithm Design 5. (20 points) Consider the following algorithm Precondition: S is...
6.3.1 [10] <§6.2> Consider the following binary search algorithm (a classic divide and conquer algorithm) that searches for a value X in a sorted N-element array A and returns the index of matched entry: BinarySearch(A[0..N−1], X) { low = 0 high = N −1 while (low <= high) { mid = (low + high) / 2 if (A[mid] >X) high = mid −1 else if (A[mid] <X) low = mid + 1 else return mid // found } return −1...
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.
Please solve and give reasoning
10. Consider the following instance variable and method. private int[l arr; /**Precondition: arr contains no duplicates; the elements in arr are in sorted order aparam low 0 slow S arr.length aparam high low - 1 s high < arr.length @param num public int mystery (int low, int high, int num) int mid(low +high) 2i if (low > high) return low else if (arr [mid] < num) return mystery (mid + 1, high, num) else if...
Show that the time complexity for the
number of assignments of records for the Mergesort algorithm
(Algorithms 2.2 and 2.4) is approximated by T (n)
= 2nlgn.
by Mingfu LI, CGUEE Algorithms Algorithms 2.2 : Mergesort O Problem Sort n keys in nondecreasing sequence. Inputs positive integer n, array of keys S index from 1 to n Output the array 5 containing the keys in nondecreasing sequence. void mergesort (int n, keytype S[]) (1 <u)и } keytype UI..h), ИI.m); copy...
Using divide and conquer algorithm approach on a binary tree, show how the following list of elements in an array can be sorted in ascending order: 38, 27,43,3,9,82,10
Show that the time complexity for the
number of assignments of records for the Mergesort algorithm
(Algorithms 2.2 and 2.4) is approximated by T (n)
= 2nlgn.
by Mingfu LI, CGUEE Algorithms Algorithms 2.2 : Mergesort O Problem Sort n keys in nondecreasing sequence. Inputs positive integer n, array of keys S index from 1 to n Output the array 5 containing the keys in nondecreasing sequence. void mergesort (int n, keytype S[]) (1 <u)и } keytype UI..h), ИI.m); copy...
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"...
Consider the following: Algorithm 1 Smallest (A,q,r) Precondition: A[ q, ... , r] is an array of integers q ≤ r and q,r ∈ N. Postcondition: Returns the smallest element of A[q, ... , r]. 1: function Smallest (A , q , r) 2: if q = r then 3: return A[q] 4: else 5: mid <--- [q+r/2] 6: return min (Smallest(A, q, mid), Smallest (A, mid + 1, r)) 7: end if 8: end function (a) Write a recurrence...
Design a divide-and-conquer algorithm in pseudocode for computing the number of levels in a binary tree. In particular, your algorithm must return 0 and 1 for the empty and single-node trees, respectively. What is the time efficiency class of your algorithm?
Design a divide-and-conquer algorithm for computing the number of levels in a binary tree. In particular, the algorithm should return 0 and 1 for the empty and single-node trees respectively. Please provide the pseudocode for your algorithm. What is the running time of your algorithm in the worst case using O() notation? Design a divide-and-conquer algorithm for computing the number of levels in a COMPLETE binary tree. In particular, the algorithm should return 0 and 1 for the empty and...