Some of comparision based sorting algorithms implemented in generic form using C
You can use any of these implementation by including to the source. Most of algorithms implemented std qsort() fashion
void qsort( void *ptr, size_t count, size_t size, int (*cmp)(const void *, const void *) )
If you want to use insertion sort for example, you should try
void insertionSort( void *ptr, size_t count, size_t size, int (*cmp)(const void *, const void *) )
where ptr is pointer to array, count is number of elements in the array, size is size of every single element of the array, cmp is comparision function, an function pointer to int. You can use following comparision function:
#define TYPE int
static int cmp(const void *a, const void *b){
const TYPE da = *((const TYPE *) a);
const TYPE db = *((const TYPE *) b);
return (da < db) ? 1 : (da == db) ? 0 : -1;
}
| Sort | Worst Case | Average Case | Best Caase |
|---|---|---|---|
| Bogo | O((n+1)!) | O((n+1)!) | O(n) |
| Bubble | O(n2) | O(n2) | O(n) |
| Cocktail Shaker | O(n2) | O(n2) | O(n) |
| Selection | O(n2) | O(n2) | O(n2) |
| Gnome | O(n2) | O(n2) | O(n2) |
| Comb | O(n2) | O(nlogn) | O(nlogn) |
| Insertion | O(n2) | O(n2) | O(n) |
| Shell | O(n(log(n))2) | O(n(log(n))2) | O(nlogn) |
| Merge Sort | O(nlogn) | O(nlogn) | O(nlogn) |
| Quick Sort | O(n2) | O(nlogn) | O(nlogn) |
| Heap Sort | O(nlogn) | O(nlogn) | O(nlogn) |