c++ help sorting arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chry44
    New Member
    • Dec 2013
    • 5

    c++ help sorting arrays

    Does anyone know how to sort a 2D array diagonally? Lets suppose I've got the following array:

    Code:
    2  5  7  4 8
    3  11 14 5 2
    6  3  12 9 1
    7  15 11 4 2
    8  16 13 5 1
    I want to create a function that sort them diagonally like this:

    http://tinypic.com/view.php?pic=15 2dvk2&s=5#.Usb8 v7RTMo4

    I am a beginner in C++...
    Please if anyone know please help me...
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    I would say the problem consists of two steps:
    1. Finding an algorithm that can convert a position in the 2D array to the number of the field in that array if you wrote them in order of use and vice versa
    2. Sorting the 2D array as if it were a 1D array

    So, your code would look something like this:
    Code:
    typedef struct {
        int x;
        int y;
    } Coords;
    
    Coords oneDtoTwoD(int pos);
    
    int twoDtoOneD(int x, int y);
    
    void sort(int** array) {
        // Normal sorting algorithm but rather than taking
        // the entry from position n you take that from
        // oneDtoTwoD(n) and rather than putting the
        // element into position n you put it into
        // oneDtoTwoD(n).
        // Alternatively the twoDtoOneD(x, y) function could
        // be used to translate the 2D array into a 1D one
        // and after that has been sorted you can turn it
        // back to 2D with the oneDtoTwoD(n) function.
    }

    Comment

    • chry44
      New Member
      • Dec 2013
      • 5

      #3
      we didn't learn the algorithm method :/ the only method we learned is the bubble sort...

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Bubble sort is an algorithm. You can use that. :-)

        Comment

        • chry44
          New Member
          • Dec 2013
          • 5

          #5
          oh really i didn't know that... :P
          can you write the code for me please?

          Comment

          • Nepomuk
            Recognized Expert Specialist
            • Aug 2007
            • 3111

            #6
            No, I can't. And not only because that is against our posting guidelines but also because this is something you're supposed to learn. If you don't learn this you won't be able to solve more difficult tasks. We here at bytes.com will be happy to help you but we won't do your work for you.

            Comment

            • chry44
              New Member
              • Dec 2013
              • 5

              #7
              oh okay :( but our teacher didn't teach us any of that what i am supposed to do... :/ please i am begging you i swear i tried everything...

              Comment

              • Nepomuk
                Recognized Expert Specialist
                • Aug 2007
                • 3111

                #8
                If you had tried everything you would have solved the problem. Learning to program isn't always easy, I know, but if you don't solve problems like this yourself you'll likely never be any good at it.

                You said you learned about bubble sort. Do you understand that algorithm? Go through it again, trying to really understand it. Write it down more like a cooking recipe rather than computer code. Then, based only on the recipe, write it as code again. That way you'll make sure you really, truly understand it.

                When it comes to the 2D array, the way that's probably easiest to understand is first converting it to a 1D array, sorting that and then converting it back. Here, look at this:
                Code:
                 1  2  4  7
                 3  5  8 11
                 6  9 12 14
                10 13 15 16
                This displays the order in which the elements in a 4x4 2D array would be accessed. So, if you translate that 2D array to this 1D array
                Code:
                 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
                it can easily be sorted. Then translate it back. It's very similar for the 5x5 array in that picture you posted.

                Comment

                Working...