Unit 3 of 4 · M.Sc IT Sem 2

Unit 3: Searching, hashing and sorting

Data Structures notes · PTU syllabus (PGCA1913)

6 min read11 topics10 exam questions
On this page
  1. Unit summary
  2. Sequential and binary search
  3. Indexed sequential search
  4. Interpolation search
  5. Hashing and hash functions
  6. Collision resolution and chaining
  7. Bubble, selection and insertion sort
  8. Quick sort
  9. Merge sort: contiguous and linked
  10. Shell sort
  11. Heap sort and tree sort
  12. Comparison of searching and sorting algorithms
  13. Key terms
  14. Quick revision
  15. Important questions

Unit summary

Searching, hashing and sorting are among the most frequently used algorithms. This unit covers sequential, binary, indexed sequential and interpolation search, hashing and collision resolution with chaining, and internal sorting — bubble, selection, insertion, quick, merge, shell, heap and tree sort.

After this unit you can

  • Implement and compare searching methods
  • Explain hashing and collision resolution
  • Implement simple and advanced sorting algorithms
  • Compare sorting algorithms by complexity and stability

PTU syllabus topics

  • Sequential/binary/indexed sequential/interpolation search
  • hashing basics
  • collision resolution and chaining
  • internal sorting — bubble
  • selection
  • insertion
  • quick
  • merge (linked and contiguous)
  • shell
  • heap and tree sort
ComparisonSorting algorithms
Average time
Worst time

Bubble / selection / insertion

O(n²)

O(n²)

Quick sort

O(n log n)

O(n²)

Merge sort

O(n log n)

O(n log n)

Heap sort

O(n log n)

O(n log n)

Shell sort

About O(n^1.3)

O(n²)

1

Topic 1

Sequential and binary search

  • Linear search: check each element one by one until the key is found or the list ends. Works on unsorted data. Best O(1), worst O(n).
  • Binary search: works only on sorted data. Compare the key with the middle element; search the left or right half; repeat. Worst case O(log n).
cint 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;
}
2

Topic 2

Indexed sequential search

  • Records are sorted and divided into blocks; an index table holds the largest (or first) key of each block and its address. Search the index to find the block, then search sequentially within it.

Example

1,000 sorted records in 10 blocks of 100: search the 10-entry index, then at most 100 records — about 110 comparisons instead of 1,000.

3

Topic 3

Interpolation search

  • Like binary search on sorted data, but estimates the probe position from the key value — good for uniformly distributed keys.
Key formulasInterpolation search
  • Probe position

    pos = low + (key − a[low]) × (high − low) ÷ (a[high] − a[low])

  • Complexity

    O(log log n) average for uniform data; O(n) worst case

Example

a = 10, 20, 30, …, 100 (indices 0–9), key 70: pos = 0 + (70 − 10) × 9 ÷ 90 = 6 → found in one probe.

4

Topic 4

Hashing and hash functions

Hashing stores and finds a record directly by computing its position from its key, giving average O(1) search. A hash function h(k) maps a key to an index in a hash table.

Key termsCommon hash functions
Division method
h(k) = k mod m (choose m prime)
Mid-square method
Square the key and take middle digits
Folding method
Split the key into parts and add them
Digit analysis
Use selected digits of the key

Example

With m = 10, the division method puts key 47 at index 7 and key 123 at index 3.

5

Topic 5

Collision resolution and chaining

A collision happens when two keys hash to the same index.

  • Open addressing (store in the table itself): linear probing (try next slots: h, h + 1, h + 2 …), quadratic probing (h + 1², h + 2² …) and double hashing (a second hash function decides the step).
  • Chaining: each table slot points to a linked list of all keys that hash there.
ComparisonOpen addressing vs chaining
Open addressing
Chaining

Storage

Inside the table

Linked lists outside the table

Load factor

Must stay below 1

Can exceed 1

Problem

Clustering in linear probing

Extra memory for pointers

Deletion

Tricky (needs markers)

Easy

6

Topic 6

Bubble, selection and insertion sort

cvoid bubble(int a[], int n) {
    for (int i = 0; i < n - 1; i++)
        for (int j = 0; j < n - 1 - i; j++)
            if (a[j] > a[j + 1]) { int t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; }
}
void insertion(int a[], int n) {
    for (int i = 1; i < n; i++) {
        int key = a[i], j = i - 1;
        while (j >= 0 && a[j] > key) { a[j + 1] = a[j]; j--; }
        a[j + 1] = key;
    }
}
void selection(int a[], int n) {
    for (int i = 0; i < n - 1; i++) {
        int min = i;
        for (int j = i + 1; j < n; j++) if (a[j] < a[min]) min = j;
        int t = a[i]; a[i] = a[min]; a[min] = t;
    }
}
ComparisonSimple sorting algorithms
Idea
Time complexity (best, worst)

Bubble sort

Repeatedly swap adjacent out-of-order pairs

O(n) with early exit, O(n²)

Insertion sort

Insert each element into the sorted part

O(n), O(n²) — good for nearly sorted data

Selection sort

Select the minimum and place it in position

O(n²), O(n²) — fewest swaps

Example

Insertion sort on 5, 2, 4, 1: after passes → 2 5 4 1 → 2 4 5 1 → 1 2 4 5.

7

Topic 7

Quick sort

