O'Reilly logo

Learn C the Hard Way: A Clear & Direct Introduction To Modern C Programming by Zed A. Shaw

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Exercise 35. Sorting and Searching

In this exercise, I’m going to cover four sorting algorithms and one search algorithm. The sorting algorithms are going to be quick sort, heap sort, merge sort, and radix sort. I’m then going to show you how do a to binary search after you’ve done a radix sort.

However, I’m a lazy guy, and in most standard C libraries you have existing implementations of the heapsort, quicksort, and merge sort algorithms. Here’s how you use them:

darray_algos.c

  1   #include <lcthw/darray_algos.h>   2   #include <stdlib.h>   3   4   int DArray_qsort(DArray * array, DArray_compare cmp)   5   {   6       qsort(array->contents, DArray_count(array), sizeof(void *), cmp);   7       return 0;   8    ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required