Hello! I have a problem: that sorting would work correctly, I have to artificially increase the size of the array! And when changing the sorting to the opposite, everything stops working again.
The code itself:
#include <iostream> using namespace std; int n; int a[1001]; int cmp(const void *x1, const void *x2); int main(int argc, char *argv[]) { cin >> n; for(int i = 0; i < n; i++) cin >> a[i], cout << i << endl; qsort(a, n + 1, sizeof(int), cmp); for(int i = 1; i <= n; i++) cout << a[i] << " "; cout << endl; return 0; } int cmp(const void *x1, const void *x2) { return (*(int*)x1 - *(int*)x2); } What is this line
qsort(a, n + 1, sizeof(int), cmp); Better than this?
qsort(a, n, sizeof(int), cmp); This is a bit strange for me, because I scored an array as it should, from scratch, cmp (auxiliary function) wrote correctly. Such crutches are a little scary, because in some cases they probably spoil everything.
n+1? - PinkTux