Why error pls help me
if you are taking j<=e
inside loop then that means you are executing at j=e also which make swap(arr[i],arr[j]) and increases “i” that mean you not need to return i+1; then return i;
but if you are taking j<e
inside the loop then you are not executing at j=e then return i+1;
Modified Function :
int partition(int *arr,int s,int e){
int i=s-1;
for(int j=s;j<=e;j++){
if(arr[j]<=arr[e]){
i++;
swap(arr[i],arr[j]);
}
}
swap(arr[i+1],arr[e]);
return i;
}
1 Like
thnx for detailed explanation.
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.