Sunday, August 13, 2006

algorithm for quicksort

subroutine's psedocode:
procedure partition(A,p,r) //p,r are upper and bottom of the subarray;
x=A[r];
for( i = j = p; j < r; j++) {
__if(A[j] <= x) {
____tmp=A[j];
____A[j]=A[i];
____A[i]=tmp;
____i++;
__}
}
tmp=A[i];
A[i]=A[r];
A[r]=tmp;
return i;
end procedure;

procedure quicksort(A,p,r)
if(p < r) {
__q=partition(A,p,r);
__quicksort(A,p,q-1);
__quicksort(A,q+1,r);
}
end procedure;
that's all;

0 Comments:

Post a Comment

<< Home