This section lists the programs covered under Sorting Techniques in C. Click on a program title to jump directly to its detailed explanation.
Write a C program that implements the Bubble Sort method to sort a given list of integers in ascending order.
n.n integers into an array.n − 2:
#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;
}
Enter number of elements:
5
Enter array elements:
40 10 30 20 50
Sorted array (Ascending):
10 20 30 40 50
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.
Write a C program that sorts a given array of integers using Selection Sort in descending order.
n and store the elements in an array.i from 0 to n − 2:
i is the current maximum.
#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;
}
Enter number of elements:
5
Enter array elements:
40 10 30 20 50
Sorted array (Descending):
50 40 30 20 10
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.
Write a C program that sorts a given array of integers using Quick Sort in ascending order.
n and the array elements.quickSort(a, low, high) with low = 0 and high = n − 1.quickSort:
#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;
}
Enter number of elements:
6
Enter array elements:
30 10 50 20 60 40
Sorted array (Ascending):
10 20 30 40 50 60
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.
Write a C program that sorts a given array of integers using Insertion Sort in ascending order.
n and the array elements.n − 1:
key.key into its correct position.
#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;
}
Enter number of elements:
5
Enter array elements:
40 10 30 20 50
Sorted array (Ascending):
10 20 30 40 50
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.