Tuesday, 23 October 2018

Comparator function in C

int compare (const void * a, const void * b)
{
 if(*(int*)a < *(int*)b )
    return 1; //if a<b do swapping
 return -1;   //else donot do swapping
}

int main ()
{
int arr[] = {10, 5, 15, 12, 90, 80};
int n = sizeof(arr)/sizeof(arr[0]), i;

qsort (arr, n, sizeof(int), compare);

for (i=0; i<n; i++)
    printf ("%d ", arr[i]);
return 0;
}