Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 830293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:01:17+00:00 2026-05-15T04:01:17+00:00

I wrote a quicksort algorithm however, I would like to make a change somewhere

  • 0

I wrote a quicksort algorithm however, I would like to make a change somewhere so that this quicksort would output elements in descending order.

I searched and found that I can change the comparison operator (<) in partition() to other way around (like below).

 //This is snippet from partition() function    
        while($array[$l] < $pivot) { $l++; }
        while($array[$r] > $pivot) { $r--; }

But it is not working..

If I quicksort the array below,
$array = (3,9,5,7);

should be:

$array = (9,7,5,3)

But actual output is:

$array = (3,5,7,9)

Below is my quicksort which trying to output elements in descending order.
How should I make change to sort in descending order? If you need any clarification please let me know. Thanks!

$array = array(3,9,5,7);
$app = new QuicksortDescending();
$app->quicksortDesc($array, 0, count($array));
print_r($array);


class QuicksortDescending {

public function partitionDesc(&$array, $left, $right) {
        $l = $left;
        $r = $right;
        $pivot = $array[($right+$left)/2];

        while($l <= $r) {
            while($array[$l] > $pivot) { $l++; }
            while($array[$r] < $pivot) { $r--; }
            if($l <= $r) {// if L and R haven't cross
                $this->swap($array, $l, $r);
                $l ++;
                $j --;
            }
        }
        return $l;
    }


public function quicksortDesc(&$array, $left, $right) {
        $index = $this->partitionDesc($array, $left, $right);
        if($left < $index-1) { //if there is more than 1 element to sort on right subarray
            $this->quicksortDesc($array, $left, $index-1);
        }
        if($index < $right) { //if there is more than 1 element to sort on right subarray
            $this->quicksortDesc($array, $index, $right);
        }
    }

public function swap(&$array, $index1, $index2) {
        $tmp = $array[$index1];
        $array[$index1] = $array[$index2];
        $array[$index2] = $tmp;

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T04:01:18+00:00Added an answer on May 15, 2026 at 4:01 am

    Just swap the comparison operators < with > and vice versa when comparing two elements:

    while($array[$l] < $pivot) { $l++; }
    while($array[$r] > $pivot) { $r--; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a trajectory planner in Matlab, and I would like to compile as
I wrote a php code that display error in red color. But somehow, this
i wrote i quicksort algorithm in c# but it has a problem,when i compile
i wrote i quicksort algorithm in c# but it has a problem,when i compile
While checking LINQ's abilities I've wrote simple QuickSort implementation and was glad that ultimately
I wrote this quicksort function: (defun quicksort (lst) (if (null lst) nil (let ((div
I wrote a PHP script that retrieves values from a MySQL Query. I used
I wrote a Flickr search engine that makes a call to either a public
I tried to write generic function that remove the duplicate elements from array. public
I wrote this code to do the IEEE 754 floating point arithmetic on a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.