Bubble Sort Recursion Doubt

In CRX-Recursion (Bubble Sort), we are sorting the array from index si to li , then , the base condition should be if(li==si ) and not if(li==0) because li may not go to 0 every time if we start the sorting of array other than the index 0.

Secondly, under the second if condition, bubblesort should be recursively called as bubblesort(arr,si,li-1) and not as bubblesort(arr,0,li-1) due to the above mentioned same reason.

Please explain me.

Hi @yashomar95,
See the concept of bubble sort is to put the largest element at last in first pass through the array … hence after si==li this will indicate that the array has the largest element at the last index … hence we need to recursively solve the array excluding the last index hence solving the remaining li-1 size array . But if li==0 that means all element are in sorted order hence it is our base case … i hope this helps.

When si !=0 , then in that case, li will not go till 0,instead it stops when li becomes equal to si.

then, in the video, we have assumed that li will go to 0 that is why we are returning when li becomes 0 and we are recursively calling bubblesort with si as 0.

@yashomar95,
li indicates the index where the element reaches it destined place or its correct place. If li =0, then we can return because the 0th index element is the first element and will always be at its correct place

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.