# sorting an 2d array by keeping track of index and get the first 9 values.

I have this code that :
2) Then it puts the values in an 2d array ( the seccond row is then so i can keep a track of the index).

Then try to sort the array and get the 9 maximum values sorted in an other 2d array MaxValues[][] by keeping the index. It doesn't . all it does is filling all 9 spaces with the bigest value and the index of it.

this is the code that I am using :

``````int gauge[2][16] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
};

int MaxValues[2][9] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 2, 3, 4, 5, 6, 7, 8}
};

int f = 0; //flag value 1
int flag = 0; // flag value 2
void setup()
{
Serial.begin(9600);
}

void loop() {
for (int i = 0; i <= 15; i++) {  // begin and read all 16 analog and place then in the first row of gauge[][] aray
}
//  for (int j = 0; j <= 15; j++) {
//    Serial.print(gauge[0][j]);
//    if (j < 15) {
//      Serial.print(",");
//    } else {
//      Serial.println(",");
//    }
//    delay(10);
//  }
for (int k = 0; k <= 15; k++) {
for (int l = 0; l <= 15; l++) {
f = 15;
for (int x = 0; x <=8; x++) {
if (gauge[0][l] > MaxValues[0][x]) {  // if the value is biger than MaxValue[][] you have to start moving all values one place next and then place the value that was biger at the opened space in MaxValues[][] array
if ( f == 15 ) {
flag = 15;
}
for (int m = 8; m > x; m--) {
if (flag == 15) {
MaxValues[0][m] = MaxValues[0][m - 1];
MaxValues[1][m] = MaxValues[1][m - 1];
MaxValues[0][x] = gauge[0][l];
MaxValues[1][x] = gauge[1][l];
flag = 0;
f = 0;
}
}
}
}
}
}

for (int row = 0; row <=1; row++)
{
for (int col = 0; col <=8 ; col++)
{
Serial.print(MaxValues[row][col]);
Serial.print("\t");
}
Serial.println();
}

}
``````

All i am tryng to do is keep track and sorted witch 9 sensors out of my 16 had the biggest values.

any ideas are more than welcome.

Just do a straight 'bubble-sort'

``````  // if you don't want to sort in place fill anew array with values and index first
for (uint8_t c = 0; c < NR_ENTRIES - 1; c++) { // do a bubble sort
for (uint8_t d = 0; d < NR_ENTRIES - c - 1; d++) {
if (gauge[0][d] > gauge[0][d + 1]) { // compare
for (uint8_t e = 0; e < 2; e++) {  // swap both values & index
int tempval = gauge[e][d];
gauge[e][d] = gauge[e][d + 1];
gauge[e][d + 1] = tempval;
}
}
}
}
// after this fill the other array with the 9 values you want
``````

the first nine will be the biggest and the index will be there ?

``````if (gauge[0][d] > gauge[0][d + 1])
``````

actually this sorts low to high for high to low use

``````if (gauge[0][d] < gauge[0][d + 1])
``````

and yes

``````for (uint8_t e = 0; e < 2; e++) {  // swap both values & index
``````

both values are swapped, that includes the index. Important is of course not just to copy code like this, but to make sure you understand it.
Bubble_sort

it works perfect ! but the code needs a small change to the part of the analoge read.

``````for (int i = 0; i <= 15; i++) {  // begin and read all 16 analog and place then in the first row of gauge[][] aray
gauge[0][i] = midle / 3;
gauge[1][i] = i;
}
``````

before it couldn't keep the index, it was suffling the index.

now with :

``````gauge[1][i] = i;
``````

we create the index from the beggining in every cycle.

and of course i read three times and get the midle of the sum for more stable readings in the analogRead()

Any way ! it is solved ! who ever needs to to the same : the entire code is :

``````int gauge[2][16] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
};

int MaxValues[2][9] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 2, 3, 4, 5, 6, 7, 8}
};
//int tempval[2][1];
int midle;
int f = 0; //flag value 1
int flag = 0; // flag value 2
void setup()
{
Serial.begin(9600);
}

void loop() {
for (int i = 0; i <= 15; i++) {  // begin and read all 16 analog and place then in the first row of gauge[][] aray
gauge[0][i] = midle / 3;
gauge[1][i] = i;
}
for (uint8_t c = 0; c < 15 - 1; c++) { // do a bubble sort
for (uint8_t d = 0; d < 15 - c - 1; d++) {
if (gauge[0][d] > gauge[0][d + 1]) { // compare
for (uint8_t e = 0; e <= 1; e++) { // swap both values & index
int tempval = gauge[e][d];
gauge[e][d] = gauge[e][d + 1];
gauge[e][d + 1] = tempval;
}
}
}
}
for (int row = 0; row <= 1; row++)
{
for (int col = 0; col <=15 ; col++)
{
Serial.print(gauge[row][col]);
Serial.print("\t");
}
Serial.println(" ");
}
}
``````

Fantastic