c qsort code example
Example 1: qsort in c
#include <stdlib.h>
int cmpfunc (const void * a, const void * b) {
return ( *(int*)a - *(int*)b );
}
int main () {
qsort(arr, n, sizeof(int), cmpfunc);
}
Example 2: quick sort predefined function in c++
#include <cstdlib>
int compare(const void* a, const void* b)
{
const int* x = (int*) a;
const int* y = (int*) b;
if (*x > *y)
return 1;
else if (*x < *y)
return -1;
return 0;
}
qsort(arr,num,sizeof(int),compare);
Example 3: definition qsort c program
void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*))