Common Sorting Algorithms in C
Course Title: Mastering C: From Fundamentals to Advanced Programming Section Title: Sorting and Searching Algorithms Topic: Common sorting algorithms: bubble sort, selection sort, and quicksort.
Introduction
Sorting algorithms are a fundamental part of computer programming, and C is no exception. In this topic, we'll explore three common sorting algorithms: bubble sort, selection sort, and quicksort. These algorithms are widely used in various applications, including data analysis, file systems, and databases. Understanding these algorithms is crucial for any aspiring C programmer.
Bubble Sort
Bubble sort is a simple sorting algorithm that repeatedly iterates through a list of elements, comparing adjacent elements and swapping them if they are in the wrong order. The algorithm continues until no more swaps are needed, indicating that the list is sorted.
Here's a step-by-step explanation of the bubble sort algorithm:
- Start from the first element of the list.
- Compare the current element with the next element.
- If the current element is greater than the next element, swap them.
- Move to the next element and repeat steps 2-3 until the end of the list is reached.
- If any swaps were made in step 3, repeat the process from the beginning of the list.
- Continue this process until no more swaps are needed.
Here's an example implementation of bubble sort in C:
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Selection Sort
Selection sort is another simple sorting algorithm that works by selecting the smallest (or largest, depending on the sorting order) element from the unsorted portion of the list and swapping it with the first element of the unsorted portion.
Here's a step-by-step explanation of the selection sort algorithm:
- Start from the first element of the list.
- Find the minimum (or maximum) element in the unsorted portion of the list.
- Swap the minimum (or maximum) element with the first element of the unsorted portion.
- Move to the next element and repeat steps 2-3 until the end of the list is reached.
Here's an example implementation of selection sort in C:
#include <stdio.h>
void selectionSort(int arr[], int n) {
int i, j, min;
for (i = 0; i < n - 1; i++) {
min = i;
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[min]) {
min = j;
}
}
if (min != i) {
int temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Quicksort
Quicksort is a divide-and-conquer algorithm that works by selecting a 'pivot' element from the list and partitioning the other elements into two lists, according to whether they are less than or greater than the pivot. The sub-arrays are then recursively sorted.
Here's a step-by-step explanation of the quicksort algorithm:
- Choose a pivot element from the list.
- Partition the list into two sub-arrays, one with elements less than the pivot and one with elements greater than the pivot.
- Recursively apply the quicksort algorithm to the sub-arrays.
Here's an example implementation of quicksort in C:
#include <stdio.h>
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
int temp;
for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {
i++;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pivot = partition(arr, low, high);
quickSort(arr, low, pivot - 1);
quickSort(arr, pivot + 1, high);
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Comparison of Sorting Algorithms
Algorithm | Time Complexity (Best) | Time Complexity (Average) | Time Complexity (Worst) |
---|---|---|---|
Bubble Sort | O(n) | O(n^2) | O(n^2) |
Selection Sort | O(n^2) | O(n^2) | O(n^2) |
Quicksort | O(n log n) | O(n log n) | O(n^2) |
Conclusion
In this topic, we explored three common sorting algorithms: bubble sort, selection sort, and quicksort. Each algorithm has its own strengths and weaknesses, and the choice of algorithm depends on the specific problem and constraints.
- Bubble sort and selection sort are simple and easy to implement but have high time complexities.
- Quicksort is a divide-and-conquer algorithm that has an average time complexity of O(n log n) but can have a worst-case time complexity of O(n^2).
Practice Questions
- Implement bubble sort and selection sort for a list of strings.
- Analyze the time complexity of quicksort and explain why it is considered to be efficient.
- Compare the performance of bubble sort, selection sort, and quicksort using a benchmarking tool.
Additional Resources
Leave a Comment or Ask for Help
If you have any questions or need help with any of the concepts discussed in this topic, please leave a comment below. We'll be happy to help.
Next Topic: Searching Algorithms
In the next topic, we'll explore searching algorithms, including linear search and binary search. These algorithms are used to find an element in a list and are essential in many applications, including databases and file systems.
Images

Comments