Thread: Making a Selection Sort into a Recursive Function with a Vector

  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,318
    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, 04: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-04-2006, 12:52 AM
  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, 04:12 PM