Bubble sort recursion

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);

}

@Samarth_Shah return is necessary otherwise all the statements following it will also get executed.
hope its clear if yes dont forget to hit like and mark resolved :smiley:

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.