Skip to content

Instantly share code, notes, and snippets.

@tylerlrhodes
Created July 8, 2018 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylerlrhodes/dfd17f883c0041f289ddeb6820e34dbe to your computer and use it in GitHub Desktop.
Save tylerlrhodes/dfd17f883c0041f289ddeb6820e34dbe to your computer and use it in GitHub Desktop.
QSort demo in C for Higher Order Function
#include <stdio.h>
#include <stdlib.h>
void print_array(int* array, size_t size)
{
for (unsigned i = 0; i < size; ++i)
{
printf("%d\n", *array++);
}
}
int compare(const void* p, const void* q)
{
int l = *(const int*)p;
int r = *(const int*)q;
return l - r;
}
int main()
{
printf("QuickSort Demo..");
int arr[] = { 7, 5, 22, 4, 3, 100, 19, 20 };
int size = sizeof(arr) / sizeof(sizeof(arr[0]));
printf("Before sort:\n");
print_array(arr, size);
qsort((void*)arr, size, sizeof(arr[0]), compare);
printf("After sort:\n");
print_array(arr, size);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment