473,320 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to sort a 2D array.

Hi.
How to sort a 2D array row by row, not the whole array at once.
Is it possible to use the STL Sort to do this. Thanks.
Jun 9 '10 #1
12 4304
whodgson
542 512MB
You could use two (nested) for loops and if a[i][j]< a[i][j-1]swap(a[i][j],a[i][j-1].
But the experts will tell you that arrays are all essentially one dimensional so sorting in the above fashion will not sort the full array.
Jun 10 '10 #2
Banfa
9,065 Expert Mod 8TB
If you have access to the stl you have access to vectors and should not be using arrays.

Essentially you can use std::sort to do the job but how do you determine that 1 row is greater than another?
Jun 10 '10 #3
@Banfa
Hi Banfa.
Can you give an example how to use std::sort in 2D arrays and you know already, let's say row1 > row2. Thanks
Jun 10 '10 #4
Banfa
9,065 Expert Mod 8TB
Actually having experimented I have changed my mind, due to the unique nature of arrays and arrays with 2 indexes it is not possible to sort them on the basis of the first index alone directy.
Jun 10 '10 #5
@Banfa
Hi Banfa.
So how can I acomplish sort a 2D arrays each row independently using C++. Can you tell me. Thanks
Jun 10 '10 #6
Banfa
9,065 Expert Mod 8TB
Not hard implement you sort algorithm to work on a single dimensioned array.

Then modify it to replace those operators that do not work on a double dimensioned array < (and other comparisons) and = (assignment) with functions which will do the job.
Jun 11 '10 #7
whodgson
542 512MB
What do you have against the method outlined in thread #2?
Jun 12 '10 #8
@whodgson
Hello whodgson.
I am sorry for not respond.
I have nothing against the method in the thread # 2. I applied it and only works as you said, in others words it does not works. If you have another idea can you let me know . Thanks for your reply in this thread.
Jun 12 '10 #9
whodgson
542 512MB
Expand|Select|Wrap|Line Numbers
  1. //If this is the 2 dim array
  2. int a [4][5]={{5,7,3,4,12},{9,7,13,2,8},{6,
  3.     14,7,9,15},{4,3,9,11,7}};
  4. //this will print it       
  5. void print(int a[4][5],int n)
  6. {
  7.    for(int i=0;i<4;i++)
  8.      for(int j=0;j<5;j++)
  9.     {  
  10.        cout<<a[i][j]<<",";
  11.       if(j==4)cout<<"\n";
  12.    }    
  13. }
  14. ....and this will sort it.
  15. void sort_rows(int a[4][5],int n)
  16. {
  17.   for(int i=0;i<4;i++)
  18.      for(int j=0;j<5;j++)
  19.      {if(j>0 && a[i][j-1]>a[i][j])
  20.      swap(a[i][j-1],a[i][j]);
  21.     }
  22.  }/*the output is:-    
  23. the 2D array is:
  24. 5,7,3,4,12,
  25. 9,7,13,2,8,
  26. 6,14,7,9,15,
  27. 4,3,9,11,7,
  28. the sorted rows are:
  29. 3,4,5,7,12,
  30. 2,7,8,9,13,
  31. 6,7,9,14,15,
  32. 3,4,7,9,11,
  33. press any key to continue...*/
  34. //But you may need to call sort_rows more than once.
Jun 12 '10 #10
Banfa
9,065 Expert Mod 8TB
@whodgson
@whodgson - I believe you have mis-understood the question.

capablanca is not trying to sort the entries in each row independently of the other rows but rather sort the rows in the array.

Also please be aware you are getting quite close to breaking our rule against posting solutions for other people.
Jun 12 '10 #11
@whodgson
Hello whodgson.
Thanks for your help.
Jun 12 '10 #12
whodgson's sort function won't work reliably unless you perform it 4 times.
(or in general, the number of items per row minus one).


so it needs these modifications to work:
Expand|Select|Wrap|Line Numbers
  1. void swap(int& a, int& b) {
  2.     int c = a;
  3.     a = b;
  4.     b = c;
  5. }
  6.  
  7. void sort_rows(int a[4][5]) {
  8.     for(int i=0;i<4;i++)
  9.     for(int z=1;z<5;z++)
  10.     for(int j=1;j<5;j++)
  11.     {
  12.         if  (a[i][j-1]>a[i][j])
  13.         swap(a[i][j-1],a[i][j]);
  14.     }
  15.  
its not the fastest way, but it gets the job done.
Jun 14 '10 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Boefje | last post by:
Hi, What I want is simple, but I can't figure it out at the moment. Let's say this is an array names $matches: Array ( => Array (
19
by: David | last post by:
Hi all, A while back I asked how to sort an array of strings which would have numerals and I wanted to put them in sequential numerical order. For example: myArray = "file1"; myArray =...
6
by: Tamir Khason | last post by:
Let's say I have Point and I want to sort it circle way (e.g. 2;2.4;1,8;2,8,5;4;8,2;7,1;4 The algo for this is: if ((p.X<p.X & p.Y<Avg(p).Y & p.Y<Avg(p).Y) | (p.X>p.X & p.Y>Avg(p).Y &...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
4
by: christiang | last post by:
Hi guys I'd like to sort a multidimensional array that hasn't numerical index, in fact it is like: Array ( => Array ( => mobile => 1 )
1
by: millw0rm | last post by:
i got 2 array one holds the actual data n another is user generated sort order... how to sort array #1 according to array #2???? array shld be sorted according to sortorder Array #1 ( =Array...
4
by: Santosh Nayak | last post by:
Hi, Is it possible to sort the array of struct based on the data members. e.g. struct { int a ; float b ; char c ; } TEMP ;
5
by: tienlx | last post by:
Hi all, I want to sort an array of struct _line: .... typedef struct _line { int x1; int x2; } line;
4
by: Kovu | last post by:
Hi, I have the next problem. I want to sort the array for the key, using a function. I have been looking for a function to operate with arrays, but I haven't found anything. Ej: Array ( ...
7
by: JWest46088 | last post by:
I am trying to write a program based off of a flowchart. The program is supposed to sort an array. I am having trouble getting it to sort though. I know it has something to do with my code, but I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.