Top Banner Ad

BCS301 — Unit 3: Searching

Table of Contents (24 sections)

BCS301 · DATA STRUCTURE · Unit 3

Searching

Unit Scope & Exam Weightage: Unit 3 covers searching techniques and sorting algorithms: sequential search, index sequential search, binary search, hashing and collision resolution, insertion sort, selection sort, bubble sort, quick sort, merge sort, heap sort, and radix sort. AKTU questions commonly test hashing/collision resolution, insertion and merge sort with tracing, binary search algorithm/program, selection sort complexity, and quick comparison of sorting/searching methods.


Front Page: Importance-Wise Topic Roadmap (PYQ Frequency × Marks)

Priority Topic (Exact Syllabus) PYQs Asked Marks Weightage Tag
1 Concept of Hashing & Collision resolution Techniques used in Hashing 2 (2023-24) 7m + 2m = 9m Survival Mode Predicted
2 Sorting: Insertion Sort 1 (2025-26) 7m Survival Mode
3 Binary Search 1 (2023-24) 7m Core Concept
4 Selection 1 (2023-24) 7m Core Concept
5 Merge Sort 1 (2024-25) 7m Core Concept
6 Index Sequential Search 1 (2024-25) 2m Core Concept
7 Quick Sort 1 (2023-24) 2m Core Concept
8 Concept of Searching, Sequential search integrated 2m/7m support Quick Scoring
9 Bubble Sort, Heap Sort and Radix Sort 0 direct — Supporting

2-Hour Last-Night Survival Checklist:

  1. Hashing: Write hash function h(k)=kmod  mh(k)=k \mod m, define collision, and compare linear probing, quadratic probing, double hashing, and chaining with one example.
  2. Sorting traces: Practice insertion sort and merge sort step-by-step on the exact kind of array given in PYQs; write pass-wise output clearly.
  3. Binary search: State precondition (sorted data), algorithm, and complexity O(log⁡n)O(\log n) average/worst, O(1)O(1) best.
  4. Selection/bubble/quick: Know worst-case O(n2)O(n^2) for selection/bubble/quick (bad pivot), while merge/heap give O(nlog⁡n)O(n\log n).
  5. Index sequential search: Explain index table + block sequential search; useful for large ordered files.

1. Concept of Searching

Searching: Searching is the process of locating a particular element (key) in a collection of data. If the key is found, the search returns its position or record; otherwise it reports failure.

1.1 Searching Performance Factors

  • Size of data: Larger data needs efficient algorithms.
  • Organization: Sorted/unsorted, array/linked list/file/indexed structure.
  • Search frequency: Frequent searches justify indexing or hashing.
  • Time and space complexity: Best, average, and worst case.

Sequential search compares the key with each element one by one from start to end.

for i = 0 to n-1
    if A[i] == key return i
return -1
Case Comparisons Complexity
Best 1 O(1)O(1)
Average n/2n/2 O(n)O(n)
Worst nn O(n)O(n)

Advantages: Simple, works on unsorted data, no extra structure needed. Disadvantage: Slow for large datasets.

Index sequential search is used on large ordered records. An index table stores key values and block pointers (or starting addresses). The algorithm first searches the index to locate the appropriate block, then performs sequential search within that block.

  • Step 1: Divide file into blocks and create index entries like (block_max_key, block_address).
  • Step 2: Find block where key may exist by searching index.
  • Step 3: Search sequentially inside selected block.

Use case: Large disk/file-based records where full binary search is costly but data is partially indexed.

Examiner Tip: Write that index sequential search combines indexing and sequential search: faster than pure sequential search on large ordered files, but usually slower than binary search on fully random-access sorted arrays.

After-Topic PYQ (2024-25 Sec A Q1f, 2 Marks): Illustrate the concept of Indexed Sequential search.


Binary Search: Binary search is a divide-and-conquer searching technique that repeatedly divides a sorted search interval in half by comparing the key with the middle element.

2.1 Algorithm

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

2.2 Complexity and Behavior

T(n)=T(n/2)+c  ⟹  T(n)=O(log⁡2n)T(n)=T(n/2)+c \implies T(n)=O(\log_2 n)
Case Time Explanation
Best O(1)O(1) Key found at first middle.
Average O(log⁡n)O(\log n) Search space halves each step.
Worst O(log⁡n)O(\log n) Key absent or found at last split.

Average behavior: In each comparison, the remaining candidate list is halved. For nn elements, maximum comparisons are ⌊log⁡2n⌋+1\lfloor \log_2 n \rfloor + 1. This makes binary search efficient for large sorted arrays, but it is unsuitable for unsorted data and inefficient for linked lists because random middle access is not O(1)O(1).

After-Topic PYQ (2023-24 Sec C Q3b, 7 Marks): Write a program in C language to implement binary search algorithm. Also discuss the average behavior of the algorithm.


3. Hashing and Collision Resolution

Hashing: Hashing is a technique that maps keys to table indices using a hash function, enabling near constant-time average insertion, deletion, and search. A collision occurs when two different keys map to the same index.

