Unit 4 of 4 · BCA Sem 3

Unit 4: Searching and sorting

Data Structures-II notes · PTU syllabus (UGCC2510)

3 min read5 topics10 exam questions
On this page
  1. Unit summary
  2. Linear and binary search
  3. Hashing and hash functions
  4. Collision resolution
  5. Sorting algorithms
  6. Comparative study
  7. Key terms
  8. Quick revision
  9. Important questions

Unit summary

Searching and sorting are the most common tasks in computing. This unit covers linear and binary search, hashing with hash tables, hash functions and collision resolution, and the sorting algorithms bubble, selection, insertion and quick sort, with a comparison of their efficiency.

After this unit you can

  • Write linear and binary search and state their complexity
  • Explain hashing, hash functions and collision resolution
  • Trace bubble, selection, insertion and quick sort
  • Compare searching and sorting algorithms

PTU syllabus topics

  • Linear and binary search
  • hashing
  • hash tables
  • hash function types
  • collision resolution (open addressing and chaining)
  • bubble/selection/insertion/quick sort
  • comparative study of searching and sorting algorithms
ComparisonSorting algorithms compared
Average time
Notes

Bubble sort

O(n²)

Simple; swaps adjacent elements

Selection sort

O(n²)

Fewest swaps

Insertion sort

O(n²)

Fast on nearly sorted data

Quick sort

O(n log n)

Worst case O(n²); very fast in practice

1

Topic 1

Linear 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

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.

3

Topic 3

Collision resolution

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

4

Topic 4

Sorting algorithms

  • Bubble sort: repeatedly swap adjacent elements that are in the wrong order; the largest "bubbles" to the end each pass.
  • Selection sort: find the smallest element and swap it into the first position, then the next smallest into the second, and so on.
  • Insertion sort: take each element and insert it into its correct place in the already-sorted left part — efficient for nearly sorted data.
  • Quick sort: choose a pivot, partition so smaller elements go left and larger go right, then sort both parts recursively (divide and conquer).

Example

Quick sort on 7, 2, 9, 4 with pivot 7: partition gives 2, 4 | 7 | 9; the parts are sorted to give 2, 4, 7, 9.

5

Topic 5

Comparative study

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).

Key terms

Binary search
Searching sorted data by repeatedly halving the range
Hash function
A function mapping a key to a table index
Collision
Two keys mapping to the same index
Chaining
Resolving collisions with linked lists at each slot
Pivot
The element used to partition the array in quick sort

Quick revision

  • Binary search needs sorted data: O(log n).
  • Division hash: k mod m; collisions by probing or chaining.
  • Bubble, selection, insertion: O(n²); quick sort: O(n log n) average.
  • Insertion sort is best for nearly sorted data.

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.Differentiate between linear and binary search.
  2. Q2.What is hashing?
  3. Q3.What is a collision? Name two resolution techniques.
  4. Q4.What is linear probing?
  5. Q5.What is a pivot in quick sort?
  6. Q6.Which sorting algorithm is best for nearly sorted data?

Long-answer questions

  1. Q1.Explain binary search with an algorithm and a worked example.
  2. Q2.Explain hash functions and collision resolution techniques in detail.
  3. Q3.Sort 25, 10, 35, 5, 30, 15 using bubble, selection and insertion sort, showing each pass.
  4. Q4.Explain quick sort with an example and analyse its complexity.

Stuck on this unit?

Message SBS on WhatsApp for help with Data Structures-II, or to ask about studying BCA at Synetic.

WhatsApp us