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

  1. Compare adjacent elements.
  2. Swap them if the first element is greater than the second.
  3. Move to the next pair and repeat until the end of the array.
  4. 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

  1. Find the smallest element in the unsorted array.
  2. Swap it with the first unsorted element.
  3. Move the boundary of the sorted portion one position to the right.
  4. 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

Insertion Sort is a simple sorting algorithm that builds the final sorted array one element at a time. It works similarly to how you arrange playing cards in your hand.

Working Principle

  1. Assume the first element is already sorted.
  2. Pick the next element (called the key).
  3. Compare the key with elements in the sorted part of the array.
  4. Shift larger elements one position to the right.
  5. Insert the key into its correct position.
  6. Repeat for all elements.

Example

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

Merge Sort is a highly efficient sorting algorithm based on the Divide and Conquer technique. It divides the array into smaller subarrays, sorts them, and then merges them back together in sorted order.

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]

Divide Phase:
[38, 27, 43, 3, 9, 82, 10]
         ↓
[38, 27, 43, 3]      [9, 82, 10]
         ↓
[38, 27] [43, 3] [9, 82] [10]
         ↓
[38] [27] [43] [3] [9] [82] [10]


Merge Phase:
[38] [27]      → [27, 38]
[43] [3]       → [3, 43]
[9] [82]       → [9, 82]
[10]
[27, 38] [3, 43]   → [3, 27, 38, 43]
[9, 82] [10]       → [9, 10, 82]
[3, 27, 38, 43] [9, 10, 82]
→ [3, 9, 10, 27, 38, 43, 82]

Final Sorted Array:
[3, 9, 10, 27, 38, 43, 82]

Recursive Tree Representation:
                [38,27,43,3,9,82,10]
               /                   \
      [38,27,43,3]             [9,82,10]
        /      \                /      \
   [38,27]   [43,3]         [9,82]   [10]
    /   \      /  \          /  \
 [38][27] [43][3]       [9][82]

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

Quick Sort is a highly efficient sorting algorithm based on the Divide and Conquer technique. It selects a pivot element and partitions the array around it, placing smaller elements on the left and larger elements on the right.

Working Principle

  1. Choose a pivot element.
  2. Partition the array so that:
    • Elements smaller than the pivot are on the left.
    • Elements greater than the pivot are on the right.
  3. Recursively apply Quick Sort to the left and right subarrays.
  4. 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:

Pivot = 70

Step 2: Partition
[10, 80, 30, 90, 40, 50, 70]
↓
[10, 30, 40, 50, 70, 90, 80]

Now pivot (70) is in its correct position.

Step 3: Recursively Sort

Left Subarray: [10, 30, 40, 50]
Right Subarray: [90, 80]

After further partitioning:
[10, 30, 40, 50, 70, 80, 90]

Recursive Tree:

              [10,80,30,90,40,50,70]
                          |
                       Pivot 70
                          |
          --------------------------------
          |                              |
   [10,30,40,50]                    [90,80]
          |                              |
      Pivot 50                      Pivot 80
          |                              |
    [10,30,40]                        [90]
          |
      Pivot 40
          |
      [10,30]


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)







Comments

Popular posts from this blog

Setting up a USB thermal printer with a Raspberry Pi 3B+

Create a autorun for a PyQt5 raspberry pi Application

Basic Input/Output C Program