3.1 Hash Functions

  • Division method: h(k)=kmod  mh(k)=k \mod m; choose mm preferably prime and not close to power of 2.
  • Multiplication method: h(k)=⌊m(kAmod  1)⌋h(k)=\lfloor m(kA \mod 1)\rfloor, 0<A<10<A<1.
  • Mid-square method: Square key and take middle digits.
  • Folding method: Split key into parts and add.

3.2 Collision Resolution Techniques

Open Addressing (Closed Hashing)

All keys stored in hash table itself.

Technique Probe Sequence Issue
Linear probing h(k,i)=(h(k)+i)mod  mh(k,i)=(h(k)+i)\mod m Primary clustering.
Quadratic probing h(k,i)=(h(k)+c1i+c2i2)mod  mh(k,i)=(h(k)+c_1i+c_2i^2)\mod m Reduces primary clustering; may cause secondary clustering.
Double hashing h(k,i)=(h1(k)+ih2(k))mod  mh(k,i)=(h_1(k)+i h_2(k))\mod m Best among open addressing; needs h2(k)h_2(k) non-zero.

Separate Chaining (Open Hashing)

Each table slot stores a linked list/chain of all keys hashed to that index. Collisions are handled by appending nodes.

Method Advantage Disadvantage
Chaining Simple, handles high load factor, easy deletion Extra pointer memory
Linear probing Cache friendly, no pointers Primary clustering
Quadratic probing Less primary clustering Secondary clustering, table must not be too full
Double hashing Less clustering More computation

3.3 Disadvantages of Linear Probing and Role of Quadratic Probing

Linear probing disadvantages:

  1. Primary clustering: Long contiguous occupied blocks form, increasing search time.
  2. Performance degrades as load factor increases.
  3. Unequal probe lengths: Keys hashing near a cluster get pulled into it.
  4. Deletion is tricky: Needs tombstone/marker to avoid breaking probe chains.

Quadratic probing improvement: Uses squared jumps (h(k)+i2)mod  m(h(k)+i^2)\mod m, so colliding keys do not fill adjacent slots linearly. This reduces primary clustering. However, it can cause secondary clustering (keys with same initial hash follow same probe sequence), and table size/parameters must be chosen carefully (often prime mm, load factor below 0.5).

After-Topic PYQ (2023-24 Sec C Q3a, 7 Marks): What are the disadvantages of linear probing in hashing? Discuss how quadratic probing can be used to solve some of these problems. After-Topic PYQ (2023-24 Sec A Q1e, 2 Marks): What are the 2 different forms of hashing?


4. Sorting Algorithms

Sorting: Sorting arranges data elements in ascending or descending order according to a key. Efficient sorting improves searching, merging, and reporting.

4.1 Insertion Sort

Insertion sort builds the sorted array one element at a time by inserting each new element into its correct position among previously sorted elements.

for(i=1;i<n;i++){
    key=A[i]; j=i-1;
    while(j>=0 && A[j]>key){ A[j+1]=A[j]; j--; }
    A[j+1]=key;
}

Complexity: Best O(n)O(n) (already sorted), Average/Worst O(n2)O(n^2), Space O(1)O(1), Stable.

4.2 Selection Sort

Selection sort repeatedly selects the minimum element from unsorted part and places it at beginning.

Complexity: Best/Average/Worst O(n2)O(n^2) because it still scans remaining elements to find minimum. Space O(1)O(1), not stable in basic form.

4.3 Bubble Sort

Repeatedly compares adjacent elements and swaps if out of order. After each pass, largest element bubbles to end. Best O(n)O(n) with swap flag, worst O(n2)O(n^2).

4.4 Quick Sort

Divide-and-conquer: choose pivot, partition array so left < pivot < right, recursively sort partitions. Average O(nlog⁡n)O(n\log n), worst O(n2)O(n^2) when pivot is consistently poor (e.g., already sorted array with first/last pivot). Space O(log⁡n)O(\log n) recursion average.

4.5 Merge Sort

Divide array into halves, recursively sort, then merge sorted halves.

T(n)=2T(n/2)+n  ⟹  O(nlog⁡n)T(n)=2T(n/2)+n \implies O(n\log n)

Space complexity O(n)O(n) for merging; stable.

4.6 Heap Sort and Radix Sort

  • Heap sort: Build max-heap, repeatedly extract maximum. Time O(nlog⁡n)O(n\log n), space O(1)O(1), not stable.
  • Radix sort: Non-comparison sort; processes digits/bits from least significant to most significant (LSD) using stable counting sort. Time O(d(n+k))O(d(n+k)), where dd digits/keys.
