recursive selection sort function

This is a discussion on recursive selection sort function within the C Programming forums, part of the General Programming Boards category; I'm having a problem with a function that uses the selection-sorting algorithm using recursion rather than a loop. I start ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    5

    Question recursive selection sort function

    I'm having a problem with a function that uses the selection-sorting algorithm using recursion rather than a loop. I start out with an integer array with 30 elements:

    199 173 175 28 175 102 114 129 199 125
    138 95 163 121 45 156 16 70 147 16
    110 27 127 78 97 130 3 35 152 19


    and when I run the function using this:
    selection_sortup(&array[0], 30);

    I end up with this:

    3 3 16 19 27 28 35 45 70 78
    95 97 102 110 114 121 125 127 129 130
    137 137 138 147 152 156 163 173 175 175


    Here's the function:

    Code:
    void selection_sortup(int *p, int n)
    {
    
       int i, largest = 0, temp, temp_index;
       if (n == 0)
          return;
    	/* finds the largest value in array - saves it and its index */
       for(i = 0; i < n; i++){
    	  if (largest < *(p+i)){
    		largest = *(p+i);
    		temp_index = i;
    	  }
       }
    
    		/* copies the last element of the array into temp variable */
       temp = *(p+n);
    		/* switches the two */
       *(p+temp_index) = temp;
       *(p+n) = largest;
    		/* recursive call - predecrements n */
       selection_sortup(p, --n);
    }
    Any help would be greatly appreciated.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Help with what? That array seems sorted to me...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006

    Re: recursive selection sort function

    Code:
       /* copies the last element of the array into temp variable */
       temp = *(p+n);
       /* switches the two */
       *(p+temp_index) = temp;
       *(p+n) = largest;
    The last element in the array is *(p+n-1).

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    5

    Re: Re: recursive selection sort function

    Originally posted by Dave_Sinkula
    The last element in the array is *(p+n-1).
    Yay, it now works correctly! Thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help designing a recursive function.
    By broli86 in forum C Programming
    Replies: 3
    Last Post: 07-24-2008, 01:45 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

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