What is Sorting?
A sorting algorithm arranges data in a specific order, usually ascending or descending. In C, sorting is often used with arrays.
- Ascending: 1, 2, 3, 4, 5
- Descending: 5, 4, 3, 2, 1
- Why it matters: sorting makes searching, comparing, and displaying data easier
Bubble Sort
Bubble sort compares adjacent elements and swaps them if they are in the wrong order. It is simple, but slow for large arrays.
#include <stdio.h> int main() { int arr[] = {5, 1, 4, 2, 8}; int n = sizeof(arr) / sizeof(arr[0]); int i, j, temp; for (i = 0; i < n - 1; i++) { for (j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } printf("Sorted array: "); for (i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; }
| Property | Value |
|---|---|
| Best for | Very small arrays |
| Time complexity | O(n²) |
| Space complexity | O(1) |
Selection Sort
Selection sort finds the smallest element in the unsorted part and places it at the beginning.
#include <stdio.h> int main() { int arr[] = {29, 10, 14, 37, 13}; int n = sizeof(arr) / sizeof(arr[0]); int i, j, minIndex, temp; for (i = 0; i < n - 1; i++) { minIndex = i; for (j = i + 1; j < n; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; } printf("Sorted array: "); for (i = 0; i < n; i++) printf("%d ", arr[i]); printf("\n"); return 0; }
- Find the smallest value
- Swap it with the current position
- Repeat until the array is sorted
Insertion Sort
Insertion sort builds the sorted part one element at a time, like sorting cards in your hand.
#include <stdio.h> int main() { int arr[] = {12, 11, 13, 5, 6}; int n = sizeof(arr) / sizeof(arr[0]); int i, key, j; for (i = 1; i < n; i++) { key = arr[i]; j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = key; } printf("Sorted array: "); for (i = 0; i < n; i++) printf("%d ", arr[i]); printf("\n"); return 0; }
Merge Sort
Merge sort uses a divide-and-conquer strategy: split the array into halves, sort each half, then merge them back together.
| Property | Value |
|---|---|
| Time complexity | O(n log n) |
| Space complexity | O(n) |
| Best for | Large data sets |
#include <stdio.h> void merge(int arr[], int l, int m, int r) { int i, j, k; int n1 = m - l + 1; int n2 = r - m; int L[20], R[20]; for (i = 0; i < n1; i++) L[i] = arr[l + i]; for (j = 0; j < n2; j++) R[j] = arr[m + 1 + j]; i = 0; j = 0; k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) arr[k++] = L[i++]; else arr[k++] = R[j++]; } while (i < n1) arr[k++] = L[i++]; while (j < n2) arr[k++] = R[j++]; } void mergeSort(int arr[], int l, int r) { if (l < r) { int m = l + (r - l) / 2; mergeSort(arr, l, m); mergeSort(arr, m + 1, r); merge(arr, l, m, r); } }
Quick Sort
Quick sort chooses a pivot element, partitions the array around it, and recursively sorts the two parts. It is usually very fast in practice.
#include <stdio.h> void swap(int* a, int* b) { int t = *a; *a = *b; *b = t; } int partition(int arr[], int low, int high) { int pivot = arr[high]; int i = low - 1; for (int j = low; j < high; j++) { if (arr[j] < pivot) { i++; swap(&arr[i], &arr[j]); } } swap(&arr[i + 1], &arr[high]); return i + 1; } void quickSort(int arr[], int low, int high) { if (low < high) { int pi = partition(arr, low, high); quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } }
| Algorithm | Best Case | Average | Worst | Extra Space |
|---|---|---|---|---|
| Bubble Sort | O(n) | O(n²) | O(n²) | O(1) |
| Selection Sort | O(n²) | O(n²) | O(n²) | O(1) |
| Insertion Sort | O(n) | O(n²) | O(n²) | O(1) |
| Merge Sort | O(n log n) | O(n log n) | O(n log n) | O(n) |
| Quick Sort | O(n log n) | O(n log n) | O(n²) | O(log n) |
Quick Quiz
Which sorting algorithm compares adjacent elements?
Which algorithm is usually best for nearly sorted arrays?
Which sorting algorithm uses divide and conquer?
Which sorting algorithm typically uses a pivot?
Lesson Checklist
- I know what sorting means
- I can explain bubble sort
- I can explain selection sort
- I can explain insertion sort
- I know why merge sort is efficient
- I know quick sort uses a pivot
- I understand sorting complexity basics
- I completed the quiz
Next Practice Idea
- Sort an array of student marks using ascending order practice
- Modify the code to sort in descending order practice
- Compare time complexity of different algorithms theory