Sort a 2D array

This is a discussion on Sort a 2D array within the C++ Programming forums, part of the General Programming Boards category; I have a 2-Dimensional array Final[20][7] and i need to sort it according to the left most integers. Now the ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    23

    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. #2
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    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?
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  3. #3
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    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. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    What do you mean by a "real function"? Does a lambda expression count?
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    23
    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. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by dciampa View Post
    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!!!!!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    23
    Quote Originally Posted by std10093 View Post
    I really want to hear which compiler does this!!!!!
    lol its built into ROOT (a data analysis program)

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    23
    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. #9
    Registered User
    Join Date
    Oct 2012
    Posts
    23
    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. #10
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by dciampa View Post
    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.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  11. #11
    Registered User
    Join Date
    Oct 2012
    Posts
    23
    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?
    Last edited by dciampa; 12-05-2012 at 11:57 AM.

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    2,242
    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 subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 05-31-2012, 01:11 AM
  2. Replies: 1
    Last Post: 01-26-2010, 08:02 AM
  3. Replies: 4
    Last Post: 12-06-2009, 11:27 AM
  4. how to sort out an array?
    By Chobo_C++ in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2004, 04:33 PM
  5. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 07:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21