Selection Sort - Why does it sort only by descending or ascending, but not by value?

This is a discussion on Selection Sort - Why does it sort only by descending or ascending, but not by value? within the C++ Programming forums, part of the General Programming Boards category; This program is designed to allocate dynamic memory using pointers and sort them ascending and descending. The problem is it ...

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    45

    Selection Sort - Why does it sort only by descending or ascending, but not by value?

    This program is designed to allocate dynamic memory using pointers and sort them ascending and descending. The problem is it will not sort the pointers by value. For example if I enter -1 2 3 -6 8. The ascending list will be -1 2 3 -6 8, and descending list will be 8 -6 3 2 -1. I would like it to be -6 -1 2 3 8 and vice-versa for the ascending and descending list respectively. I'm sure its something simple, but I just cannot seem to straighten the problem out.


    If you scroll down to where my print function is, that is where i pass in "pointerArray" which is a double pointer. I also have "dataArray" being passed in, but it is unused for now. I want to sort "pointerArray" seperately from "dataArray."

    Please help, and ask questions if I need to be more clear.

    Code:
    #include<iostream>
    using std::cin;
    using std::cout;
    using std::cerr;
    using std::endl;
    
    
    #include <new>
    using std::bad_alloc;
    
    
    #include<cstdlib>
    
    #define int_array 1
    #define pointer2int_array 2
    
    
    
    
    
    // Prototype declarations
    
    int getSize();
    
    void*  getArray( int &howMuch, int typeOf );
    
    void populateArray( int num_of, int* ptr );
    
    void printArray( int num_of, int** ptr, int*ptr2 );
    
    
    
    /////////////////////////// -- main -- ///////////////////////////////////////////////////////
    
    int  main()
    {
    
      int SIZE = getSize();
    
      int* dataArray =  ( int*) getArray(SIZE, int_array );
      int** pointerArray = ( int**)getArray(SIZE, pointer2int_array );
      
      for(int i=0; i<SIZE; i++)
      pointerArray[i] = &dataArray[i];
      
      populateArray(SIZE, dataArray);
      printArray(SIZE, pointerArray, dataArray);  //  sort and print array
    
    
      delete [] dataArray;
      delete [] pointerArray;
    
      cout << endl << endl;
    
      return ( 0 );
    
    }
    
    
    ////////////////////////////////  -- end main --  ////////////////////////////////////////////////
    
    
    
    
    
    /***************************************************************************************************
    *
    *   Function Name:        getSize
    *
    *   Purpose:            requests an integer from user
    *
    *   Input Parameters:        none
    *
    *   Output parameters:        none
    *
    *   Return Value:        retuns the user entered integer howMuch
    *
    ***************************************************************************************************/
    
    int getSize()
    {
      int howMuch;
      cout << "enter the size of the array :  ";
      cin >> howMuch;
      return howMuch;
    
    }
    
    
    
    /***************************************************************************************************
    *
    *   Function Name:        getArray
    *
    *   Purpose:            dynamically allocates either an array of ints or an array of
    *                int pointers
    *
    *   Input Parameters:        an int howMuch - the size of array to allocate
    *                an int typeOf indicating the type of array to allocate
    *
    *   Output parameters:        none
    *
    *   Return Value:        returns a pointer to an int array or an array of int pointers
    *
    ***************************************************************************************************/
    
    
    void*  getArray( int &howMuch, int typeOf )
    {
    
      try
        {
          switch ( typeOf )
            {
            case int_array:
            {
              int* p = new int [ howMuch ];
              return( p );
            }
    
            case pointer2int_array:
            {
              int** p = new int* [ howMuch ];
              return( p );
            }
            default:
            {
              cerr << " problem allocating array \n\n";
              exit( -666 );
            }
    
            }
        }
      catch ( bad_alloc &memoryAllocationException )
        {
          cerr << memoryAllocationException.what() << endl;
          exit( 666 );
        }
    
    
    }
    
    
    
    /***************************************************************************************************
    *
    *   Function Name     :       populateArray
    *
    *   Purpose           :       To give values to the array of ints or the array of int pointers, and
    *                        sort the array in ascending value.
    *   Input Parameters  :          an int num_of keeps track of the size of the array
    *                        an int* ptr to pass in the dataArray variable
    *
    *   Output parameters :          none
    *
    *   Return Value      :          none
    *
    ***************************************************************************************************/
    
    
    void populateArray( int num_of, int* ptr )
    {
        cout << "Enter the values for the array: ";
        for(int i=0; i<num_of; i++)
        cin >> ptr[i];
    }
    
    /***************************************************************************************************
    *
    *   Function Name     :            printArray
    *
    *   Purpose           :            sorts and prints out array of values in ascending, descending, and original order.
    *
    *
    *   Input Parameters  :            an int num_of that counts the number of values in the array
    *                        an int** ptr to pass in the pointer values
    *   Output parameters :            none
    *
    *   Return Value      :            none
    *
    ***************************************************************************************************/
    
    
    void printArray( int num_of, int** ptr, int* ptr2 )  \\  sort & print
    {
        for(int i=0; i<num_of; i++ )
        {
        int smallestIndex = i;
    
            for(int location = i; location <num_of; location++ )
                if( ptr[location] > ptr[smallestIndex])
                    smallestIndex = location;
            int *temp = ptr[smallestIndex];
            ptr[smallestIndex] = ptr[i];
            ptr[i] = temp;
        }
             cout << "\nDescending List: ";
             for ( int i = 0 ; i < num_of; i++ )  //  print out values
                 cout << *ptr[i] << " ";
    
    }

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,342
    Shouldn't the compare in printArray() be if( *(ptr[location]) > *(ptr[smallestIndex]) ) ... ?
    Last edited by rcgldr; 09-14-2013 at 02:10 PM.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    45
    Quote Originally Posted by rcgldr View Post
    Shouldn't the compare in printArray() be if( *(ptr[location]) > *(ptr[smallestIndex]) ) ... ?
    amazing, although i do not fully understand why.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    It's because you decided to sort an array of pointers, and an expression of that form actually compares the data.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,662
    Quote Originally Posted by thomann061 View Post
    This program is designed to allocate dynamic memory using pointers and sort them ascending and descending. The problem is it will not sort the pointers by value. For example if I enter -1 2 3 -6 8. The ascending list will be -1 2 3 -6 8, and descending list will be 8 -6 3 2 -1. I would like it to be -6 -1 2 3 8 and vice-versa for the ascending and descending list respectively. I'm sure its something simple, but I just cannot seem to straighten the problem out.
    You need to get your description straight.

    Sorting in ascending or descending order IS sorting by value, by default. So, in your example, a sort in ascending order will produce the sequence -6 -1 2 3 8 and a sort in descending order will produce the sequence 8 3 2 -1 -6.

    If your algorithm is producing the result 1 2 3 -6 8 then it is sorting in ascending order by magnitude. Similarly, if it is producing 8 -6 3 2 -1 it is sorting in descending order by magnitude.


    Other guys have pointed out problems in your code already.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    1,342
    Quote Originally Posted by rcgldr View Post
    Shouldn't the compare in printArray() be if( *(ptr[location]) > *(ptr[smallestIndex]) ) ... ?
    Quote Originally Posted by thomann061 View Post
    I do not fully understand why.
    ptr[location] is a pointer to an integer, *(ptr[location]) is the integer.

    The original if statement was comparing pointers, while the if statment I suggested changes this to compare the integers that ptr[] points to.
    Last edited by rcgldr; 09-14-2013 at 05:05 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to sort 3 numbers in descending order
    By Tigertan in forum C Programming
    Replies: 2
    Last Post: 10-31-2012, 08:49 PM
  2. Replies: 1
    Last Post: 12-04-2011, 11:31 PM
  3. Sort a list in Ascending Order
    By Coding in forum C++ Programming
    Replies: 15
    Last Post: 01-21-2008, 01:59 PM
  4. 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
  5. Can counting sort sort in descending order?
    By Nutshell in forum C Programming
    Replies: 3
    Last Post: 03-06-2003, 09:59 AM

Tags for this Thread


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