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%

Classic Algorithms: Sorting

Lesson 43 of 53 in the free Foundation of C & C++ notes on Siksha Sarovar, written by Rohit Jangra.

Bringing Order to Chaos

Sorting is the process of arranging data in a specific order (usually ascending or descending). It is a building block for many other algorithms (like Binary Search).

1. Bubble Sort (The Simplest)

Works by repeatedly swapping adjacent elements if they are in the wrong order. The largest elements "bubble up" to the end of the array.

  • Complexity: O(n²) - Very slow for large lists.
  • Best for: Learning purposes or very small arrays.

2. Selection Sort

Divides the array into two parts: sorted and unsorted. It repeatedly "selects" the smallest element from the unsorted part and moves it to the sorted part.

  • Complexity: O(n²).

3. Insertion Sort

Builds the sorted array one item at a time. It's how most people sort a hand of playing cards.

  • Complexity: O(n²) but very efficient for arrays that are "almost sorted."

Why so many sorting algorithms?

No single algorithm is best for every situation.

  • Small lists: Insertion sort is great.
  • Massive data: QuickSort or MergeSort (which use recursion) are the industry standards.

The Trade-off

Better performance usually means more complex code. As a beginner, master the O(n²) sorts first to understand the logic of swapping and indexing.

Standard C actually provides a built-in sort function in <stdlib.h> called qsort(). In a real job, you'd use that instead of writing your own!