Pragmatics of Sorting
Key Considerations
The Comparison Function
#include <stdlib.h>
/* Sort integers in increasing order */
int intcompare(int *i, int *j)
{
if (*i > *j) return (1);
if (*i < *j) return (-1);
return (0);
}
/* Sort first n elements of array a */
qsort(a, n, sizeof(int), intcompare);#include <algorithm>
#include <vector>
std::vector<int> a = {5, 2, 8, 1};
// Using a lambda as the comparison function
std::sort(a.begin(), a.end(), [](int i, int j) {
return i < j;
});Last updated