| | |
sorting an 2d array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2009
Posts: 2
Reputation:
Solved Threads: 0
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
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
C++ Syntax (Toggle Plain Text)
#include <list.h> #include <vcl.h> #pragma hdrstop #include <iostream> //to use cin / cout #include <string> //to use strings #include <math.h> //to use mathematical functions #include <fstream> //to read and write files #include <cstdlib> //to use exit() #include <ctime> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <utility> #include <map> #include "Unit.h" #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { calculate_RunonCells(); } //-------------------------------------------------------------------------------- void TForm1::calculate_RunonCells() { for (int i=0; i< xsize * ysize;i++) {//1 array[i] [0] =-1; array[i] [1] =-1; array [i] [2] =-1; }//1 for (int i=0; i<xsize; i++) for (int j=0; j<ysize; j++) { for (int k=max(0,i-1); k<=min(i+1,xsize-1); k++) for (int l=max(0,j-1); l<=min(j+1,ysize-1); l++) { if (((direction[k][l]-direction[k][l]%ysize)/ysize == i) && (direction[k][l]%ysize == j)) { surroundFlow[i][j]++; } } helpFlow = surroundFlow[i][j]; array [position][0] = helpFlow; array[position][1] = i; array [position][2] = j; position++; } }
Last edited by mimita; Feb 26th, 2009 at 4:16 pm.
•
•
Join Date: Feb 2009
Posts: 71
Reputation:
Solved Threads: 9
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] ) { ...
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] ) { ...
•
•
Join Date: Jan 2009
Posts: 46
Reputation:
Solved Threads: 7
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
http://cppkid.wordpress.com/?s=how+to+sort+an+array
![]() |
Similar Threads
- Sorting an Array of numbers in VB 2008 (Visual Basic 4 / 5 / 6)
- sorting 2d array (C++)
- SORTING ARRAY (C++)
- sorting array (C++)
- bubble sorting in an array (C)
- sorting an array of string (C)
Other Threads in the C++ Forum
- Previous Thread: Code works, would appreciate style advice
- Next Thread: wrapper dll
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





