I have a double-type array with 2 columns, and a max number of rows of 1000, that I want to sort based on the first element of each row, then move the entire row. In essence, I want the second column element to have no impact.
I introduce the array as:
double A[1000][2];
in my main. An example of A could be:
18.0 2.0
5.5 3.5
10.0 8.1
4.0 2.5
After sorting, I would want it to look like this:
4.0 2.5
5.5 3.5
10.0 8.1
18.0 2.0
It would also be nice to know how to have it sorted in reverse, such that it looks like this:
18.0 2.0
10.0 8.1
5.5 3.5
4.0 2.5
Notice how it's only being sorted based on the value in the first column, then the whole row gets switched.
I tried using a pretty standard insertion sort algorithm, with just changing the input argument to be the two dimensional array, and having a block of code inside the function which changes both elements of the row, but I kept getting this error:
error: array has incomplete element type 'double []' sort_double_array(double A[][], int n) {
How do I do this? Would be nice to be able to do it using insertion sort. I can post the function I'm using for insertion sort, but it works perfectly for one dimensional arrays.
Thanks so much in advance, really stuck here.
Cheers,
James.
qsortby write compare function.