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: Sorting Techniques

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

Sorting: Organizing Chaos

Sorting is fundamental. Whether it's organizing files by date, contacts by name, or products by price, sorting is everywhere.

Comprehensive Sorting Comparison

AlgorithmBestAverageWorstSpaceStable?Method
Bubble SortO(n)O(n²)O(n²)O(1)YesComparison
Selection SortO(n²)O(n²)O(n²)O(1)NoComparison
Insertion SortO(n)O(n²)O(n²)O(1)YesComparison
Merge SortO(n log n)O(n log n)O(n log n)O(n)YesDivide & Conquer
Quick SortO(n log n)O(n log n)O(n²)O(log n)NoDivide & Conquer
Heap SortO(n log n)O(n log n)O(n log n)O(1)NoComparison

What is Stability in Sorting?

Stable Sort: Two elements with equal keys maintain their original relative order after sorting.

OriginalStable SortUnstable Sort
(Alice, 85), (Bob, 85), (Carol, 90)(Alice, 85), (Bob, 85), (Carol, 90)(Bob, 85), (Alice, 85), (Carol, 90)
When does stability matter? When sorting by multiple criteria (e.g., sort by name, then by grade — you want same-grade students to stay alphabetical).

1. Insertion Sort (The "Card Player" Method)

How it works: Pick elements one at a time and insert them into their correct position in the sorted portion.

Trace: Array: [5, 3, 4, 1, 2]

PassKeyActionArray
133 < 5, shift 5 right[3, 5, 4, 1, 2]
244 < 5, shift; 4 > 3, insert[3, 4, 5, 1, 2]
31Shift 5, 4, 3 right[1, 3, 4, 5, 2]
42Shift 5, 4, 3 right; insert after 1[1, 2, 3, 4, 5]

Best for: Small arrays, nearly sorted data. Worst for: Large unsorted arrays.

2. Selection Sort (The "Pick the Best" Method)

How it works: Find the minimum element and swap it with the first unsorted position.

Trace: Array: [64, 25, 12, 22]

PassMin FoundSwap WithResult
112 (index 2)64 (index 0)[12, 25, 64, 22]
222 (index 3)25 (index 1)[12, 22, 64, 25]
325 (index 3)64 (index 2)[12, 22, 25, 64]

Note: Always makes O(n²) comparisons regardless of input order.

3. Merge Sort (The "Divide & Conquer" Strategy)

How it works: Recursively split array in half until single elements, then merge sorted halves.

Trace: [38, 27, 43, 3]

[38, 27, 43, 3]
   /          \
[38, 27]    [43, 3]
 /    \      /    \
[38] [27]  [43]  [3]
 \    /      \    /
[27, 38]    [3, 43]
     \        /
  [3, 27, 38, 43]

Key Advantage: Always O(n log n). Disadvantage: Requires O(n) extra space.

4. Quick Sort (The "Pivot" Strategy)

How it works: Choose a pivot, partition array so elements < pivot go left, > pivot go right. Recurse on both halves.

Trace: [10, 7, 8, 9, 1, 5], Pivot = 5

  1. Partition: elements < 5 go left, > 5 go right → [1, 5, 8, 9, 10, 7]
  2. Recurse on [1] (done) and [8, 9, 10, 7]
  3. Continue partitioning until sorted.
FeatureQuick SortMerge Sort
AverageO(n log n)O(n log n)
WorstO(n²) (bad pivot)O(n log n)
SpaceO(log n) in-placeO(n) extra
StableNoYes
In PracticeFaster (cache-friendly)Used when stability needed

Choosing the Right Sort

ScenarioBest AlgorithmWhy
Small array (< 50)Insertion SortLow overhead, simple
Nearly sorted dataInsertion SortO(n) best case
Large dataset, guaranteed perfMerge SortO(n log n) always
General purpose, in-placeQuick SortFastest in practice
Memory constrainedHeap SortO(1) extra space