Sorting Algorithms in C Programming
Sorting Algorithm :
1. Bubble Sort
Bubble Sort is a simple sorting algorithm that repeatedly compares adjacent elements and swaps them if they are in the wrong order. This process continues until the array is sorted.
Algorithm Steps
- Compare adjacent elements.
- Swap them if the first element is greater than the second.
- Move to the next pair and repeat until the end of the array.
- Repeat the entire process for all elements until no swaps are needed.
Array: [5, 3, 8, 4, 2]
Pass 1:
- Compare 5 and 3 → Swap →
[3, 5, 8, 4, 2] - Compare 5 and 8 → No swap
- Compare 8 and 4 → Swap →
[3, 5, 4, 8, 2] - Compare 8 and 2 → Swap →
[3, 5, 4, 2, 8]
Pass 2:
- Compare 3 and 5 → No swap
- Compare 5 and 4 → Swap →
[3, 4, 5, 2, 8] - Compare 5 and 2 → Swap →
[3, 4, 2, 5, 8]
Continue until sorted:
Final Result: [2, 3, 4, 5, 8]
C code:
Complexity:
Best Case: O(n) (already sorted)
Average Case: O(n²)
Worst Case: O(n²)
Space: O(1) (in-place)
2. Selection Sort
Selection Sort is a simple sorting algorithm that repeatedly selects the smallest element from the unsorted portion of the array and places it at the beginning.
Working Principle
- Find the smallest element in the unsorted array.
- Swap it with the first unsorted element.
- Move the boundary of the sorted portion one position to the right.
- Repeat until the entire array is sorted.
Example
Array: [64, 25, 12, 22, 11]
Pass 1
- Smallest element = 11
- Swap with 64
[11, 25, 12, 22, 64]
Pass 2
- Smallest element in remaining array = 12
- Swap with 25
[11, 12, 25, 22, 64]
Pass 3
- Smallest element = 22
- Swap with 25
[11, 12, 22, 25, 64]
Pass 4
- Remaining elements already sorted
Final Sorted Array:
[11, 12, 22, 25, 64]
C code:
Complexity:
Best Case: O(n²)
Average Case: O(n²)
Worst Case: O(n²)
Space: O(1)
3. Insertion Sort
Working Principle
- Assume the first element is already sorted.
- Pick the next element (called the key).
- Compare the key with elements in the sorted part of the array.
- Shift larger elements one position to the right.
- Insert the key into its correct position.
- Repeat for all elements.
Array: [5, 2, 4, 6, 1, 3]
Pass 1
Key = 2
[5, 2, 4, 6, 1, 3]
↓
[2, 5, 4, 6, 1, 3]
Pass 2
Key = 4
[2, 5, 4, 6, 1, 3]
↓
[2, 4, 5, 6, 1, 3]
Pass 3
Key = 6
[2, 4, 5, 6, 1, 3]
(No change)
Pass 4
Key = 1
[2, 4, 5, 6, 1, 3]
↓
[1, 2, 4, 5, 6, 3]
Pass 5
Key = 3
[1, 2, 4, 5, 6, 3]
↓
[1, 2, 3, 4, 5, 6]
Final Sorted Array
[1, 2, 3, 4, 5, 6]C code:
Complexity:
Best Case: O(n) (already sorted)
Average Case: O(n²)
Worst Case: O(n²)
Space: O(1)
4. Merge Sort
Working Principle
Step 1: Divide
Split the array into two halves until each subarray contains only one element.
Step 2: Conquer
Recursively sort the subarrays.
Step 3: Merge
Combine the sorted subarrays to produce the final sorted array.
Example
Array: [38, 27, 43, 3, 9, 82, 10]
C code:
Complexity:
Best Case: O(n log n)
Average Case: O(n log n)
Worst Case: O(n log n)
Space: O(n) (extra arrays)
void insertionSort(int arr[], int n) { for (int i = 1; i < n; i++) { int key = arr[i]; int j = i - 1; while (j >= 0 && arr[j] > key) { arr[j+1] = arr[j]; j--; } arr[j+1] = key; } }
void insertionSort(int arr[], int n) { for (int i = 1; i < n; i++) { int key = arr[i]; int j = i - 1; while (j >= 0 && arr[j] > key) { arr[j+1] = arr[j]; j--; } arr[j+1] = key; } }
void selectionSort(int arr[], int n) { for (int i = 0; i < n-1; i++) { int minIndex = i; for (int j = i+1; j < n; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } int temp = arr[minIndex]; arr[minIndex] = arr[i]; arr[i] = temp; } }
5. Quick Sort
Working Principle
- Choose a pivot element.
- Partition the array so that:
- Elements smaller than the pivot are on the left.
- Elements greater than the pivot are on the right.
- Recursively apply Quick Sort to the left and right subarrays.
- Continue until subarrays contain zero or one element.
Example
Array: [10, 80, 30, 90, 40, 50, 70]
Step 1: Choose Pivot
Take the last element as pivot:
Now pivot (70) is in its correct position.
Step 3: Recursively Sort
Code:
Complexity:
Best Case: O(n log n)
Average Case: O(n log n)
Worst Case: O(n²) (bad pivot choice)
Space: O(log n) (recursive stack)
Related Links :






Comments
Post a Comment