I need to make a vector sort code using recursion. I looked into sorting methods and found two interesting ones: selection sort and quicksort. However, I tried to use selection sort and I did not succeed. Here is my code:
#include <stdio.h>
#include <stdlib.h>
int numeros[5];
int i;
void Ordenar(int numeros[], int i, int j){
int aux;
int menor;
if(i==3){
for(i=0; i<5; i++){
printf("%d", numeros[i]);
}
return 0;
}
if(j==4){
return Ordenar(numeros, i+1, i+2);
}
if(numeros[i]>numeros[j]){
menor = j;
aux = numeros[j];
numeros[menor] = numeros[i];
numeros[i] = aux;
}
else{
return Ordenar(numeros, i, j+1);
}
}
int main(){
for(i=0; i<5; i++){
scanf("%d", &numeros[i]);
}
Ordenar(numeros, 0, 1);
return 0;
}
The idea was to make the first number stop and compare it with all the others until you find the smallest number. Then do the same with the second, third etc.
Another question: what is better to use to sort a vector with recursion: selection sort or quicksort?