Searching Algorithms: Analysis & Applications

Efficiency comparison

AlgorithmTimeSpaceScenario
SequentialO(n)O(n)O(1)O(1)Unordered/small
BlockO(n)O(\sqrt{n})O(n)O(\sqrt{n})Large table, frequent
BinaryO(log⁡2n)O(\log_2 n)O(1)O(1)Sorted list
HashO(1)O(1) (amort.)O(n)O(n)Very frequent / big data
B/B+ TreeO(log⁡mn)O(\log_m n)O(n)O(n)Disk/DB index
String matchingO(m+n)O(m+n) (KMP)O(n)O(n)Text/sequence

Search algorithm efficiency comparison demo

This placeholder can be extended with comparison charts of search algorithm efficiency and space complexity.

  • Sequential search: O(n)
  • Block search: O(√n)
  • Binary search: O(log₂ n)
  • Hash table search: O(1)
  • B-tree / B+ tree search: O(logₘ n)
  • String pattern matching (KMP): O(m+n)

Space & difficulty

  • Sequential/Binary: simple, tiny space
  • Hash/B-Tree: extra structures, higher space, more complex

Scenario mapping

  • Small, unordered: sequential
  • Large, frequent lookups: hash, B/B+
  • Sorted list: binary
  • Range queries: B+
  • Strings/sequences: KMP, etc.

Exercises

Exercise 1

Compare sequential, binary, and hash search (time + scenarios).

Sequential SearchBinary SearchHash Table

Approach: Compare the efficiency and the applications.

Steps:

  1. Sequential O(n)O(n), for unordered or small data
  2. Binary O(log⁡2n)O(\log_2 n), for sorted lists
  3. Hash O(1)O(1), for large data or frequent lookups

Answer: Sequential search for small tables, binary search for sorted lists, hash search for large data.

Exercise 2

Which structure suits frequent lookups and range queries, and why?

B+ TreeRange QueryFrequent Lookup

Approach: Range queries plus frequent lookups point to a B+ tree.

Steps:

  1. For frequent lookups together with range queries, the B+ tree is the best choice
  2. Its linked leaf list makes range queries easy

Answer: A B+ tree.

Past exam

2021·408

Analyse hash search versus B-Tree search on big data.

Hash TableB-TreeRange Query

Approach: Equality search versus range search; space versus disk friendliness.

Steps:

  1. Hash search is fast and good for equality lookups, but it uses more space and is weak at range queries
  2. The B-tree is a little slower, but disk friendly and it supports range queries

Answer: Hash search is fast but not suitable for range queries; the B-tree suits disk storage and range queries.

Found an issue with this content?

Tell us what could be improved.