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...
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...
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...
7. Explain Dynamic Program ming algorithm in contrast to Divide and Conquer algorithm Discuss the advantages of Dynamic Programming over the other iophs method. 5pts) Then find the LCS of the following two strings X ABCBDAB) and Y- (BDCABA) (Explain the algorit g two strings. (He pts) thm as well 8. a) Explain the difference between recursive and iterative algorithms.(2 pts) b) The recursive Euclid algorithm is given as below: int GCD(int a, int b) f (b0) return a else...
Java program: Convex Hull using Divide and Conquer Algorithm A convex hull is the smallest convex polygon containing all the given points. Input is an array of points specified by their x and y coordinates. The output is the convex hull of this set of points. Examples: Input : points[] = {(0, 0), (0, 4), (-4, 0), (5, 0), (0, -6), (1, 0)}; Output : (-4, 0), (5, 0), (0, -6), (0, 4) use Divide and Conquer Algorithm please explain...