0% found this document useful (0 votes)
5 views

Binary search is an efficient algorithm for finding an element within a sorted array

The document explains the binary search algorithm for finding a target element in a sorted array, using the example of locating the number 23 in the array {2, 5, 8, 12, 16, 23, 38, 56, 72, 91}. It details the step-by-step process of initializing pointers, calculating the middle index, and adjusting the search range based on comparisons. A code snippet in C++ is also provided to implement the binary search algorithm.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Binary search is an efficient algorithm for finding an element within a sorted array

The document explains the binary search algorithm for finding a target element in a sorted array, using the example of locating the number 23 in the array {2, 5, 8, 12, 16, 23, 38, 56, 72, 91}. It details the step-by-step process of initializing pointers, calculating the middle index, and adjusting the search range based on comparisons. A code snippet in C++ is also provided to implement the binary search algorithm.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Binary search is an efficient algorithm for finding an element within a sorted array.

Let’s walk through


the steps to find the target element 23 in the array `arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91}`:

1. Initialize two pointers: `left` at the start (index 0) and `right` at the end (index 9) of the array.

2. Calculate the middle index: `middle = left + (right - left) / 2`. In the first iteration, `middle = 0 + (9 - 0) /
2 = 4`.

3. Compare `arr[middle]` with the target value 23:

- If `arr[middle]` is equal to the target, return the index.

- If `arr[middle]` is less than the target, set `left = middle + 1` and repeat from step 2.

- If `arr[middle]` is greater than the target, set `right = middle - 1` and repeat from step 2.

Here's the step-by-step process:

- Initial array: `{2, 5, 8, 12, 16, 23, 38, 56, 72, 91}`

- Target: `23`

1. Initial indices: `left = 0`, `right = 9`

2. Calculate `middle = 0 + (9 - 0) / 2 = 4`

- `arr[4] = 16`, which is less than 23. Set `left = 4 + 1 = 5`

3. Now, `left = 5`, `right = 9`

4. Calculate `middle = 5 + (9 - 5) / 2 = 7`

- `arr[7] = 56`, which is greater than 23. Set `right = 7 - 1 = 6`

5. Now, `left = 5`, `right = 6`

6. Calculate `middle = 5 + (6 - 5) / 2 = 5`

- `arr[5] = 23`, which is equal to the target.

Thus, the target element `23` is found at index `5` in the array.

Would you like a code snippet to implement this in a specific programming language?
#include <iostream>

using namespace std;

int binarySearch(int arr[], int left, int right, int target) {

while (left <= right) {

int middle = left + (right - left) / 2;

// Check if target is present at mid

if (arr[middle] == target)

return middle;

// If target greater, ignore left half

if (arr[middle] < target)

left = middle + 1;

// If target is smaller, ignore right half

else

right = middle - 1;

// If target is not present in array

return -1;

int main() {

int arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91};

int n = sizeof(arr) / sizeof(arr[0]);

int target = 23;

int result = binarySearch(arr, 0, n - 1, target);


if (result != -1)

cout << "Element found at index " << result << endl;

else

cout << "Element not found in array" << endl;

return 0;

You might also like