C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-05-2004, 11:59 AM   #1
Registered User
 
Join Date: May 2004
Posts: 2
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.

Last edited by kmoyle73; 05-05-2004 at 12:17 PM. Reason: First code blocks didn't work
kmoyle73 is offline   Reply With Quote
Old 05-05-2004, 12:17 PM   #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   Reply With Quote
Old 05-05-2004, 12:20 PM   #3
Registered User
 
Join Date: May 2004
Posts: 2
Sounds like a plan. Thanks for your help!
kmoyle73 is offline   Reply With Quote
Old 05-05-2004, 01:54 PM   #4
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,710
A recent example
qsort with multi-dimensional arrays
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 05:16 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22