MCS-208 · Unit 9

Searching and Sorting Techniques

Section 3 of 9

Block 3 · Unit 9Searching and Sorting Techniques9 min · Section 3 of 9

9.3Applications of Searching

Covers IGNOU MCS-208 §9.4

Searching is at the heart of many everyday programs. Real applications rarely use plain linear search on large data; they choose a data structure that makes searching fast. This page looks at the examples in the study material and the structures behind them.

Spell checkers

A spell checker looks up each word of a document in a dictionary; a word not found is flagged. A dictionary has hundreds of thousands of words and a document many thousands, so the look-up must be fast.

Search engines

A web search engine uses programs called crawlers (robots, spiders) that follow links from page to page and download documents. The words of every page are put into an inverted index: for each word, a list of the pages that contain it. A query is answered by looking up the query words in this index (with hashing or search trees) and combining their page lists, never by scanning the web page by page.

String pattern matching

Finding all occurrences of a word in a text (the “Find” command, grep, DNA analysis) is string matching. The naive method tries every starting position and costs O(nm)O(nm) for a text of length nn and a pattern of length mm; algorithms such as Knuth–Morris–Pratt work in O(n+m)O(n + m). To search many strings at once, tries and suffix trees store all strings so that a look-up takes time proportional to the length of the word, not to the number of words.

Databases

A database query such as “find the customer with ID 41725” uses an index, usually a B-tree (Unit 7) or a hash table, so it reads only a few disk blocks instead of scanning the whole table.

Key takeaways

  • Spell checkers look words up with hashing, tries or binary search, not linear search.
  • Search engines answer queries from an inverted index built by crawlers.
  • String matching finds patterns in text; tries and suffix trees speed up multi-string search.
  • Databases use B-tree or hash indexes.

Practice questions

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

Question 1 · multiple choice

Which structure lets a spell checker look up a word in about one probe on average?

Show answer

Answer: B. A hash table

Hashing computes the position directly (Unit 10). An unsorted array or list needs linear search.

Question 2 · multiple choice

What does a search engine’s inverted index store?

Show answer

Answer: C. For each word, the list of pages that contain it

Looking up a query word gives its page list at once; the lists of several words are combined.

Question 3 · multiple choice

Binary search on a sorted list of 1,000,000 words needs at most how many probes per word?

Show answer

Answer: B. 20

⌊log⁡2106⌋+1=19+1=20\lfloor \log_2 10^6 \rfloor + 1 = 19 + 1 = 20, since 219=524,2882^{19} = 524{,}288 and 220=1,048,5762^{20} = 1{,}048{,}576.

Question 4 · multiple choice

Which is the best choice to find a value once in an unsorted array of 30 elements?

Show answer

Answer: D. Linear search

Any preparation costs more than the at most 30 comparisons of a linear search.

Question 5 · multiple choice

What does this program print?

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

int main(void)
{
    long words = 10000, dict = 100000;
    long linear = words * dict;            /* worst case */
    long binary = 0;
    for (long n = dict; n > 0; n /= 2)     /* floor(log2 dict) + 1 */
        binary++;
    binary *= words;
    printf("%ld %ld\n", linear, binary);
    return 0;
}
Show answer

Answer: D. 1000000000 170000

Linear: 104×105=10910^4 \times 10^5 = 10^9 comparisons. Binary: 17 probes per word (100,000 halves 17 times before reaching 0), so 170,000.

Question 6 · short answer

Explain how searching is used in a spell checker and in a search engine, and name the data structures that make each fast.

Show worked answer

A spell checker reads the document word by word and looks each word up in a dictionary of valid words; words not found are flagged as misspelt. To be fast, the dictionary is stored in a hash table (about one probe per word), a trie (time proportional to the word’s length) or a sorted array searched with binary search (about log⁡2n\log_2 n probes). A search engine uses crawlers to collect web pages and builds an inverted index that maps each word to the list of pages containing it; a query looks up its words in this index (using hashing or search trees) and combines the lists, so the engine never scans the pages themselves at query time.

↑ ↓ move↵ openTab switch type