Making a Selection Sort into a Recursive Function with a Vector

This is a discussion on Making a Selection Sort into a Recursive Function with a Vector within the C++ Programming forums, part of the General Programming Boards category; Just need to convert my working selection sort function into a recursive one. I don't understand recursive functions very well ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    1

    Making a Selection Sort into a Recursive Function with a Vector

    Just need to convert my working selection sort function into a recursive one. I don't understand recursive functions very well so any explanation or assistance is much appreciated.
    Code:

    Code:
    sort(vector<int> unsorted_list)
    {
        int temp, min;
    
        for( int x = 0; x < unsorted_list.size()-1; x++)
        {
            min = x;
            for (int j = min+1; j < unsorted_list.size(); j++)
            {
                if (unsorted_list[j] < unsorted_list[min])
                min = j;
            }
                temp = unsorted_list[x];
                unsorted_list[x] = unsorted_list[min];
                unsorted_list[min] = temp;
         }
        return unsorted_list;
    }
    Thanks.

    PS: Declaration needs to remain the same. I realize that only one input is obscure but that is required -.-
    Last edited by bwright; 05-10-2011 at 03:38 PM.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    It cant be done with that as the declaration.
    First off it's missing the type and thus cannot compile, secondly there is no way for the sorted data to leave the function.
    Lastly, passing a vector by value is a bad idea.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recursive selection sort..
    By transgalactic2 in forum C Programming
    Replies: 35
    Last Post: 01-13-2009, 03:46 PM
  2. Selection sort on a vector
    By Neo1 in forum C++ Programming
    Replies: 2
    Last Post: 09-22-2008, 05:36 AM
  3. merge sort: recursive is fasfter than non-recursive
    By rio_cat in forum C Programming
    Replies: 8
    Last Post: 12-03-2006, 11:52 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, 02:24 PM
  5. recursive selection sort function
    By Cornelius in forum C Programming
    Replies: 3
    Last Post: 11-08-2002, 03:12 PM

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