# Thread: Recursive Array Sort

1. ## 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. 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. 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.

4. Thanks!

Popular pages Recent additions