Sorting a 2-dimensional array

This is a discussion on Sorting a 2-dimensional array within the C++ Programming forums, part of the General Programming Boards category; I have a small problem. I have been given an assignment to sort a 2-d array using a selectoin sort. ...

  1. #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

  2. #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.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    2
    Sounds like a plan. Thanks for your help!

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array Sorting problem
    By ___________ in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 12:17 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. two dimensional array
    By leisiminger in forum C Programming
    Replies: 12
    Last Post: 03-09-2008, 11:53 PM
  4. two dimensional string array question
    By Hoser83 in forum C Programming
    Replies: 8
    Last Post: 02-07-2006, 07:15 PM
  5. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM

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