Manually run the merge sort algorithm on the array 100 10 90
Manually run the merge sort algorithm on the array {100, 10, 90, 20, 80, 30, 70, 40, 60, 50}
Question 5: What is Binary Search Algorithm discuss with its complexity?
The first quetione have answered with sorting method suppose with merge follw the divide way. the second i need more clear and ideal answer please
Solution
Question 5. What is binary search algorithm, discuss with its complexity?
Binary search is the concept of search the key from an array, by dividing the array into 2 halves.
Suppose, there are 6 elements in the array and if you\'re searching for an element in that array.
The array indices start from 0 and will go upto 5.
Each time a mid value is calculated, using the formula (low + high) / 2 i.e., (0 + 5) / 2 = 2.
Now, the element at the index position 2 in the array is compared with the key element.
The 3 possible cases here are:
1. The search key and the element at index 2 are the same. Which means the element is found, and stop the algorithm.
2. The search key is less than the element at index 2. Which means the element will be only on the left side of mid. So, now run the algorithm again, with low value being the same 0, and high value updated to mid - 1, i.e., 1.
3. The search key is greater than the element at index 2. Which means the element will be only on the right side of mid. So, now run the algorithm again, with high value being the same 5, and low value updated to mid + 1, i.e., 3.
And the BinarySearch algorithm is reapplied with the new low, high index values.
Observe here that, everytime the array is being halved, and therefore, the efficiency of the algorithm is: O(log n).
