⚡0 XP🔥0

Sorting Algorithms

Lesson 3 of 7 ⏱ 6 min read ✎ 2 exercises ❓ 5-question quiz ▶ Runs C++

Bubble, selection and insertion sort, the divide-and-conquer sorts merge sort and quick sort, stability, comparing their complexity, and sorting with std::sort and custom comparators.

Why sorting matters#

Sorted data is easy to search (binary search), find duplicates in, rank, merge and display. Sorting is also the classic way to learn how algorithm design affects speed.

InteractiveWatch sorting algorithms work

Compare bubble, insertion and other sorts on the same data. Count the comparisons and swaps.

Bubble sort - O(n²)#

Repeatedly swap neighbours that are in the wrong order. After each pass the largest remaining element "bubbles" to the end.

▶ Try it YourselfC++
Ctrl + Enter to run
Output
after pass 1: 10 14 29 13 37 
after pass 2: 10 14 13 29 37 
after pass 3: 10 13 14 29 37 
after pass 4: 10 13 14 29 37 

Selection sort and insertion sort - O(n²)#

  • Selection sort - find the smallest element and put it first, then the next smallest second... Always n²/2 comparisons, few swaps.
  • Insertion sort - take each element and insert it into the already-sorted part on its left, like arranging playing cards in your hand. Very fast on nearly sorted data, and used inside real-world sorts for small pieces.
हिंदी में समझें

Insertion sort को ताश के पत्तों की तरह समझें। हाथ में पत्ते पहले से क्रम में हैं; नया पत्ता उठाते हैं और उसे सही जगह पर सरका कर डालते हैं - बड़े पत्ते एक-एक जगह दाईं ओर खिसक जाते हैं।

Merge sort - O(n log n), divide and conquer#

  1. Divide the array into two halves.
  2. Sort each half recursively.
  3. Merge the two sorted halves in one linear pass.
▶ Try it YourselfC++
Ctrl + Enter to run
Output
3 9 10 27 38 43 82 

There are log n levels of splitting, and each level does O(n) merging work → O(n log n). It needs O(n) extra memory and is stable (because of <=).

Quick sort - O(n log n) on average#

Pick a pivot, move smaller elements to its left and bigger ones to its right (partition), then sort both sides recursively.

▶ Try it YourselfC++
Ctrl + Enter to run
Output
10 30 40 50 70 80 90 

Quick sort is usually the fastest in practice and sorts in place, but a bad pivot (e.g. always the last element of already-sorted data) makes it O(n²). Real libraries pick pivots carefully.

Comparison#

Algorithm Best Average Worst Extra space Stable
Bubble O(n) O(n²) O(n²) O(1) yes
Selection O(n²) O(n²) O(n²) O(1) no
Insertion O(n) O(n²) O(n²) O(1) yes
Merge O(n log n) O(n log n) O(n log n) O(n) yes
Quick O(n log n) O(n log n) O(n²) O(log n) no

In real code: std::sort#

std::sort (introsort - quick sort + heap sort + insertion sort) is O(n log n) and very fast. Use stable_sort when you need stability.

▶ Try it YourselfC++
Ctrl + Enter to run
Output
1 2 5 7 9 
9 7 5 2 1 
Om Asha Imran Bhavna 

Summary#

  • Simple sorts (bubble, selection, insertion) are O(n²) - fine for small or nearly sorted data.
  • Merge sort: O(n log n) always, stable, needs extra memory.
  • Quick sort: O(n log n) on average, in place, O(n²) worst case.
  • In C++ use sort (or stable_sort) with a comparator or lambda.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Insertion sort

Implement insertion sort in insertionSort (no std::sort). The program prints the array after sorting:

5 12 19 23 37 41 64

Expected output:
5 12 19 23 37 41 64
2

Rank list with a custom comparator

Sort the students by marks, highest first; when marks are equal, by name A-Z. Print each as Name Marks.

Expected output:
Asha 92
Bhavna 92
Imran 85
Neha 78
Ravi 78

Quick Quiz

Q1.

What is the worst-case time complexity of bubble sort?

Q2.

Which sort is guaranteed O(n log n) in the worst case?

Q3.

Insertion sort is very fast when the data is...

Q4.

A sorting algorithm is stable if...

Q5.

How do you sort a vector v in descending order with the STL?