cint partition(int a[], int low, int high) {
    int pivot = a[high], i = low - 1;               /* Lomuto partition */
    for (int j = low; j < high; j++)
        if (a[j] <= pivot) { i++; int t = a[i]; a[i] = a[j]; a[j] = t; }
    int t = a[i + 1]; a[i + 1] = a[high]; a[high] = t;
    return i + 1;
}
void quickSort(int a[], int low, int high) {
    if (low < high) {
        int p = partition(a, low, high);
        quickSort(a, low, p - 1);
        quickSort(a, p + 1, high);
    }
}
  • Average O(n log n); worst O(n²) with bad pivots; in-place; not stable. Median-of-three or random pivots avoid the worst case.
8

Topic 8

Merge sort: contiguous and linked

cvoid merge(int a[], int l, int m, int r) {
    int n1 = m - l + 1, n2 = r - m, L[n1], R[n2];
    for (int i = 0; i < n1; i++) L[i] = a[l + i];
    for (int j = 0; j < n2; j++) R[j] = a[m + 1 + j];
    int i = 0, j = 0, k = l;
    while (i < n1 && j < n2) a[k++] = (L[i] <= R[j]) ? L[i++] : R[j++];
    while (i < n1) a[k++] = L[i++];
    while (j < n2) a[k++] = R[j++];
}
void mergeSort(int a[], int l, int r) {
    if (l < r) { int m = (l + r) / 2; mergeSort(a, l, m); mergeSort(a, m + 1, r); merge(a, l, m, r); }
}
  • O(n log n) in all cases, stable; the contiguous version needs O(n) extra space; the linked-list version splits the list with slow and fast pointers and merges by relinking nodes, needing no extra array — the preferred sort for linked lists.
9

Topic 9

Shell sort

  • Insertion sort on elements a gap apart, with the gap reduced each pass (n/2, n/4, …, 1). Early passes move elements long distances, so the final insertion sort has little to do.

Example

35, 33, 42, 10, 14, 19, 27, 44 with gap 4: compare (35, 14), (33, 19), (42, 27), (10, 44) → 14, 19, 27, 10, 35, 33, 42, 44; then gap 2, then gap 1.

  • Complexity depends on the gap sequence — about O(n^1.5) with Knuth's sequence; not stable.
10

Topic 10

Heap sort and tree sort

A heap is a complete binary tree that satisfies the heap property:

  • Max-heap: every parent ≥ its children; the largest value is at the root.
  • Min-heap: every parent ≤ its children; the smallest value is at the root.

Heaps are stored in arrays and are used for priority queues and heap sort.

ProcessHeap sort
  1. 1Build a max-heap from the array
  2. 2Swap the root with the last element

    Largest goes to the end

  3. 3Reduce heap size by one
  4. 4Heapify the root

    Restore the heap property

  5. 5Repeat until one element remains

Heap sort runs in O(n log n) time in all cases.

  • Tree sort: insert all elements into a binary search tree, then output an inorder traversal — O(n log n) on average, O(n²) if the tree becomes skewed (sorted input) unless a balanced tree is used.
11

Topic 11

Comparison of searching and sorting algorithms

AlgorithmBestAverageWorstStable?
Linear searchO(1)O(n)O(n)–
Binary searchO(1)O(log n)O(log n)–
Bubble sortO(n)O(n²)O(n²)Yes
Selection sortO(n²)O(n²)O(n²)No
Insertion sortO(n)O(n²)O(n²)Yes
Quick sortO(n log n)O(n log n)O(n²)No

Exam tip

Quick sort's worst case O(n²) happens when the pivot is always the smallest or largest (for example an already sorted array with the first element as pivot).

AlgorithmBestAverageWorstStable?
Merge sortO(n log n)O(n log n)O(n log n)Yes
Heap sortO(n log n)O(n log n)O(n log n)No
Shell sortO(n log n)About O(n^1.3)O(n²)No
Tree sortO(n log n)O(n log n)O(n²)Yes (if equal keys go right)

Key terms

Interpolation search
Search estimating position from key values
Hash function
Function mapping keys to table indexes
Chaining
Collision resolution using linked lists at each index
Divide and conquer
Splitting a problem, solving parts and combining
Stable sort
Sort keeping equal keys in original order

Quick revision

  • Sequential O(n); binary O(log n); indexed sequential; interpolation O(log log n) average.
  • Division, mid-square, folding hash functions; linear and quadratic probing, double hashing, chaining; load factor.
  • Bubble, selection, insertion O(n²).
  • Quick (partition), merge (contiguous and linked), shell (gaps), heap, tree sort.
  • Complexities and stability table.

Important exam questions

Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).

Short-answer questions

  1. Q1.When is interpolation search better than binary search?
  2. Q2.What is indexed sequential search?
  3. Q3.Distinguish open addressing and chaining.
  4. Q4.Why is quick sort's worst case O(n²)?
  5. Q5.Why is merge sort preferred for linked lists?
  6. Q6.What is the idea of shell sort?

Long-answer questions

  1. Q1.Explain searching techniques with complexities.
  2. Q2.Explain hashing and collision resolution techniques.
  3. Q3.Explain quick sort and merge sort with examples.
  4. Q4.Explain shell, heap and tree sort and compare sorting algorithms.

Stuck on this unit?

Message SBS on WhatsApp for help with Data Structures, or to ask about studying M.Sc IT at Synetic.

WhatsApp us