Thread: Recursive Array Sort

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    155

    Recursive Array Sort

    How would I accomplish this? You don't need to help me code it unless you want to be generous, but I just would like to know the psuedocode because I am quite confused:

    Write a recursive function to sort an array of integers into ascending order using the following idea: Place the smallest element in the first position, then sort the rest of the array by a recursive call. This is ar ecursive version of the selection sort. Note that merely doing a selection sort will not suffice here, since the function to do the sorting must itself be recursive and not merely use a recusive function.

  2. #2
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    psuedocode::::

    Code:
    void swap(int array[], int index1, int index2){
        int temp = array[index1];
        array[index1] = array[index2];
        array[index2] = temp;
    
    }
    
    void sort(const int array[],int startpoint, int arraylength) {
    
        if(startpoint == arraylength - 1) return;
    
        int minindex = startpoint;
    
        //find smallest in rest of array
        for(int i = startpoint ; i < arraylength ; i++) if(array[i] < array[minindex]) minindex = i;
    
         //put smallest in first spot
         swap(array, startpoint, minindex);
            
    
    sort(array,startpoint+1,arraylength);
    
    }

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Thought about it yourself?
    What code did you come up with?
    The key to this is that the find the smallest value in the array passed and place it in the first location of the array.Then call the function again but each time pass in a 1 element less array. You will need to pass arraysize and arraystartpos.The array is sorted when you have arraysize 1.Theres the info now you write the code.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help to sort array of structs
    By sass208 in forum C Programming
    Replies: 1
    Last Post: 11-26-2006, 12:11 AM
  2. using qsort to sort an array of strings
    By bobthebullet990 in forum C Programming
    Replies: 6
    Last Post: 11-25-2005, 08:31 AM
  3. how to sort out an array?
    By Chobo_C++ in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2004, 05:33 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM