![]() |
| | #1 |
| Registered User Join Date: May 2004
Posts: 2
| Sorting a 2-dimensional array 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. Last edited by kmoyle73; 05-05-2004 at 12:17 PM. Reason: First code blocks didn't work |
| kmoyle73 is offline | |
| | #2 |
| Registered User Join Date: Mar 2002
Posts: 1,595
| 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. Last edited by elad; 05-05-2004 at 01:14 PM. |
| elad is offline | |
| | #3 |
| Registered User Join Date: May 2004
Posts: 2
| Sounds like a plan. Thanks for your help! |
| kmoyle73 is offline | |
| | #4 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,710
| A recent example qsort with multi-dimensional arrays |
| Salem is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Array Sorting problem | ___________ | C++ Programming | 4 | 07-22-2008 12:17 AM |
| Dynamic array of structures containing yet another dynamic array of structures | innqubus | C Programming | 2 | 07-11-2008 07:39 AM |
| two dimensional array | leisiminger | C Programming | 12 | 03-09-2008 11:53 PM |
| two dimensional string array question | Hoser83 | C Programming | 8 | 02-07-2006 08:15 PM |
| Type and nontype parameters w/overloading | Mr_LJ | C++ Programming | 3 | 01-02-2004 01:01 AM |