void bubblesort(int *arr, int n){
if(n==1){
return;
}
for(int i=0; i<n-1;i++){
if(arr[i]>arr[i+1]){
swap(arr[i], arr[i+1]);
}
bubblesort(arr,n-1); // if I put return bubblesort(), the function doesnt work
}
}
I do not understand when to use return for the recursive statement, because when we write another function for an appraoch without any loop we have to use the return statement:
void bubblesort2(int *arr, int n, int j){
if(n==1){
return;
}
if(j==n-1){
return bubblesort2(arr, n-1, 0);
}
if(arr[j]>arr[j+1]){
swap(arr[j], arr[j+1]);
}
return bubblesort2(arr, n, j+1); // here the return works and without it i get an unsorted array.
}