📊 C Sorting Algorithms Collection
A comprehensive C library featuring the most popular sorting algorithms categorized by stability. This educational project offers clean code and useful array helper functions, making it a perfect foundation for learning algorithms and data structures!
🚀 Implemented Algorithms
The table below presents all the sorting algorithms available in this library, along with their average time complexity, space complexity, and stability characteristics.
| Algorithm Name | Time Complexity (Avg) | Space Complexity | Stability |
|---|
| Bubble Sort | O(n^2) | O(1) | Stable |
| Insertion Sort | O(n^2) | O(1) | Stable |
| Merge Sort | O(n log n) | O(n) | Stable |
| Counting Sort | O(n + k) | O(n + k) | Stable |
| Selection Sort | O(n^2) | O(1) | Unstable |
| Quick Sort | O(n log n) | O(log n) | Unstable |
| Heap Sort | O(n log n) | O(1) | Unstable |
🛠️ Helper Functions
In addition to the sorting algorithms, this project includes a set of handy tools for array manipulation:
swap_int - Safely swaps the values of two variables using pointers.
print_int_array - Quickly prints the contents of an array to the console.
find_min - Returns a pointer to the smallest element within a specified array range.
partition - The dividing module used in the Quick Sort algorithm.
merge - Merges two sorted subarrays (used in Merge Sort).
heapify - A utility function to build and maintain a Max-Heap data structure.
💻 How to Use
- Clone this repository to your local machine.
- Add the
sorting.h and sorting.c files to your project directory.
- Include the header file in your main program:
#include <stdio.h>
#include "sorting.h"
int main(void) {
int arr[] = {64, 25, 12, 22, 11};
size_t n = sizeof(arr) / sizeof(arr[0]);
printf("Array before sorting:\n");
print_int_array(arr, n);
printf("\n");
// Call the sorting algorithm of your choice
merge_sort(arr, 0, n - 1);
printf("Array after sorting:\n");
print_int_array(arr, n);
printf("\n");
return 0;
}