Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 4: Searching Techniques

Lesson 13 of 14 in the free Data Structure notes on Siksha Sarovar, written by Rohit Jangra.

Searching: Finding the Needle

Searching is the operation of finding a specific record (Key) in a dataset.

Searching Algorithms — Comparison

AlgorithmBestAverageWorstRequires Sorted?Space
Linear SearchO(1)O(n)O(n)NoO(1)
Binary SearchO(1)O(log n)O(log n)YesO(1) iterative
HashingO(1)O(1)O(n)NoO(n)

1. Linear Search

Check every element from index 0 to n-1.

  • Pros: Works on unsorted data. Simple implementation.
  • Cons: Extremely slow for large data (1 million items = avg 500,000 comparisons).

2. Binary Search

Works only on sorted data. Repeatedly divide the search interval in half.

Worked Example: Find 23 in [2, 5, 8, 12, 16, 23, 38, 56, 72, 91]

StepLowHighMidarr[Mid]Action
10941623 > 16, go right
25975623 < 56, go left
356523Found at index 5!

Only 3 comparisons instead of 6 (linear). For 1 million items: max 20 steps.

3. Hashing (The "Instant" Search)

Uses a Hash Function to calculate the storage address directly.

Hash Function: Index = Key % TableSize Example: Store 25 in table of size 10 → 25 % 10 = 5 → Store at Index 5. To Find 25: Calculate 25 % 10 = 5 → Go to Index 5. Found instantly!

Types of Hash Functions

MethodFormulaExample (Key=234, Size=10)
DivisionKey % Size234 % 10 = 4
Mid-SquareSquare key, extract middle digits234² = 54756, middle = 47
FoldingSplit key into parts, add them2 + 34 = 36, 36 % 10 = 6
Digit ExtractionSelect specific digitsExtract 2nd,4th digits

The Collision Problem

Collision: Two keys map to the same index. E.g., 25 and 35 both map to Index 5.

Collision Resolution Methods

MethodTypeDescriptionProsCons
ChainingOpen HashingEach slot points to a linked list of elementsSimple, no table size limitExtra memory for pointers
Linear ProbingClosed HashingIf slot full, try next slot (i+1, i+2...)Simple, cache-friendlyPrimary clustering
Quadratic ProbingClosed HashingTry i+1², i+2², i+3²...Reduces primary clusteringSecondary clustering
Double HashingClosed HashingUse second hash function for step sizeBest distributionMore complex

Chaining vs Open Addressing

FeatureChainingOpen Addressing
Collision handlingExternal linked listsProbe within table
Load factorCan exceed 1.0Must be < 1.0
MemoryExtra pointer overheadNo extra pointers
DeletionEasy (remove from list)Difficult (needs tombstones)
PerformanceDegrades gracefullyDegrades sharply near full

Load Factor (α)

α = Number of Elements / Table Size

Load FactorPerformance Impact
α < 0.5Excellent — few collisions
0.5 < α < 0.7Good — acceptable performance
0.7 < α < 1.0Degrading — many collisions
α > 1.0Only possible with chaining
Best Practice: Resize (rehash) the table when α exceeds 0.7 to maintain O(1) performance.