qsort in c
#include <stdlib.h>
int cmpfunc (const void * a, const void * b) {
return ( *(int*)a - *(int*)b );
}
int main () {
//n is number of elements in arr(size f that arr)
qsort(arr, n, sizeof(int), cmpfunc);
}
qsort in c
#include <stdlib.h>
int cmpfunc (const void * a, const void * b) {
return ( *(int*)a - *(int*)b );
}
int main () {
//n is number of elements in arr(size f that arr)
qsort(arr, n, sizeof(int), cmpfunc);
}
quick sort predefined function in c++
#include <cstdlib>
//declare compare
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;
}
//fuction used
qsort(arr,num,sizeof(int),compare);
qsort
#include <stdlib.h>
/* Comparison function. Receives two generic (void) pointers to the items under comparison. */
int compare_ints(const void *p, const void *q) {
int x = *(const int *)p;
int y = *(const int *)q;
/* Avoid return x - y, which can cause undefined behaviour
because of signed integer overflow. */
if (x < y)
return -1; // Return -1 if you want ascending, 1 if you want descending order.
else if (x > y)
return 1; // Return 1 if you want ascending, -1 if you want descending order.
return 0;
// All the logic is often alternatively written:
return (x > y) - (x < y);
}
/* Sort an array of n integers, pointed to by a. */
void sort_ints(int *a, size_t n) {
qsort(a, n, sizeof(*a), compare_ints);
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us