# Thread: Sort a 2D array

1. ## Sort a 2D array

I have a 2-Dimensional array Final[20][7] and i need to sort it according to the left most integers. Now the tricky part is that I can't use any returns or and real functions. It pretty much has to be bruce force with what I assume will be loops.

This is tetious and energy consuming but its the only way I can do it without functions. Anyone have any simple code that I could work around to get this working?

2. Just implement something simple like bubblesort, and instead of making it a function, put the code directly in your main().

What is this requirement about no functions anyways, what purpose does that serve?

3. I would not mind helping but its useful for you to post code of your idea and then tell us where you ran into problems. If I just post code of how I would do it I might be inadvertently doing your homework for you.

4. What do you mean by a "real function"? Does a lambda expression count?

5. The compiler I am using cannot use more than one function -_- sucks.

As for the real functions i meant i cant use anything other than my main()...

And don't worry it's not homework, this is for personal data analysis

6. Originally Posted by dciampa
The compiler I am using cannot use more than one function -_- sucks.

As for the real functions i meant i cant use anything other than my main()...
I really want to hear which compiler does this!!!!!

7. Originally Posted by std10093
I really want to hear which compiler does this!!!!!
lol its built into ROOT (a data analysis program)

8. Hey so if I were to use a bubble sort and sort it according to the first column on the left then won't I only be sorting those numbers? I need the whole row to go wherever the column goes. Do u get that or did it confuse you? Haha

9. Bubble sort would be:

Original:
4 6 4 8 6
6 1 3 2 8
1 4 7 6 0

Sorted:
1 6 4 8 6
4 1 3 2 8
6 4 7 6 0

See how it would only sort the first column?

10. Originally Posted by dciampa
See how it would only sort the first column?
Nobody ever said you could just copy/paste any bubble sort implementation that Google turns up. You will need to customize the sorting function so it moves the entire row, instead of only the first element.

If your Bubble Sort looks like the standard one.

Then you just need to change the swap() function, basically.

11. This is my code, the array wont bring the rows with the assorted number

Code:
```{
int array[5][5] = {5,4,8,3,8,9,2,5,4,9,7,6,1,4,3,5,6,3,9,7,8,3,4,2,6};
int temp[5][5] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

cout << "=============UNSORTED============="<< endl;
for (int w=0;w<5;w++){
for (int k=0; k<5 ; k++){
cout << array[w][k] << "\t";

}
cout << endl;
}

cout << "=============SORTED==============" << endl;
for(int i=0; i<5;i++){
for(int j=0; j<5;j++){
if(array[j][0] > array[j+1][0])
{
temp[0][0] = array[j][0];
array[j][0] = array[j+1][0];
array[j+1][0] = temp[0][0];
}
}
}

for (int w=0;w<5;w++){
for (int k=0; k<5 ; k++){
cout << array[w][k] << "\t";

}
cout << endl;
}

}```
My output is:

=============UNSORTED=============
5 4 8 3 8
9 2 5 4 9
7 6 1 4 3
5 6 3 9 7
8 3 4 2 6
=============SORTED==============
5 4 8 3 8
5 2 5 4 9
7 6 1 4 3
8 6 3 9 7
9 3 4 2 6

Now how to get the rows to go with the assigned sorted numbers?

12. Code:
```for(int i=0; i<5;i++){
for(int j=0; j<5;j++){
if(array[j][0] > array[j+1][0])
{
temp[0][0] = array[j][0];
array[j][0] = array[j+1][0];
array[j+1][0] = temp[0][0];
}
}
}```
In your "if()" block, you're swapping the values at "array[j][0]" and "array[j+1][0]". Perhaps you could add something in there to swap other elements, too.

Popular pages Recent additions