DSA Sorting Algorithms


Definition

Sorting is the technique of reordering elements so that they follow a specific sequence, like organizing files alphabetically or ranking scores from highest to lowest. It helps make searching faster and organizes data in a meaningful way—just like alphabetizing a contact list.


Why Sorting Matters

Sorting improves efficiency, readability, and accessibility of data. It’s essential in everything from e-commerce product listings to leaderboards in games and database indexing.


Types of Sorting Algorithms

Let’s explore common sorting techniques, each explained uniquely and with easy examples.


1. Bubble Sort

How it Works:

It checks neighboring values one by one and flips them if they're in the wrong sequence, gradually moving the largest items toward the end. This continues repeatedly like "bubbles" rising to the top.

Example

Sort [4, 2, 7, 1]

  • Compare 4 and 2 → swap → [2, 4, 7, 1]
  • Compare 4 and 7 → ok
  • Compare 7 and 1 → swap → [2, 4, 1, 7]

Repeat until sorted.

Unique Insight:

Think of students adjusting their position in line by checking with the person next to them again and again until no swaps are needed.

Time Complexity: O(n²)

Best For: Small datasets or for educational purposes.


2. Selection Sort

How it Works:

Find the smallest item in the list and move it to the front. Repeat this for the remaining list.

Example

Sort [5, 3, 8, 2]

  • Smallest is 2 → move to front → [2, 3, 8, 5]
  • Next smallest is 3 (already in place)
  • Then 5 and 8 → swap → [2, 3, 5, 8]

Unique Insight:

It’s like picking the shortest person from a group and placing them at the front, then repeating with the rest.

Time Complexity: O(n²)

Best For: Simplicity and easy implementation.


3. Insertion Sort

How it Works:

Take elements one by one and insert them into their correct place in the sorted part of the list.

Example:

Sort [4, 1, 3]

  • Start with 1 → insert before 4 → [1, 4, 3]
  • Then 3 → insert between 1 and 4 → [1, 3, 4]

Unique Insight:

Like arranging playing cards in your hand—each new card goes where it fits.

Time Complexity: O(n²)

Best For: Data that's almost ordered or collections with only a few elements work best here.


4. Merge Sort

How it Works:

Keep splitting the array into smaller pieces until each piece has a single item, then combine them step by step while sorting along the way.

Example

  • Sort [6, 2, 8, 4]
  • Split → [6, 2] and [8, 4] →
  • Sort and merge → [2, 6] and [4, 8] →
  • Final merge → [2, 4, 6, 8]

Unique Insight:

Imagine cutting a pizza into slices, sorting each slice, then assembling them back in perfect order.

Time Complexity: O(n log n)

Best For: Great choice for tiny lists or when things are almost sorted already.


5. Quick Sort

How it Works:

Pick a "pivot" value. Pick a pivot, shift lower values to its left and higher ones to its right, then sort both sides the same way.

Example

  • Sort [5, 3, 9, 1]
  • Pivot = 5
  • Left: [3, 1], Right: [9] → Sort recursively
  • Final: [1, 3, 5, 9]

Unique Insight:

Like choosing a leader and arranging everyone else to stand on the correct side of them, based on their height.

Time Complexity: O(n log n) average

Best For: Fast sorting with good average-case speed.


6. Heap Sort

How it Works:

Turn the list into a special tree where the biggest value is always on top, then pull it out one by one and place it at the end of the array.

Example:

Sort [4, 10, 3]

  • Build max heap → [10, 4, 3]
  • Swap 10 with last → [3, 4, 10]
  • Re-heapify remaining

Unique Insight:

Think of pulling the biggest rock from a pile and stacking it separately, one by one.

Time Complexity: O(n log n)

Best For: Guaranteed performance and no extra memory.


7. Counting Sort

How it Works:

Count the number of times each value appears and use this to determine positions in the final list.

Example

  • Sort [2, 1, 2, 0]
  • Count: 0→1, 1→1, 2→2
  • Build sorted array: [0, 1, 2, 2]

Unique Insight:

Perfect when sorting kids by age, assuming everyone’s age is a whole number and within a small range.

Time Complexity: O(n + k)

Best For: Non-negative integers in a small range.


8. Radix Sort

How it Works:

Sort numbers digit by digit—starting from the least significant digit (units place) to the most.

Example

  • Sort [170, 45, 75]
  • First by units → [170, 75, 45]
  • Then by tens → [170, 45, 75]
  • Then by hundreds → [45, 75, 170]

Unique Insight:

Like sorting documents by their last digit first, then middle digit, then starting digit.

Time Complexity: O(nk)

Best For: Integers with a fixed number of digits.


Time Complexity Summary

AlgorithmBest CaseAverage CaseWorst Case
Bubble SortO(n)O(n²)O(n²)
Selection SortO(n²)O(n²)O(n²)
Insertion SortO(n)O(n²)O(n²)
Merge SortO(n log n)O(n log n)O(n log n)
Quick SortO(n log n)O(n log n)O(n²)
Heap SortO(n log n)O(n log n)O(n log n)
Counting SortO(n + k)O(n + k)O(n + k)
Radix SortO(nk)O(nk)O(nk)

Use Cases

  • Bubble/Selection Sort – Teaching tools for understanding basic sorting
  • Quick Sort – Default in many libraries due to speed
  • Merge Sort – External sorting (huge data on disk)
  • Counting/Radix Sort – Useful for organizing test marks, postal codes, or identification numbers in a clear sequence.

Summary

  • Sorting organizes data and boosts search efficiency.
  • Use simple sorts for small inputs or educational goals.
  • For big datasets, go with Quick Sort, Merge Sort, or Heap Sort to handle them fast and efficiently.
  • If you're working with numeric data that follows a pattern, try Counting Sort or Radix Sort for better speed.

Prefer Learning by Watching?

Watch these YouTube tutorials to understand DATA STRUCTURES ALGORITHMS Tutorial visually:

What You'll Learn:
  • 📌 Searching & Sorting Techniques | Introduction | Playlist | DAA ADA Data Structure | Algorithms
  • 📌 10 Sorting Algorithms Easily Explained
Previous Next