5

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.

1
  • use qsort by write compare function. Commented Apr 23, 2014 at 8:43

4 Answers 4

2

Try declaring the function as sort_double_array(double A[][2], int n);

Multidimensional array must have bounds for all dimensions except the first.

Sign up to request clarification or add additional context in comments.

4 Comments

void sort_double_array(double A[][2], int n) { int i, j; for (i=1; i<n; i++) { /*swap A[i] left into correct position */ for (j=i-1; j>=0 && A[j+1]<A[j]; j--) { double_swap(&A[j][0], &A[j+1][0]); double_swap(&A[j][1], &A[j+1][1]); } } } This is the block of code I'm using (insertionsort) to sort things. Is there anything wrong here? Because I implemented the method you suggested, and whilst it didn't return any errors upon compiling, it doesn't sort the array.
A[j+1]<A[j] - This seems wrong - aren't you comparing addresses? Where is the other subscript?
You are fantastic. This was a remnant from when the function worked for single dimension arrays. Thanks so much. Just added in the [0]'s. Works perfectly. Now, how would invert it such that it sorts in descending order from the biggest number downwards? Can I just flip that inequality sign?
Yes, that should do the job. Don't forget to accept the answer? :)
1

Try prototyping your sorting function as void sort_double_array(double *A[], int n). For extra bonus points, use size_t rather than int.

2 Comments

Would that work if my argument is a two-dimensional array when I call that function?
With that prototype A is an array of pointers which is not the same as a pointer to an array, which is what the OP wants to pass.
1

For this specific case, it sounds like each row has a significant meaning, it seems to be some kind of object? Then I would recommend to forget about the 2D array and declare it as an array of structs. The struct would look something like:

#define N 2

typedef struct
{
  double data [N];
} my_data_t;

And then you declare an array of that struct:

my_data_t arr [1000];

Now your question is narrowed down to "how to sort an array of x". There's plenty of information to be had about that all over the web. If you don't want to implement the sort algorithm yourself (which you would only do for educational purposes), I would suggest to use the qsort() function in stdlib.h. For this qsort that works is : qsort (arr, sizeof(arr)/sizeof(*arr), sizeof(*arr), less);

For qsort, you would implement the comparison function used by the sort. Something like this:

int less (const void* a, const void* b)
{
  const my_data_t* ptr_a = a;
  const my_data_t* ptr_b = b;

   return (int)(ptr_a->data[0] - ptr_b->data[0]);
}

To change the sorting order, implement a similar function "more".

2 Comments

I haven't really learnt/done any work with structures/typedefs...is there any way to do it in the way of described? iterating over only one column of the array, then switching the whole row? I tried declaring the function as void sort_double_array(double A[][2], int n); but I don't think it's worked as intended. Is there an easy way to print an array like I have in the O.P, to check things are going well? :) Edit: I just iterated over the array, so printing isn't an issue, but the sort isn't going as planned.
@JamesAdams You need to know fundamental C before studying sorting algorithms.
0
#include<stdio.h>
#include<stdlib.h>
int main()
{
        int arr[4][2] = {18,2,5,3,10,8,4,2};
        int row=4,col=2;
        int i,j,k=0,x,temp;
        for(i=0;i<row;i++)
        {
                for(j=i+1;j<row;j++)
                {
                        if(arr[i][k] > arr[j][k])
                        {
                            for(x=0;x<2;x++) {
                                temp=arr[i][x];
                                arr[i][x]=arr[j][x];
                                arr[j][x]=temp;
                                }
                        }
                }
        }
        for(i=0;i<row;i++)
        {
                for(j=0;j<col;j++)
                printf("%d ", arr[i][j]);
                printf("\n");
        }
}

1 Comment

brute force! old school

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.