Unit 4: Searching and sorting
Data Structures-II notes · PTU syllabus (UGCC2510)
On this page
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
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
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;
}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.
- 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.
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.
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
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.
Topic 5
Comparative study
| Algorithm | Best | Average | Worst | Stable? |
|---|---|---|---|---|
| Linear search | O(1) | O(n) | O(n) | – |
| Binary search | O(1) | O(log n) | O(log n) | – |
| Bubble sort | O(n) | O(n²) | O(n²) | Yes |
| Selection sort | O(n²) | O(n²) | O(n²) | No |
| Insertion sort | O(n) | O(n²) | O(n²) | Yes |
| Quick sort | O(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
- Q1.Differentiate between linear and binary search.
- Q2.What is hashing?
- Q3.What is a collision? Name two resolution techniques.
- Q4.What is linear probing?
- Q5.What is a pivot in quick sort?
- Q6.Which sorting algorithm is best for nearly sorted data?
Long-answer questions
- Q1.Explain binary search with an algorithm and a worked example.
- Q2.Explain hash functions and collision resolution techniques in detail.
- Q3.Sort 25, 10, 35, 5, 30, 15 using bubble, selection and insertion sort, showing each pass.
- 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.
