The 2d selection sort

This is a discussion on The 2d selection sort within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, So I have finally got my 2d array put together and now I need to sort the random ...

  1. #1
    Registered User Techgique's Avatar
    Join Date
    Jun 2011
    Location
    San Diego
    Posts
    9

    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)
    
     //Not sure about this 8, but again, only comfortable with 1d arrays so far,
    //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. #2
    Registered User
    Join Date
    Jul 2011
    Location
    Bangalore,India
    Posts
    24
    Quote Originally Posted by Techgique View Post
    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
    
     //Not sure about this 8, but again, only comfortable with 1d arrays so far,
    //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. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,523
    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).
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    Registered User Techgique's Avatar
    Join Date
    Jun 2011
    Location
    San Diego
    Posts
    9
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Selection Sort help
    By Twigstar in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2005, 09:39 PM
  2. Selection Sort
    By hopeolicious in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2005, 08:42 AM
  3. merge sort and selection sort and time spent on both
    By misswaleleia in forum C Programming
    Replies: 3
    Last Post: 06-04-2003, 03:24 PM
  4. Selection Sort
    By volk in forum C++ Programming
    Replies: 3
    Last Post: 05-08-2003, 07:54 AM
  5. Selection-sort
    By Nutshell in forum C Programming
    Replies: 10
    Last Post: 01-11-2002, 04:32 AM

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