---
title: "Binary search for 54 in sorted data"
subject: "Data Structure and Algorithm"
chapter: 8
key: "semester_4/data_structure_and_algorithm:binary-search-54"
source: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/binary-search-54
site: BCT Engineering
---

# Binary search for 54 in sorted data

Data Structure and Algorithm · Chapter 8: Searching

- Asked in: 2069 Poush, 4+4 marks
- Video: [Binary Search examples | Successful Search | DAA | Lec-13 | Bhanu Priya](https://www.youtube.com/watch?v=81QLBCW94Oo) — Education 4u
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/binary-search-54
- Whole chapter: https://www.bctengineer.com/md/semester_4/data_structure_and_algorithm/chapter-8

## Question

Write down the algorithm for binary search. Show steps to search 54 from the given data 8, 15, 36, 40, 41, 54, 62 using binary search.

## Short answer

Repeatedly compare the key with the middle element, discarding half. For 8,15,36,40,41,54,62: mid=3 (40) → low=4; mid=5 (54) → found at index 5 in 2 comparisons.

## Answer

Binary search finds a key in a **sorted** array by repeatedly comparing it with the middle element and discarding the half that cannot contain it.

#### Algorithm
```text
BINARYSEARCH(a, n, key)
1. low = 0, high = n - 1
2. while low <= high
3.     mid = (low + high) / 2
4.     if key == a[mid]  return mid        // found
5.     else if key < a[mid]  high = mid - 1  // search left half
6.     else  low = mid + 1                 // search right half
7. return -1                               // not found
```

```c
int binarySearch(int a[], int n, int key) {
    int low = 0, high = n - 1;
    while (low <= high) {
        int mid = (low + high) / 2;
        if (a[mid] == key) return mid;
        else if (key < a[mid]) high = mid - 1;
        else low = mid + 1;
    }
    return -1;
}
```

#### Search 54 in 8, 15, 36, 40, 41, 54, 62 (index 0 to 6)
| Step | low | high | mid | a[mid] | Decision |
|---|---|---|---|---|---|
| 1 | 0 | 6 | 3 | 40 | 54 > 40, so low = 4 |
| 2 | 4 | 6 | 5 | 54 | equal, found |

**Answer: 54 is found at index 5 (position 6) in 2 comparison steps.**

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. For 8, 15, 36, 40, 41, 54, 62 (index 0 to 6), what is the first mid?
   A. 2
   B. 3
   C. 4
   D. 6
   Answer: B. (0+6)/2 = 3. (Key point: First mid)

2. 54 > a[3] = 40; what next?
   A. high = 2
   B. low = 4
   C. stop
   D. low = 3
   Answer: B. Search the right half. (Key point: Decision)

3. Where is 54 found?
   A. Index 5 in 2 steps
   B. Index 4 in 1 step
   C. Index 6 in 3 steps
   D. Not found
   Answer: A. Second mid is (4+6)/2 = 5. (Key point: Result)

## Related in Data Structure and Algorithm

- [Binary search with algorithm and example](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Abinary-search-algorithm-example) — Data Structure and Algorithm, chapter 8, asked 4×
- [Binary search for 89; hash functions](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Abinary-search-89-hashing) — Data Structure and Algorithm, chapter 8
- [Complexity of binary search](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Abinary-search-complexity) — Data Structure and Algorithm, chapter 8
- [Sequential vs binary search](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Asequential-vs-binary-search) — Data Structure and Algorithm, chapter 8
- [BST properties and recursive search](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Abst-properties-recursive-search) — Data Structure and Algorithm, chapter 6
- [Binary tree traversals; preorder of BST](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Abinary-tree-traversal-methods-preorder) — Data Structure and Algorithm, chapter 6

## Related in other subjects

- [Rankine's data, 8° curve, 187.62 m long chord](https://www.bctengineer.com/question.md?key=semester_4%2Fsurveying_ii%3Asimple-curve-8deg-18762-long-chord) — Surveying II, chapter 6
- [Muskingum routing, K=36 hr, x=0.15](https://www.bctengineer.com/question.md?key=semester_5%2Fengineering_hydrology%3Amuskingum-k36-x0-15) — Engineering Hydrology, chapter 7
- [IC manufacturing steps; top-down F = xz + y](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Aic-manufacturing-steps-topdown-xz-y) — Embedded System, chapter 8
- [Display records sorted by name from file](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-sorted-records-by-name) — Computer Programming, chapter 10
