solve using binary search write stepwise procedure for given array - 23,12,5,29,10,65,55,70​

1 answer

To solve this using binary search, we need to follow these steps:

1. Sort the given array in ascending order: 5, 10, 12, 23, 29, 55, 65, 70
(The sorted array will help us perform the binary search operation efficiently.)

2. Initialize two variables, 'start' and 'end', as the indices of the first and last elements in the sorted array, respectively.
- 'start' = 0
- 'end' = length of the array - 1

3. Calculate the middle index as (start + end) / 2.
- 'mid' = (0 + 7) / 2 = 3

4. Check if the element at the middle index is equal to the target value (let's say 'target' is the item we are searching for). In this case, the target value is unknown, so let's assume 'target' as 23.
- If arr[mid] == target, we have found the element. Return the index (mid).
- If arr[mid] > target, the target element must lie in the left half of the array. Update the 'end' value as mid - 1.
- If arr[mid] < target, the target element must lie in the right half of the array. Update the 'start' value as mid + 1.

5. Repeat steps 3-4 until the start index becomes greater than the end index, signifying that the target element is not present in the array.

Let's apply these steps to find the index of 23 in the given array [23, 12, 5, 29, 10, 65, 55, 70]:

Step 1: Sort the array => [5, 10, 12, 23, 29, 55, 65, 70]

Step 2: Initialize 'start' = 0, 'end' = 7

Step 3: Calculate 'mid' = (0+7)/2 = 3

Step 4: Check arr[mid] = arr[3] = 23
Since arr[mid] = 23, we have found the target element. Return the index 3.

Therefore, the target element 23 is found at index 3 in the array.