# Thread: The 2d selection sort

1. ## The 2d selection sort

Hi everyone,

So I have finally got my 2d array put together and now I need to sort the random numbers that are in the elements. I feel like I am close because I put together a one dimensional array and sorted it with no problems.

My problem is that I am a little unsure how to implement the knowledge I have about selection sort with one dimensional arrays on a two dimensional platform. I want them to sort left to right, top to bottom. So essentially, I need it to sort in the fashion that a nested for loop inserts numbers into the array elements.

I have the concept of using nested for loops, but I am having trouble wrapping my head around working it into the void function.

As usual and per the tradition of this forum, I did my best to get it, but I am getting errors and at this point it feels like I am jamming variables into spots that I am unsure if they even go there.

I would greatly appreciate any help on this issue.

Thank you, and here is my code so far. Techgique.

PS - If needed, Win 7, DevC, i7
Code:
```#include <iostream>
#include <cstdlib>
using namespace std;
// Random 2d array 100 numbers sorted by Steve P.
const int amount = 100;

int main()
{
void selectionSort(int arr[][8], int);
int row, col, possible;
int arr[13][8];

for (row=0;row<14;row++)
{
cout<<endl;
for (col=0;col<8;col++)
{
arr[row][col]=600+rand()%199;

if ((row==13)&&(col==3))
{
col=9;
}

}
}
selectionSort (arr, amount);
cout<<"Your sorted array of random numbers ranging from 600 to 799."<<endl;
for (row=0;row<13;row++)
{
cout<<endl;
for (col=0;col<8;col++)
{
cout<<arr[row][col]<<"  ";
if ((col+1) % 8 == 0)
cout << endl<<endl;
if ((row==12)&&(col==3))
{
col=9;
row=14;
}
}
}
cout<<endl;

cin.get();
return 0;
}

// something feels out of order here
//and it seems that I am flinging values around more than I should be

void selctionSort( int array [][8], int size)

//and no values were needed using the 1d version (for the first number)

{
int startScan, startScan2, minIndex, minIndex2, minValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex=startScan;
minValue = array[startScan][startScan2];
for (int index = startScan+1; index<size; index++)
{
for (int index2 = startScan2+1; index2<size; index2++)
{

if (array[index][index2] < minValue)
{
minValue = array [index][index2];
minIndex = index;
minIndex2 = index2;
}
}
}
array[minIndex][minIndex2] = array[startScan][startScan2];
array[startScan][startScan2] = minValue;
}
}```

2. Originally Posted by Techgique
Hi everyone,

So I have finally got my 2d array put together and now I need to sort the random numbers that are in the elements. I feel like I am close because I put together a one dimensional array and sorted it with no problems.

My problem is that I am a little unsure how to implement the knowledge I have about selection sort with one dimensional arrays on a two dimensional platform. I want them to sort left to right, top to bottom. So essentially, I need it to sort in the fashion that a nested for loop inserts numbers into the array elements.

I have the concept of using nested for loops, but I am having trouble wrapping my head around working it into the void function.

As usual and per the tradition of this forum, I did my best to get it, but I am getting errors and at this point it feels like I am jamming variables into spots that I am unsure if they even go there.

I would greatly appreciate any help on this issue.

Thank you, and here is my code so far. Techgique.

PS - If needed, Win 7, DevC, i7
Code:
```#include <iostream>
#include <cstdlib>
using namespace std;
// Random 2d array 100 numbers sorted by Steve P.
const int amount = 100;

int main()
{
void selectionSort(int arr[][8], int);
int row, col, possible;
int arr[13][8];

for (row=0;row<14;row++)//will go out of bound u have defined arr[13][8]
{
cout<<endl;
for (col=0;col<8;col++)
{
arr[row][col]=600+rand()%199;

if ((row==13)&&(col==3))
{
col=9;//what are u trying to do here
}

}
}
selectionSort (arr, amount);
cout<<"Your sorted array of random numbers ranging from 600 to 799."<<endl;
for (row=0;row<13;row++)
{
cout<<endl;
for (col=0;col<8;col++)
{
cout<<arr[row][col]<<"  ";
if ((col+1) % 8 == 0)
cout << endl<<endl;
if ((row==12)&&(col==3))
{
col=9;
row=14;
}
}
}
cout<<endl;

cin.get();
return 0;
}

// something feels out of order here
//and it seems that I am flinging values around more than I should be

void selctionSort( int array [][8], int size)//is this a typo

//and no values were needed using the 1d version (for the first number)

{
int startScan, startScan2, minIndex, minIndex2, minValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex=startScan;
minValue = array[startScan][startScan2];//startScan2 is not initalized
for (int index = startScan+1; index<size; index++)//starts from 1..do u need that to start from 1...then shouldnt it be index<=size..not sure
{
for (int index2 = startScan2+1; index2<size; index2++)
{

if (array[index][index2] < minValue)
{
minValue = array [index][index2];
minIndex = index;
minIndex2 = index2;
}
}
}
array[minIndex][minIndex2] = array[startScan][startScan2];
array[startScan][startScan2] = minValue;
}
}```
pleas check ur code....

3. feel like I am close because I put together a one dimensional array and sorted it with no problems.
Not only are you close, you're probably done.
Don't think 2d arrays are any different; because all the computer understands is 1d sequence of zeroes and ones.
So, just modify your loop control in the original program and access the required element by
*(array + loop_counter) where the loop_counter ranges from 0 to less than (Rows*Cols).

4. Hi guys,

thanks for the replies, I'm gonna hit the lab and check into this. I believe I pasted one of my earlier versions by accident. The for loops were fixed (the <14 thing) and the col=9 is something to purposely pull out of the loop so that only 100 numbers are displayed rather than 104. Also, for startScan2, I believe I initialized it 4 lines prior to your comment, but I'll double check it. Perhaps I did something wrong. Gonna put my nose back in this one and see if I can nail it. Thanks for the hints so far guys. TG