MCS-208 · Unit 9

Searching and Sorting Techniques

Section 2 of 9

Block 3 · Unit 9Searching and Sorting Techniques10 min · Section 2 of 9

9.2Binary Search

Covers IGNOU MCS-208 §9.3

If the keys are sorted, we can do much better than checking them one by one. Binary search looks at the middle element and throws away half of the remaining array with a single comparison.

The algorithm

  1. low = 0, high = n - 1.
  2. While low <= high:
    • mid = (low + high) / 2 (integer division);
    • if key == k[mid]: report position mid and stop;
    • else if key < k[mid]: high = mid - 1;
    • else: low = mid + 1.
  3. Report “not found”.

Efficiency

Each probe halves the range, so after kk probes at most n/2kn/2^k elements remain. The worst case needs ⌊log⁡2n⌋+1\lfloor \log_2 n \rfloor + 1 probes: O(log⁡n)O(\log n).

Linear searchBinary search
Data must be sortedNoYes
Works on linked listsYesNo (needs O(1)O(1) access to the middle)
Comparisons (worst)nn⌊log⁡2n⌋+1\lfloor \log_2 n \rfloor + 1
OrderO(n)O(n)O(log⁡n)O(\log n)
Best forSmall or unsorted data, one-off searchesLarge sorted arrays searched many times

For an array sorted in descending order (Check Your Progress 2, Q2), only the direction of the comparison changes: if key > k[mid], search the left half (high = mid - 1), otherwise the right half (low = mid + 1); see binary_search_desc above.

Key takeaways

  • Binary search needs sorted data and random access (arrays).
  • Compare with the middle; go left (high = mid - 1) or right (low = mid + 1); stop when found or when low > high.
  • Worst case ⌊log⁡2n⌋+1\lfloor \log_2 n \rfloor + 1 probes: 10 for 1000 elements, 17 for 100,000.
  • For descending order, reverse the direction of the comparison.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What is the maximum number of probes binary search needs for a sorted array of 1000 elements?

Show answer

Answer: B. 10

⌊log⁡21000⌋+1=9+1=10\lfloor \log_2 1000 \rfloor + 1 = 9 + 1 = 10. The study material’s table says 11.

Question 2 · multiple choice

In a search with low = 6 and high = 11, which index is probed?

Show answer

Answer: C. 8

(6+11)/2=8(6 + 11)/2 = 8 with integer division.

Question 3 · multiple choice

The key is smaller than k[mid]. What is the next step in an ascending array?

Show answer

Answer: D. high = mid − 1

The key can only be to the left of mid, and mid itself has been checked. high = mid may cause an endless loop.

Question 4 · multiple choice

What does this program print?

C · q9-binary.c
#include <stdio.h>

int main(void)
{
    int k[] = {3, 8, 15, 21, 30, 42, 57, 64};
    int key = 42, low = 0, high = 7, probes = 0, pos = -1;
    while (low <= high) {
        int mid = (low + high) / 2;
        probes++;
        if (k[mid] == key) { pos = mid; break; }
        if (key < k[mid]) high = mid - 1; else low = mid + 1;
    }
    printf("pos=%d probes=%d\n", pos, probes);
    return 0;
}
Show answer

Answer: C. pos=5 probes=2

Probe 1: mid = 3 (21 < 42), low = 4. Probe 2: mid = (4 + 7)/2 = 5, k[5] = 42: found.

Question 5 · multiple choice

Why can binary search not be used efficiently on a linked list?

Show answer

Answer: C. Reaching the middle element of a linked list takes O(n)O(n) steps.

Binary search depends on jumping to the middle in O(1)O(1), which arrays allow and linked lists do not. (Skip lists, Unit 3, solve this.)

Question 6 · short answer

Write the binary search algorithm for an array sorted in descending order (Check Your Progress 2, Q2).

Show worked answer

low = 0, high = n − 1. While low ≤ high: mid = (low + high)/2; if key = k[mid], return mid (found, stop); else if key > k[mid], high = mid − 1 (larger keys are on the left in descending order); else low = mid + 1. After the loop, return “not found”. It still needs at most ⌊log⁡2n⌋+1\lfloor \log_2 n \rfloor + 1 probes.

↑ ↓ move↵ openTab switch type