2

I'm trying to sort the elements within the individual rows of a 2D array. I understand how to sort the elements inside a 1D array, but I am having serious trouble getting it to sort the 2D.

Code for the 1D array:

for (i = 0; i < size; i++)
{
    for (j = i +1; j < size; ++j)
    {
        if (array2[i] > array2[j])
        {
            swap = array2[i];
            array2[i] = array2[j];
            array2[j] = swap;
        }
    }
}

What I want to do: 2D Array before sorting

9 2 0 1 6 3
0 9 1 2 3 8
4 2 5 4 3 6
3 6 4 3 9 3
0 2 1 2 0 4
4 1 9 4 2 7

2D array after sorting:

0 1 2 3 6 9
0 1 2 3 8 9
2 3 4 4 5 6
3 3 3 4 6 9
0 0 1 2 2 4
1 2 4 4 7 9

My code for the 2D so far:

size: the user defined dimensions (in the above case it is 6)

for (i = 0; i < size; i++)
{
    for (j = 0; j < size; j++)
    {
        if(array[i][j] > array[i][j+1])
        {
            swap = array[i][j];
            array[i][j] = array[i][j+1];
            array[i][j+1] = swap;
        }
    }
}

Any help or advice would be much appreciated. Thank you all.

Amit Sharma
  • 1,987
  • 2
  • 18
  • 29
amstl14
  • 31
  • 1
  • 1
  • 3
  • Could you run `qsort` over the rows or are you contractually obliged to use your own sort? – ooga Oct 31 '14 at 19:26
  • In your 1D example the `i` loop is wrong. It should be `for (i = 0; i < size-1; i++)` because `j` starts at `i + 1`. This error is also in your 2D example. Write a function that sorts a 1D array passed as an argument (with size). Then build on that to sort each array in your 2D example. – Weather Vane Oct 31 '14 at 19:28
  • It appears to me a bubble sort assignment. Just make a function of the bubble sort (your code for 1 D array) and call it for each row of the 2D array – Peter Li Oct 31 '14 at 19:31
  • For C++ and Java this may help. http://stackoverflow.com/questions/20931669/sort-a-2d-array-in-c-using-built-in-functionsor-any-other-method – prime Jan 25 '17 at 14:18

4 Answers4

4

If you want to use your single array sorting algorithm (bubble sort) to sort the two dimensional array then you have to add the another for loop: An outer for loop which will take care of each row. Let's say m is number of row and n is number of column.

for(k=0; k< m; k++) {
   for (i = 0; i < n; i++) {
     for (j = i +1; j < n; ++j) {
       if (array2[k][i] > array2[k][j])  {
           int swap = array2[k][i];
           array2[k][i] = array2[k][j];
           array2[k][j] = swap;
       }
     }
   } 
}

But this is not an efficient approach to sort the array, it's time complexity will be O(mn^2)

Amit Sharma
  • 1,987
  • 2
  • 18
  • 29
  • for (k = 0; k < size; k++) { for (i = 0; i < size; i++) { for (j = i+1; j < size; ++j) { if (array[k][i] > array[k][j]) { swap = array[k][i]; array[k][i] = array[k][j]; array[k][j] = swap; } } } } Working code. Thank you. – amstl14 Oct 31 '14 at 19:59
  • Yes,, in case you have row==column then its OK. :) – Amit Sharma Oct 31 '14 at 20:02
0

copy all the elements of the 2d array into an 1d array then apply any sorting algorithm on 1d array & then copy back the sorted 1d array to the 2d array. please don't mind if you have a better solution then post it that will be helpfull for me.

Engineer
  • 459
  • 1
  • 4
  • 8
  • The question asks to sort _individual rows of a `2D` array_. This would just sort the `2D` array as if it was `1D` (it is, in memory). Also, there is no need to copy the array, bubble sort is in place - copying arrays is very expensive. – Ramon Aug 11 '16 at 17:35
  • sir, I have already made it clear that if you have any perfect logic to sort whole 2d array then please post it. sorting n rows is equal to sorting n 1d arrays. & i don't find sorting n rows helpfull because that's a very simple task. – Engineer Aug 12 '16 at 04:07
0

You can simply use STL to sort 2D array row-wise..

for (i=0;i<n;i++){
    for ( j=0;j<n;j++){
        cin>>a[i][j];
    }
    sort(a[i],a[i]+n);
  }
Divyanshi
  • 1
  • 2
0
int tmp,l;
for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
        tmp = a[i][j];
        l = j + 1;
        for (int k = i; k < 2; k++) {
            while (l < 2) {
                if (tmp < a[k][l]) {
                    tmp = a[k][l];
                    a[k][l] = a[i][j];
                    a[i][j] = tmp;
                }
                l++;
            }
            l = 0;
        }
    }
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241