How to sort a 2D array.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • capablanca
    New Member
    • May 2010
    • 60

    #1

    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.
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    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.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      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?

      Comment

      • capablanca
        New Member
        • May 2010
        • 60

        #4
        Originally posted by Banfa
        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?
        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

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          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.

          Comment

          • capablanca
            New Member
            • May 2010
            • 60

            #6
            Originally posted by Banfa
            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.
            Hi Banfa.
            So how can I acomplish sort a 2D arrays each row independently using C++. Can you tell me. Thanks

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              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.

              Comment

              • whodgson
                Contributor
                • Jan 2007
                • 542

                #8
                What do you have against the method outlined in thread #2?

                Comment

                • capablanca
                  New Member
                  • May 2010
                  • 60

                  #9
                  Originally posted by whodgson
                  What do you have against the method outlined in thread #2?
                  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.

                  Comment

                  • whodgson
                    Contributor
                    • Jan 2007
                    • 542

                    #10
                    Code:
                    //If this is the 2 dim array
                    int a [4][5]={{5,7,3,4,12},{9,7,13,2,8},{6,
                        14,7,9,15},{4,3,9,11,7}};
                    //this will print it       
                    void print(int a[4][5],int n)
                    {
                       for(int i=0;i<4;i++)
                         for(int j=0;j<5;j++)
                        {  
                           cout<<a[i][j]<<",";
                          if(j==4)cout<<"\n";
                       }    
                    }
                    ....and this will sort it.
                    void sort_rows(int a[4][5],int n)
                    {
                      for(int i=0;i<4;i++)
                         for(int j=0;j<5;j++)
                         {if(j>0 && a[i][j-1]>a[i][j])
                         swap(a[i][j-1],a[i][j]);
                        }
                     }/*the output is:-    
                    the 2D array is:
                    5,7,3,4,12,
                    9,7,13,2,8,
                    6,14,7,9,15,
                    4,3,9,11,7,
                    the sorted rows are:
                    3,4,5,7,12,
                    2,7,8,9,13,
                    6,7,9,14,15,
                    3,4,7,9,11,
                    press any key to continue...*/
                    //But you may need to call sort_rows more than once.

                    Comment

                    • Banfa
                      Recognized Expert Expert
                      • Feb 2006
                      • 9067

                      #11
                      Originally posted by whodgson
                      What do you have against the method outlined in thread #2?
                      @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.

                      Comment

                      • capablanca
                        New Member
                        • May 2010
                        • 60

                        #12
                        Originally posted by whodgson
                        Code:
                        //If this is the 2 dim array
                        int a [4][5]={{5,7,3,4,12},{9,7,13,2,8},{6,
                            14,7,9,15},{4,3,9,11,7}};
                        //this will print it       
                        void print(int a[4][5],int n)
                        {
                           for(int i=0;i<4;i++)
                             for(int j=0;j<5;j++)
                            {  
                               cout<<a[i][j]<<",";
                              if(j==4)cout<<"\n";
                           }    
                        }
                        ....and this will sort it.
                        void sort_rows(int a[4][5],int n)
                        {
                          for(int i=0;i<4;i++)
                             for(int j=0;j<5;j++)
                             {if(j>0 && a[i][j-1]>a[i][j])
                             swap(a[i][j-1],a[i][j]);
                            }
                         }/*the output is:-    
                        the 2D array is:
                        5,7,3,4,12,
                        9,7,13,2,8,
                        6,14,7,9,15,
                        4,3,9,11,7,
                        the sorted rows are:
                        3,4,5,7,12,
                        2,7,8,9,13,
                        6,7,9,14,15,
                        3,4,7,9,11,
                        press any key to continue...*/
                        //But you may need to call sort_rows more than once.
                        Hello whodgson.
                        Thanks for your help.

                        Comment

                        • cottonvibes
                          New Member
                          • Jun 2010
                          • 1

                          #13
                          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:
                          Code:
                          void swap(int& a, int& b) {
                          	int c = a;
                          	a = b;
                          	b = c;
                          }
                          
                          void sort_rows(int a[4][5]) {
                          	for(int i=0;i<4;i++)
                          	for(int z=1;z<5;z++)
                          	for(int j=1;j<5;j++)
                          	{
                          		if  (a[i][j-1]>a[i][j])
                          		swap(a[i][j-1],a[i][j]);
                          	}
                          }
                          its not the fastest way, but it gets the job done.

                          Comment

                          Working...