Created
July 8, 2018 18:11
-
-
Save tylerlrhodes/dfd17f883c0041f289ddeb6820e34dbe to your computer and use it in GitHub Desktop.
QSort demo in C for Higher Order Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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