C Programs

C Programming – Sorting


Programs at a Glance

This section lists the programs covered under Sorting Techniques in C. Click on a program title to jump directly to its detailed explanation.

  1. Bubble Sort in Ascending Order
  2. Selection Sort in Descending Order
  3. Quick Sort in Ascending Order
  4. Insertion Sort in Ascending Order

1. Bubble Sort in Ascending Order


Problem Statement

Write a C program that implements the Bubble Sort method to sort a given list of integers in ascending order.

Student-Friendly Algorithm
  1. Start the program.
  2. Read the number of elements n.
  3. Read n integers into an array.
  4. Repeat for each pass from 0 to n − 2:
    • Compare each pair of adjacent elements.
    • If the left element is greater than the right, swap them.
  5. After all passes, the array will be sorted in ascending order.
  6. Display the sorted array.
  7. Stop the program.
C Program

#include <stdio.h>

int main() {
    int a[50], n, i, j, temp;

    printf("Enter number of elements:\n");
    scanf("%d", &n);

    printf("Enter array elements:\n");
    for (i = 0; i < n; i++)
        scanf("%d", &a[i]);

    for (i = 0; i < n - 1; i++) {
        for (j = 0; j < n - 1 - i; j++) {
            if (a[j] > a[j + 1]) {
                temp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = temp;
            }
        }
    }

    printf("Sorted array (Ascending):\n");
    for (i = 0; i < n; i++)
        printf("%d ", a[i]);

    return 0;
}
    
Sample Output

Enter number of elements:
5
Enter array elements:
40 10 30 20 50
Sorted array (Ascending):
10 20 30 40 50
    
Explanation

Bubble sort repeatedly compares adjacent elements and swaps them if they are in the wrong order, causing larger values to “bubble up” towards the end of the array in each pass. After the i-th pass, the last i elements are in their correct positions, so the inner loop length decreases over time.

2. Selection Sort in Descending Order


Problem Statement

Write a C program that sorts a given array of integers using Selection Sort in descending order.

Student-Friendly Algorithm
  1. Start the program.
  2. Read the number of elements n and store the elements in an array.
  3. For each position i from 0 to n − 2:
    • Assume the element at position i is the current maximum.
    • Scan the remaining elements to find the true maximum element index.
    • Swap the current element with the maximum element found.
  4. After all passes, the array will be sorted in descending order.
  5. Display the sorted array.
  6. Stop the program.
C Program

#include <stdio.h>

int main() {
    int a[50], n, i, j, max, temp;

    printf("Enter number of elements:\n");
    scanf("%d", &n);

    printf("Enter array elements:\n");
    for (i = 0; i < n; i++)
        scanf("%d", &a[i]);

    for (i = 0; i < n - 1; i++) {
        max = i;
        for (j = i + 1; j < n; j++) {
            if (a[j] > a[max])
                max = j;
        }
        temp = a[i];
        a[i] = a[max];
        a[max] = temp;
    }

    printf("Sorted array (Descending):\n");
    for (i = 0; i < n; i++)
        printf("%d ", a[i]);

    return 0;
}
    
Sample Output

Enter number of elements:
5
Enter array elements:
40 10 30 20 50
Sorted array (Descending):
50 40 30 20 10
    
Explanation

Selection sort repeatedly selects the largest remaining element and places it at the current position i. After each pass, the prefix of the array from index 0 to i becomes sorted in descending order, while the rest is still unsorted.

3. Quick Sort in Ascending Order


Problem Statement

Write a C program that sorts a given array of integers using Quick Sort in ascending order.

Student-Friendly Algorithm
  1. Start the program.
  2. Read the number of elements n and the array elements.
  3. Call quickSort(a, low, high) with low = 0 and high = n − 1.
  4. In quickSort:
    • Choose a pivot element (for example, the middle element).
    • Partition the array so that elements less than the pivot come before it and elements greater than the pivot come after it.
    • Recursively apply quick sort to the left subarray and the right subarray.
  5. After recursion finishes, the array is sorted in ascending order.
  6. Display the sorted array.
  7. Stop the program.
C Program

#include <stdio.h>

void quickSort(int a[], int low, int high) {
    int i = low, j = high, pivot = a[(low + high) / 2], temp;

    while (i <= j) {
        while (a[i] < pivot)
            i++;
        while (a[j] > pivot)
            j--;
        if (i <= j) {
            temp = a[i];
            a[i] = a[j];
            a[j] = temp;
            i++;
            j--;
        }
    }

    if (low < j)
        quickSort(a, low, j);
    if (i < high)
        quickSort(a, i, high);
}

int main() {
    int a[50], n, i;

    printf("Enter number of elements:\n");
    scanf("%d", &n);

    printf("Enter array elements:\n");
    for (i = 0; i < n; i++)
        scanf("%d", &a[i]);

    quickSort(a, 0, n - 1);

    printf("Sorted array (Ascending):\n");
    for (i = 0; i < n; i++)
        printf("%d ", a[i]);

    return 0;
}
    
Sample Output

Enter number of elements:
6
Enter array elements:
30 10 50 20 60 40
Sorted array (Ascending):
10 20 30 40 50 60
    
Explanation

Quick sort is a divide-and-conquer algorithm: the pivot splits the array into two parts, and each part is sorted independently using the same process. The while loop moves i forward until an element ≥ pivot is found and moves j backward until an element ≤ pivot is found, then swaps them.

The recursive calls quickSort(a, low, j) and quickSort(a, i, high) sort the left and right partitions respectively. This approach is efficient for large arrays and typically runs in near linearithmic time.

4. Insertion Sort in Ascending Order


Problem Statement

Write a C program that sorts a given array of integers using Insertion Sort in ascending order.

Student-Friendly Algorithm
  1. Start the program.
  2. Read the number of elements n and the array elements.
  3. Treat the first element as a sorted part of the array.
  4. For each element from index 1 to n − 1:
    • Store the current element in key.
    • Shift all larger elements in the sorted part one position to the right.
    • Insert key into its correct position.
  5. After processing all elements, the entire array is sorted in ascending order.
  6. Display the sorted array.
  7. Stop the program.
C Program

#include <stdio.h>

int main() {
    int a[50], n, i, j, key;

    printf("Enter number of elements:\n");
    scanf("%d", &n);

    printf("Enter array elements:\n");
    for (i = 0; i < n; i++)
        scanf("%d", &a[i]);

    for (i = 1; i < n; i++) {
        key = a[i];
        j = i - 1;

        while (j >= 0 && a[j] > key) {
            a[j + 1] = a[j];
            j--;
        }
        a[j + 1] = key;
    }

    printf("Sorted array (Ascending):\n");
    for (i = 0; i < n; i++)
        printf("%d ", a[i]);

    return 0;
}
    
Sample Output

Enter number of elements:
5
Enter array elements:
40 10 30 20 50
Sorted array (Ascending):
10 20 30 40 50
    
Explanation

Insertion sort builds the sorted section of the array one element at a time by inserting each new element into its correct position among the already sorted elements. Larger elements are shifted to the right to make space for the key.

This algorithm is easy to understand and efficient for small or nearly sorted arrays, making it useful for teaching and for practical use on small datasets.