The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
0 votes
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?

in Algorithms by Boss (41.4k points) | 13 views

1 Answer

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.

by Active (1.1k points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,092 questions
55,287 answers
86,113 users