sorting an 2d array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2009
Posts: 2
Reputation: mimita is an unknown quantity at this point 
Solved Threads: 0
mimita mimita is offline Offline
Newbie Poster

sorting an 2d array

 
1
  #1
Feb 26th, 2009
Hello,

I'm really a beginner in programming, and i might need you're help.
I have an array[position][3] with fixed coloumns
Position is from 0 to 99 an it looks like this:

array [position][0] = helpFlow;
array [position][1] = i; //i=rows of a [10[10]-grid
array [position][2] = j; //j=column of a [10][10]-grid


So it looks like
0 {5 0 0}
1 {0 0 1}
2 {2 0 2}
3 {7 0 3}
........

The first column, helpFlow, i want to sort from small to large.
But I have no idea how to do that.

Perhaps ist helps if I give you a part of my code

  1. #include <list.h>
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <iostream> //to use cin / cout
  5. #include <string> //to use strings
  6. #include <math.h> //to use mathematical functions
  7. #include <fstream> //to read and write files
  8. #include <cstdlib> //to use exit()
  9. #include <ctime>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <utility>
  14. #include <map>
  15. #include "Unit.h"
  16. #pragma package(smart_init)
  17. #pragma resource "*.dfm"
  18.  
  19. TForm1 *Form1;
  20.  
  21. __fastcall TForm1::TForm1(TComponent* Owner)
  22. : TForm(Owner)
  23. {
  24. calculate_RunonCells();
  25. }
  26.  
  27. //--------------------------------------------------------------------------------
  28.  
  29. void TForm1::calculate_RunonCells()
  30. {
  31. for (int i=0; i< xsize * ysize;i++)
  32. {//1
  33. array[i] [0] =-1;
  34. array[i] [1] =-1;
  35. array [i] [2] =-1;
  36. }//1
  37.  
  38. for (int i=0; i<xsize; i++)
  39. for (int j=0; j<ysize; j++)
  40. {
  41. for (int k=max(0,i-1); k<=min(i+1,xsize-1); k++)
  42. for (int l=max(0,j-1); l<=min(j+1,ysize-1); l++)
  43. {
  44. if (((direction[k][l]-direction[k][l]%ysize)/ysize == i) && (direction[k][l]%ysize == j))
  45.  
  46. {
  47. surroundFlow[i][j]++;
  48. }
  49.  
  50. }
  51.  
  52. helpFlow = surroundFlow[i][j];
  53.  
  54. array [position][0] = helpFlow;
  55. array[position][1] = i;
  56. array [position][2] = j;
  57. position++;
  58. }
  59.  
  60. }
Last edited by mimita; Feb 26th, 2009 at 4:16 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 71
Reputation: r.stiltskin is an unknown quantity at this point 
Solved Threads: 9
r.stiltskin r.stiltskin is offline Offline
Junior Poster in Training

Re: sorting an 2d array

 
0
  #2
Feb 27th, 2009
Conceptually it's the same as sorting a 1D array, since the 2D array is an array of 1D arrays.

Say you want to use bubble sort. So write a "swap3" function that takes two 3-element int arrays and swaps them element by element, i.e.:
arr1[0] <--> arr2[0]
arr1[1] <--> arr2[1]
arr1[2] <--> arr2[2]

Then write your bubble sort function, comparing the first element of each 3-element array with its neighbor, swapping them if the first element of the first array is greater than the first element of the neighbor. It's just like an ordinary bubble sort, except that when you swap you're swapping two 3-element arrays instead of two individual elements, and the sorting comparison will look like:
if( arr[i][0] > arr[i+1][0] ) { ...
instead of
if( arr[i] > arr[i+1] ) { ...
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 2
Reputation: mimita is an unknown quantity at this point 
Solved Threads: 0
mimita mimita is offline Offline
Newbie Poster

Re: sorting an 2d array

 
0
  #3
Feb 27th, 2009
Great! Thanks a lot!!
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 46
Reputation: kbshibukumar is an unknown quantity at this point 
Solved Threads: 7
kbshibukumar kbshibukumar is offline Offline
Light Poster

Re: sorting an 2d array

 
0
  #4
Feb 27th, 2009
To sort an array, see the following article. It is simpler than you writing your own algorithm.

http://cppkid.wordpress.com/?s=how+to+sort+an+array
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC