1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
int _compare_ints(const void* a1, const void* a2) {
int const* a_n1 = a1;
int const* a_n2 = a2;
int n1 = *a_n1;
int n2 = *a_n2;
if(n1 < n2) {
return -1;
}
else if(n1 == n2) {
return 0;
}
else {
assert(n1 > n2);
return 1;
}
}
int main(int argc, char* argv[]) {
int numbers[] = { 4, 1, 3, 2 };
size_t num_numbers = sizeof numbers / sizeof numbers[0]; // Do not use for ▒▒▒* !!!
// Sort array using qsort(…)
qsort(numbers, num_numbers, sizeof numbers[0], _compare_ints);
assert(numbers[0] == 1 && numbers[1] == 2 && numbers[2] == 3 && numbers[3] == 4);
return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2024 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.