Algorithm Best Average Worst Space Stable
Insertion O(n)O(n) O(n2)O(n^2) O(n2)O(n^2) O(1)O(1) Yes
Selection O(n2)O(n^2) O(n2)O(n^2) O(n2)O(n^2) O(1)O(1) No
Bubble O(n)O(n) O(n2)O(n^2) O(n2)O(n^2) O(1)O(1) Yes
Quick O(nlog⁡n)O(n\log n) O(nlog⁡n)O(n\log n) O(n2)O(n^2) O(log⁡n)O(\log n) No
Merge O(nlog⁡n)O(n\log n) O(nlog⁡n)O(n\log n) O(nlog⁡n)O(n\log n) O(n)O(n) Yes
Heap O(nlog⁡n)O(n\log n) O(nlog⁡n)O(n\log n) O(nlog⁡n)O(n\log n) O(1)O(1) No
Radix O(d(n+k))O(d(n+k)) O(d(n+k))O(d(n+k)) O(d(n+k))O(d(n+k)) O(n+k)O(n+k) Yes

After-Topic PYQ (2023-24 Sec A Q1f, 2 Marks): How the choice of pivot element effects the running time of quick sort algorithm?


Solved Exam-Ready Questions

Solved Q1 (2023-24 Sec C Q3a, 7 Marks)

Q: What are the disadvantages of linear probing in hashing? Discuss how quadratic probing can be used to solve some of these problems.

Answer:

Linear probing: When collision occurs at h(k)h(k), try (h(k)+1)mod  m,(h(k)+2)mod  m,...(h(k)+1)\mod m,(h(k)+2)\mod m,...

Disadvantages:

  1. Primary clustering: Occupied slots form continuous clusters; any key hashing into cluster must traverse to end.
  2. Longer average search time as load factor grows.
  3. Insertion/search degradation: Clusters merge into bigger clusters.
  4. Deletion complexity: Removed slot needs special marker to preserve probe sequence.

Quadratic probing: Probe sequence h(k,i)=(h(k)+c1i+c2i2)mod  mh(k,i)=(h(k)+c_1i+c_2i^2)\mod m, commonly (h(k)+i2)mod  m(h(k)+i^2)\mod m. It jumps by 1,4,9,... slots, so keys do not fill consecutive positions around collision point. This reduces primary clustering and improves average performance compared to linear probing.

Limitation: Quadratic probing may cause secondary clustering and requires appropriate table size/load factor to find an empty slot.

Result: Quadratic probing alleviates primary clustering of linear probing but does not eliminate all clustering issues.


Solved Q2 (2025-26 Sec C Q3b, 7 Marks)

Q: Write the algorithm of Insertion sort and sort the following list of numbers using insertion sort: 70, 17, 30, 80, 55, 1, 29, 67, 15.

Solution:

Algorithm: For i=1i=1 to n−1n-1, set key=A[i], j=i-1; while j>=0 && A[j]>key, shift A[j+1]=A[j], j--; place A[j+1]=key.

Trace (ascending):

Pass Key Array after pass
1 17 17, 70, 30, 80, 55, 1, 29, 67, 15
2 30 17, 30, 70, 80, 55, 1, 29, 67, 15
3 80 17, 30, 70, 80, 55, 1, 29, 67, 15
4 55 17, 30, 55, 70, 80, 1, 29, 67, 15
5 1 1, 17, 30, 55, 70, 80, 29, 67, 15
6 29 1, 17, 29, 30, 55, 70, 80, 67, 15
7 67 1, 17, 29, 30, 55, 67, 70, 80, 15
8 15 1, 15, 17, 29, 30, 55, 67, 70, 80

Result: Sorted list = 1, 15, 17, 29, 30, 55, 67, 70, 80.


Solved Q3 (2024-25 Sec C Q3b, 7 Marks)

Q: Demonstrate merge sort algorithm to sort the following elements in ascending order: 11, 16, 13, 11, 4, 12, 6, 7. What is the time and space complexity of merge sort?

Solution:

Divide: [11,16,13,11,4,12,6,7] → [11,16,13,11] and [4,12,6,7] → [11,16],[13,11],[4,12],[6,7] → [11],[16],[13],[11],[4],[12],[6],[7]

Merge: [11],[16] → [11,16]; [13],[11] → [11,13]; [4],[12] → [4,12]; [6],[7] → [6,7] [11,16],[11,13] → [11,11,13,16]; [4,12],[6,7] → [4,6,7,12] Final merge → [4,6,7,11,11,12,13,16]

Complexity: Time T(n)=2T(n/2)+n=O(nlog⁡n)T(n)=2T(n/2)+n=O(n\log n) in best/average/worst. Space O(n)O(n) auxiliary for merge.

Result: Sorted array = 4, 6, 7, 11, 11, 12, 13, 16.


Practice Questions (Self-Test)

  1. (2 Marks) Differentiate sequential search and binary search.
  2. (2 Marks) What are the two different forms of hashing? (open addressing and chaining).
  3. (7 Marks) Explain division method, linear probing, quadratic probing, and double hashing with a suitable example. Insert keys 23, 43, 13, 27 using h(k)=kmod  7h(k)=k\mod 7.
  4. (7 Marks) Write C program/algorithm for selection sort and discuss its worst-case time complexity.
  5. (7 Marks) Explain quick sort with pivot selection. Why can poor pivot choice make quick sort O(n2)O(n^2)?
  6. (7 Marks) Compare bubble, insertion, selection, merge, heap, and radix sort on time, space, and stability.