Next:9.6.3
Quicksort: Average Case AnalysisUp:9.6
Quick SortPrevious:9.6.1
Algorithm:
9.6.2 Algorithm for Partitioning
int partition (int i; int j; key-type pivot);
/* partitions the elements a[k]
... a[j],
wrt the pivot and returns
the position k */
{
int,
r;
{
= i; /*
starts from left end */
r
= j; /*r starts from right end */
do
swap the records a[]
and a[r];
while (a[]
. key < pivot)
=
+ 1;
while (a[r] . key
pivot)
r = r - 1;
while(r);
return ();
}
}
-
For an example, see Figure 9.3.
-
Worst case arises when the input is already sorted: O(n2)
-
Average case : O(nlog n)
Figure 9.3: Quicksort applied to a list of 10 elements
|
eEL,CSA_Dept,IISc,Bangalore