why we can’t compare pivot element with itself while partition
Quick sort algorithm
hi @ritikbunty2511_729d0258992e0982 u can do jaisa code bhaiya ne likha hai i dont think there is any issue.
there is no issue in it. But question ye hai ke jab ham last element ko pivot element banate hai tab partition karne ka bad loop se bahar aake pivot element jo set karte hai loop mai nhi aaisa q
int partition (int a[], int start, int end) { int pivot = a[end]; int i = (start - 1); for (int j = start; j <= end - 1; j++) { if (a[j] < pivot) { i++; swap (arr[j],arr[i]); } } swap (arr[j],arr[i+1]); return (i + 1); } Ye har jagah aisa code hai quick sort ka Aur question ye hai ke return se pehle wale line ko comment kar deta hai aur jo j ka loop hai us hai j<=end. Ye q nhi karte.
Kyuki andar if condition me swap ho rha aur bahar Bina koi condition dekhe @ritikbunty2511_729d0258992e0982 bahar wali swap uncoditional swap hai
https://ide.codingblocks.com/s/680470 , See this code it is working absolutely fine. Now the question is if we can do it like that as well then why all websites as sir as well coded it like what i have commented.
Hi @ritikbunty2511_729d0258992e0982 to tumne if condition me = bhi laga diya na tb to lega kaam kyuki wo condition har baar true aayegi hi samjhe to wo equivalent to unconditional jump hi hai na
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.