# Sorting a 2-dimensional array

Printable View

• 05-05-2004
kmoyle73
Sorting a 2-dimensional array
I have a small problem. I have been given an assignment to sort a 2-d array using a selectoin sort. The array must firs be sorted on the 0 index, then on the 1 index WITHIN the 0 index (like sorting on year then on months within the year). I have solved the first phase as follows:

Code:

#include <iostream>
#include <cstdlib>

using namespace std;

short employee[16][3]={{2,5,1050},
{5,17,1200},
{3,12,1225},
{2,23,1300},
{3,6,1140},
{3,3,1110},
{2,4,1420},
{2,1,1360},
{5,14,1225},
{5,11,1380},
{5,10,1410},
{2,2,1565},
{3,8,1445},
{5,7,1387},
{3,9,1128},
{2,16,1448}};

void sort_em(short[][3],short);
void showArray();

int main()

showArray();
sort_em(employee,16);
cout<<"\n\nNow it is sorted\n";
showArray();
system("PAUSE");
return 0;
}

void sort_em(short array[][3],short elems)
{
short start,minIndex,minValue,tempValue,otherTemp;

for(start=0;start<(elems-1);start++)
{
minIndex=start;
minValue=array[start][0];
tempValue=array[start][1];
otherTemp=array[start][2];

for(short index=start+1;index<elems;index++)
{
if(array[index][0]<minValue)
{
minValue=array[index][0];
tempValue=array[index][1];
otherTemp=array[index][2];
minIndex=index;
}
}
array[minIndex][0]=array[start][0];
array[minIndex][1]=array[start][1];
array[minIndex][2]=array[start][2];
array[start][0]=minValue;
array[start][1]=tempValue;
array[start][2]=otherTemp;
}

}

void showArray()
{
for(short r=0;r<16;r++)
{
for(short c=0;c<3;c++)
{
cout<<employee[r][c]<<" ";
}
cout<<"\n\n";
}
}

My problem is the next pass where I sort the 1 index within the 0 index. I am really confused here.
• 05-05-2004
elad
Pass each element from the unsorted array to the sort function one at as time, using a tracking variable to keep track of how many elements are in the sorted array already. If the sorted array is empty add the passed element to the first index of the sorted array. If the sorted array isn't empty, loop through the sorted array until from the start until you have found the correct spot for insertion or you have looked at all elements in the sorted array. If the first number in the passed element is smaller than the first number in the current element of the sorted array, then insert it into the array to the left of the current element. Else if the first number of the passed element is the same as the first element of the current element of the sorted array then compare the second numbers. If the second number of the passed element is smaller than the second number of the current element of the sorted array, then insert it into the sorted array to the left of the current element. Else compare the passed element to the next element of the sorted array. If you reach the end of the sorted array without inserting the passed element into the sorted array already, then add the passed element to the end of the sorted array.

To insert the element to the left of a given element in the sorted array, shift all elements of the sorted array with indexes higher than or equal to the index of the current element to the right by one, starting with the element with the highest index. Then assign the passed element to the current element of the sorted array.
• 05-05-2004
kmoyle73
Sounds like a plan. Thanks for your help!
• 05-05-2004
Salem