The Gateway to Computer Science Excellence

0 votes

QUICKSORT(A,p,r) 1 if p < r 2 q = PARTITION(A,p,r) 3 QUICKSORT(A, p , q-1) 4 QUICKSORT(A, q + 1, r)

How would you modify QUICKSORT to sort into nonincreasing order?

0 votes

If you observe * PARTITION *of the array algorithm carefully then you'll observe at line 4 i.e. in

$if A[j]\leq x$

simply the change it to $\leqslant$ which is

$if A[j]\geqslant x$

this will make all the elements greater than pivot move to the left of the array and rest to the right which is the desired result.

52,215 questions

60,022 answers

201,249 comments

94,710 users