Dear team,
In bubble sort using recursion, why do i need “return” after the if (j ==n-1) in the bubble sort recursion function (if return is removed, it gives segmentation error):
void bubblesort_rec(int a[], int j, int n){
//base case for outer loop of bubble sort (total n-1 passes)
if(n==1){
return;
}
//base case for inner loop: j = n-1 for 1 pass to complete
if(j==n-1){
return bubblesort_rec(a, 0, n-1);
}
if(a[j]>a[j+1]){
swap(a[j], a[j+1]);
}
bubblesort_rec(a,j+1,n);
}