Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Explain the sorting techniques in C language
In this article, we are going to learn about the different sorting techniques and their implementations in C language. Sorting in C programming requires rearranging elements in the array into a determined order, i.e., in ascending or descending order. This uses comparison operators to specify the new order of a list or array. This is widely used in different applications that include searching algorithms, data structure optimization, and database management.
Types of Sorting Techniques
The following are the main sorting techniques that we can implement in C language −
Bubble Sort
Bubble Sort is an elementary sorting algorithm that works by repeatedly exchanging adjacent elements if they are in the wrong order. This process continues until no exchanges are required, indicating the array is sorted.
Example of Bubble Sort
The following example repeatedly compares and swaps adjacent elements if they are in the wrong order −
#include <stdio.h>
void bubbleSort(int array[], int size) {
for(int i = 0; i < size; i++) {
int swaps = 0;
for(int j = 0; j < size - i - 1; j++) {
if(array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
swaps = 1;
}
}
if(!swaps)
break;
}
}
int main() {
int arr[5] = {67, 44, 82, 17, 20};
int n = 5;
printf("Array before Sorting: ");
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("
");
bubbleSort(arr, n);
printf("Array after Sorting: ");
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("
");
return 0;
}
Array before Sorting: 67 44 82 17 20 Array after Sorting: 17 20 44 67 82
Insertion Sort
Insertion sort is a simple sorting algorithm that builds the sorted array one element at a time. It works by taking each element and inserting it into its correct position among the previously sorted elements.
Example of Insertion Sort
This C program implements Insertion Sort to sort an array of integers −
#include <stdio.h>
void insertionSort(int array[], int size) {
for(int i = 1; i < size; i++) {
int key = array[i];
int j = i - 1;
while(j >= 0 && array[j] > key) {
array[j + 1] = array[j];
j--;
}
array[j + 1] = key;
}
}
int main() {
int arr[5] = {67, 44, 82, 17, 20};
int n = 5;
printf("Array before Sorting: ");
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("
");
insertionSort(arr, n);
printf("Array after Sorting: ");
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("
");
return 0;
}
Array before Sorting: 67 44 82 17 20 Array after Sorting: 17 20 44 67 82
Selection Sort
Selection sort is a simple in-place sorting algorithm that divides the array into sorted and unsorted portions. It repeatedly finds the minimum element from the unsorted part and places it at the beginning.
Example of Selection Sort
The following C program implements Selection Sort to sort an array of integers −
#include <stdio.h>
void selectionSort(int array[], int size) {
for(int i = 0; i < size - 1; i++) {
int minIndex = i;
for(int j = i + 1; j < size; j++) {
if(array[j] < array[minIndex]) {
minIndex = j;
}
}
int temp = array[i];
array[i] = array[minIndex];
array[minIndex] = temp;
}
}
int main() {
int arr[5] = {12, 19, 55, 2, 16};
int n = 5;
printf("Array before Sorting: ");
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("
");
selectionSort(arr, n);
printf("Array after Sorting: ");
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("
");
return 0;
}
Array before Sorting: 12 19 55 2 16 Array after Sorting: 2 12 16 19 55
Merge Sort
Merge sort is a divide-and-conquer algorithm that divides the array into halves, sorts them recursively, and then merges the sorted halves. It has a time complexity of O(n log n).
Example of Merge Sort
This C program implements Merge Sort to sort an array of integers −
#include <stdio.h>
void merge(int arr[], int left, int mid, int right) {
int i, j, k;
int n1 = mid - left + 1;
int n2 = right - mid;
int leftArr[n1], rightArr[n2];
for(i = 0; i < n1; i++)
leftArr[i] = arr[left + i];
for(j = 0; j < n2; j++)
rightArr[j] = arr[mid + 1 + j];
i = 0; j = 0; k = left;
while(i < n1 && j < n2) {
if(leftArr[i] <= rightArr[j]) {
arr[k] = leftArr[i];
i++;
} else {
arr[k] = rightArr[j];
j++;
}
k++;
}
while(i < n1) {
arr[k] = leftArr[i];
i++;
k++;
}
while(j < n2) {
arr[k] = rightArr[j];
j++;
k++;
}
}
void mergeSort(int arr[], int left, int right) {
if(left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
int main() {
int arr[6] = {64, 34, 25, 12, 22, 11};
int n = 6;
printf("Array before Sorting: ");
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("
");
mergeSort(arr, 0, n - 1);
printf("Array after Sorting: ");
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("
");
return 0;
}
Array before Sorting: 64 34 25 12 22 11 Array after Sorting: 11 12 22 25 34 64
Quick Sort
Quick sort is an efficient divide-and-conquer algorithm that selects a pivot element and partitions the array around it, then recursively sorts the subarrays.
Example of Quick Sort
This C program implements Quick Sort to sort an array of integers −
#include <stdio.h>
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
for(int j = low; j <= high - 1; 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);
}
}
int main() {
int arr[6] = {64, 34, 25, 12, 22, 11};
int n = 6;
printf("Array before Sorting: ");
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("
");
quickSort(arr, 0, n - 1);
printf("Array after Sorting: ");
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("
");
return 0;
}
Array before Sorting: 64 34 25 12 22 11 Array after Sorting: 11 12 22 25 34 64
Conclusion
We've explored five fundamental sorting algorithms in C: Bubble Sort, Insertion Sort, Selection Sort, Merge Sort, and Quick Sort. Each algorithm has different time complexities and use cases, from simple O(n²) algorithms like Bubble Sort to efficient O(n log n) algorithms like Merge Sort and Quick Sort.
