Finding information efficiently in a collection of data.
Total Duration: 4 Hours
Searching is the process of locating a specific element in a collection of data.
[10, 25, 33, 47, 59, 68]
Find: 47
| Roll No | Name |
|---|---|
| 101 | Ram |
| 102 | Sita |
| 103 | Hari |
How quickly can we find Roll No 103?
Check elements one by one.
Repeatedly divide the search space into half.
| Method | Best Case | Worst Case |
|---|---|---|
| Sequential Search | O(1) | O(n) |
| Binary Search | O(1) | O(log n) |
Faster searching becomes critical when datasets grow large.
Sequential Search (Linear Search) checks each element one by one until the target is found.
No sorting is required.
Array = [12, 25, 8, 41, 19, 33]
Target = 19
Start from the first element.
Compare each element with the target.
Stop when found or end of array reached.
| 12 | 25 | 8 | 41 | 19 | 33 |
Compare 12 ✗
Compare 25 ✗
Compare 8 ✗
Compare 41 ✗
Compare 19 ✓ Found
Array = [5, 9, 3, 7, 2]
Target = 7
| Step | Element | Result |
|---|---|---|
| 1 | 5 | Not Found |
| 2 | 9 | Not Found |
| 3 | 3 | Not Found |
| 4 | 7 | Found |
Step 1: Start
Step 2: Read array and target
Step 3: Compare target
with each element
Step 4: If match found
return position
Step 5: Otherwise continue
Step 6: If end reached
return NOT FOUND
Step 7: Stop
LINEAR_SEARCH(A, n, key)
for i = 0 to n-1
if A[i] == key
return i
return -1
#include <stdio.h>
int linearSearch(int arr[],
int n,
int key)
{
for(int i=0;i<n;i++)
{
if(arr[i]==key)
return i;
}
return -1;
}
int main()
{
int arr[] = {12,25,8,41,19};
int pos =
linearSearch(arr,5,19);
printf("%d",pos);
return 0;
}
Array = [19, 12, 25, 8, 41]
Target = 19
Found at first position.
Comparisons = 1
Time Complexity = O(1)
Array = [12,25,8,41,19]
Target = 100
Target does not exist.
Every element must be checked.
Time Complexity = O(n)
On average, the target is found around the middle of the list.
Comparisons ≈ n/2
Complexity = O(n)
Find 45 using Sequential Search
[10, 15, 22, 45, 60, 72]
Can we search faster than O(n)?
Binary Search is a fast searching algorithm that repeatedly divides the search space into two halves.
Works only on sorted data.
| Elements | Linear Search | Binary Search |
|---|---|---|
| 1,000 | Up to 1,000 checks | ≈ 10 checks |
| 1,000,000 | Up to 1,000,000 checks | ≈ 20 checks |
Huge improvement for large datasets.
[2, 5, 8, 11, 15, 20, 25]
Without sorting, Binary Search cannot work correctly.
[2, 5, 8, 11, 15, 20, 25]
Middle = 11
15 > 11
Ignore left half
[15, 20, 25]
Middle = 20
15 < 20
Ignore right half
[15]
Found!
low = first index high = last index mid = (low + high)/2
Search happens between low and high.
Array = [2,5,8,11,15,20,25] Target = 15
| low | high | mid | A[mid] |
|---|---|---|---|
| 0 | 6 | 3 | 11 |
| 4 | 6 | 5 | 20 |
| 4 | 4 | 4 | 15 |
1. Set low = 0 2. Set high = n-1 3. Find mid 4. Compare target with mid 5. If equal → Found 6. If smaller → Search left 7. If larger → Search right 8. Repeat until found
BinarySearch(A,n,key)
low = 0
high = n-1
while low <= high
mid = (low+high)/2
if A[mid] == key
return mid
else if key < A[mid]
high = mid - 1
else
low = mid + 1
return -1
int binarySearch(int arr[],
int n,
int key)
{
int low = 0;
int high = n - 1;
while(low <= high)
{
int mid =
(low+high)/2;
if(arr[mid]==key)
return mid;
if(key < arr[mid])
high = mid - 1;
else
low = mid + 1;
}
return -1;
}
The algorithm naturally divides the problem into smaller subproblems.
Therefore recursion is a natural fit.
BinarySearch(A,low,high,key)
if low > high
return -1
mid=(low+high)/2
if A[mid]==key
return mid
if key < A[mid]
search left half
else
search right half
int binarySearch(
int arr[],
int low,
int high,
int key)
{
if(low > high)
return -1;
int mid =
(low+high)/2;
if(arr[mid]==key)
return mid;
if(key < arr[mid])
return binarySearch(
arr,low,
mid-1,key);
return binarySearch(
arr,
mid+1,
high,key);
}
n n/2 n/4 n/8 n/16
Each step removes half of the data.
n / 2^k = 1 n = 2^k k = log₂(n)
Time Complexity = O(log n)
| Case | Complexity |
|---|---|
| Best | O(1) |
| Average | O(log n) |
| Worst | O(log n) |
| Feature | Linear | Binary |
|---|---|---|
| Sorted Data | No | Yes |
| Worst Case | O(n) | O(log n) |
| Implementation | Easy | Moderate |
[3,6,9,12,15,18,21,24,27] Find 24
Determine low, high and mid for each iteration.
Can we search even faster than O(log n)?
Hashing aims for O(1) average search time.
Goal: Achieve O(1) average search time.
We improved searching from:
Can we search in almost constant time?
Hashing provides the answer.
Dictionary stores:
(Key, Value)
| Key | Value |
|---|---|
| 101 | Ram |
| 102 | Sita |
Roll Number = 105 Store directly at Index 105
Search becomes O(1)
But memory usage becomes huge.
Student ID = 9999999
Need an enormous array.
Memory waste becomes unacceptable.
Solution → Hash Function
A function that converts a key into an array index.
index = h(key)
Example:
h(35)=5 h(27)=7
h(key) = key mod 10
| Key | Index |
|---|---|
| 23 | 3 |
| 41 | 1 |
| 57 | 7 |
| 62 | 2 |
Index Data 0 - 1 41 2 62 3 23 4 - 5 - 6 - 7 57
Two keys generate the same index.
h(25)=5 h(35)=5
Both want location 5.
Index 5 25 35
What should we do now?
Store multiple elements in a linked list.
Index 5 25 → 35 → 45
Search next empty location.
h(k) = index index occupied? Try: index+1 index+2 index+3 ...
Table Size = 10 25 → 5 35 → 5 (collision) Store at 6
5 : 25 6 : 35
5 : 25 6 : 35 7 : 45 8 : 55
Consecutive occupied locations form clusters.
h(k)+1² h(k)+2² h(k)+3² ...
Reduces clustering.
h(35)=5 Collision Try: 5+1² = 6 occupied Try: 5+2² = 9
Store at index 9.
Index = h1(key) + i*h2(key)
Uses a second hash function.
Produces better distribution.
h1(k)=k mod 10 h2(k)=7-(k mod 7)
Collision resolution becomes more random.
α = n / m
n = number of stored keys m = table size
Higher load factor means more collisions.
Stored Keys = 8 Table Size = 10 α = 8/10 = 0.8
80% full
| Operation | Average |
|---|---|
| Insert | O(1) |
| Search | O(1) |
| Delete | O(1) |
If many collisions occur:
Search = O(n)
Hash table performance depends heavily on the hash function.
| Method | Complexity |
|---|---|
| Sequential Search | O(n) |
| Binary Search | O(log n) |
| Hashing | O(1) |
Hash Function h(k)=k mod 10 Insert: 23, 15, 44, 52, 35
Construct the hash table manually.
Searching Algorithms, Binary Search, Hashing and Hash Tables
| Topic | Resource |
|---|---|
| Sequential Search | CLRS Chapter on Searching |
| Binary Search | Programiz & Visualgo |
| Hash Functions | Weiss & GeeksforGeeks |
| Collision Resolution | CLRS & Knuth |
Next Chapter: Sorting Algorithms