matrix

Dec 14, 2011 at 10:55am
Sort array of n x m integers on spiral way from outside to inside to a n x m matrix
Export results to a text file.
Please help me!!!!!!!!!!!!!!!!
Last edited on Dec 14, 2011 at 11:03am
Dec 14, 2011 at 12:22pm
Write your matrix elements into a big (m*n) 1D array. Sort that array. Write them back in a spiral.
Dec 14, 2011 at 2:12pm
can u make it clearly. I still don't have clue or solution with ur suggestion
Dec 14, 2011 at 3:45pm
Lets do it step by step. Firstly, do you have any understanding of how sorting could be done. If, for example, you had an array int my_array[10] = {1, 5, 7, 6, 2, 3, 4, 9, 10, 8};, would you be able to sort that? If no, google "bubble sort". Wikipedia has plenty of pseudo code and if you're too lazy for that, I'm sure you could find yourself an example C++ implementation.
Dec 15, 2011 at 5:37am
I say that it requires u to arrange in a "matrix" , not in order, buble sort is useless
Dec 15, 2011 at 6:27am
You're not going to get anywhere if you try to do everything at the same time. It's obvious that this algorithm can be implemented as in my first post. Just try doing it on paper. And now, bubble sort isn't useless any more.

Writing a spiral could be more tricky. I suggest writing a function that puts the numbers on the outermost ring (using four for loops) and then calls itself for the inner ones. Example:

* * *    1 2 *    1 2 3    1 2 3    1 2 3
* * * => * * * => * * 4 => * * 4 => 8 * 4
* * *    * * *    * * *    * 6 5    7 6 5
and finally recursively call the same function for the inner * (maybe I should have used a bigger matrix for an example..)
* => 9
Topic archived. No new replies allowed.