Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[SOLVED] Sort two dimensional array

c++ array

  • This topic is locked This topic is locked
9 replies to this topic

#1 alenn

alenn

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 57 posts
  • Programming Language:C++, C#
  • Learning:C++

Posted 20 October 2012 - 09:00 AM

Hi all

Can you help with sorting two dimensional array. For example:

User enter numbers, something like this:

4 2 5
1 3 7
6 9 8

and then after sorting it should look like this:

1 2 3
4 5 6
7 8 9

I know how to do this by moving all numbers into one dimensional array, then sort it using bubble sort and then return it to two dimensional array, but that's unefficient.
  • 0

#2 neil

neil

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 373 posts
  • Location:Philadelphia
  • Programming Language:C++, Perl, Bash
  • Learning:C, Java, C++, Python, JavaScript, Perl, Bash, Others

Posted 20 October 2012 - 12:08 PM

Use qsort
http://www.cplusplus.../cstdlib/qsort/

edit:
Here's an answer showing how to use it:
http://stackoverflow...ultiple-columns
  • 0

#3 alenn

alenn

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 57 posts
  • Programming Language:C++, C#
  • Learning:C++

Posted 20 October 2012 - 12:31 PM

is there any way for manual implement of this because I need this also in java. Can you show me how to do this using bubble sort (because it's simplest sort algoritm).
  • 0

#4 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 20 October 2012 - 05:38 PM

You can use the bubble sort with an index like this:

my_array[i / 3][i % 3]

This will make accessing it like a linear array.
  • 1

sudo rm -rf / && echo $'Sanitize your inputs!'


#5 alenn

alenn

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 57 posts
  • Programming Language:C++, C#
  • Learning:C++

Posted 22 October 2012 - 12:34 AM

You can use the bubble sort with an index like this:

my_array[i / 3][i % 3]

This will make accessing it like a linear array.


I don't understand how to do it. Can you show me example.
  • 0

#6 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 22 October 2012 - 09:35 AM

for( int i = 0; i < 9; ++i )
    my_array[i / 3][i % 3] = 0;
This will set the entire 2D grid to zero.
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#7 alenn

alenn

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 57 posts
  • Programming Language:C++, C#
  • Learning:C++

Posted 22 October 2012 - 12:52 PM

for( int i = 0; i < 9; ++i )
my_array[i / 3][i % 3] = 0;
This will set the entire 2D grid to zero.


I understand now, but how to make bubble sort out of this, it's kind of tricky
  • 0

#8 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 22 October 2012 - 02:01 PM

Just treat it as if it were a linear array! To make things clearer, use a macro like this:

#define MY_ARRAY_VALUE(index)   my_array[(index) / 3][(index) % 3]

So now you can do this:
if( MY_ARRAY_VALUE(i) > MY_ARRAY_VALUE(i + 1) )
    /* Swap the array values here */

  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#9 alenn

alenn

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 57 posts
  • Programming Language:C++, C#
  • Learning:C++

Posted 23 October 2012 - 08:52 AM

thank you, I understand now
  • 0

#10 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 23 October 2012 - 09:02 AM

This topic has been marked as SOLVED. If you have a similar question or topic, you can go back to the subforum and start a new topic to continue discussions.
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download