- #1
evinda
Gold Member
MHB
- 3,836
- 0
Hi! (Nerd)
This is the algorithm of [m] Quicksort [/m], according to my notes:
Why is this command: [m] swap(A[q],A[r]); [/m] needed at the algorithm of [m]Quicksort[/m]?
Don't we swap these values in [m] Partition [/m] ? (Thinking)
This is the algorithm of [m] Quicksort [/m], according to my notes:
Code:
Quicksort(A,p,r)
if (p>=r) return;
q=Partition(A,p,r);
swap(A[q],A[r]);
Quicksort(A,p,q-1);
Quicksort(A,q+1,r)
Code:
Partition(A,p,r)
x=A[r];
i=p-1;
for (j=p; j<r; j++){
if (A[j]<=x){
i=i+1;
swap(A[i],A[j]);
}
}
swap(A[i+1],A[r]);
return i+1;
Why is this command: [m] swap(A[q],A[r]); [/m] needed at the algorithm of [m]Quicksort[/m]?
Don't we swap these values in [m] Partition [/m] ? (Thinking)