• retagged by
835 views
1 1 vote
Could anyone describe how the partitioning algorithm vary when the pivot is  varied ?

In Cormen , last element is taken as pivot . Suppose I took first element or middle element or 3 rd element as pivot then how the partitioning algorithm will change.

1 Answer

Best answer
1 1 vote

Normal  quik sort can be implemented by two ways

1) select first element as pivot element: Declare two variables i,j(assumed) initially i,j and pivot will be at same place...now in partition algorithm make a whole loop with condition J will be looking for element greater than pivot and if smaller element occur then move i one position ahead and swap elements at position i and j....and so on...

2)Fix i and pivot at last position an dj at first position and run the partion algo in the same manner as illustrated above.

Third type of quik sort algo is RANDOMIZED quik sort:

In randomized quiksort pivot is calculated randomly...or we can say that array is shuffled. At every instatnt we exchange A[p] with an element chosen at random from A[p…r]

RANDOMIZED-PARTITION(A, p, r)

  i ← RANDOM(p, r)

  exchange A[p] ↔ A[i]

  return PARTITION(A, p, r)

Pivot element is likely to be any of r-p+1 

RANDOMIZED-QUICKSORT(A, p, r)

  if p < r

  then q ← RANDOMIZED-PARTITION(A, p, r)

  RANDOMIZED-QUICKSORT(A, p, q)

  RANDOMIZED-QUICKSORT(A, q + 1, r)

This is how randomized quik sort partition algorithm work.

• selected by
Position:
Show:

Related questions

0 0 votes
0 0 answers
1.5k
1.5k views
0 0 votes
0 0 answers
480
480 views
Gurdeep Saini asked Nov 13, 2018
480 views
between hoare and loranto quicksort which give better cache performance ?we know that in hoare quicksort we move the pointer i,j in different direction but in loranto qui...
0 0 votes
1 1 answer
966
966 views
Vaishnavi01 asked Sep 21, 2018
966 views
True or False :In randomized quicksort , each key is involved in the same number of comparisons.
3 3 votes
1 1 answer
1.8k
1.8k views
manish suthar asked Dec 7, 2017
1,770 views
3-way partitioning is a modification of quicksort that partitioned the elements into groups smaller than,equal to and larger than pivot. Only the group of smaller and lar...