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 8129697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:24:11+00:00 2026-06-06T08:24:11+00:00

I just implemented QuickSort algorithm from book and got weird output. It works but

  • 0

I just implemented QuickSort algorithm from book and got weird output. It works but it sorts in descending order instead of ascending. For example: [1, 5, 2, 10, 6, 9, 8, 3, 7, 4]
is sorted [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] cant seem to find source in my code:

private void quicksort(int[] A, int p, int r) {
    if (p < r) {
        int q = partition(A, p, r);
        quicksort(A, p, q);
        quicksort(A, q + 1, r);
    }
}

private int partition(int[] A, int p, int r) {
    int x = A[p]; // pivot
    int i = p;
    int j = r;
    while (true) {

        while (A[i] > x) {
            i++;
        }

        while (A[j] < x) {
            j--;
        }
        if (i < j) {
            int temp = A[i];
            A[i] = A[j];
            A[j] = temp;
        } else {
            return j;
        }
    }
}

INITIAL CALL:

   quicksort(A, 0, A.length - 1);

how do i calculate the space complexity for the quicksort?

thank you guys

  • 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-06-06T08:24:13+00:00Added an answer on June 6, 2026 at 8:24 am

    It’s in your partition function, you are sorting in descending order.

     while(true) {
     //ignore all the numbers greater than X to left
     while (A[i] > x) {
            i++;
        }
     //ignore all numbers lesser than X to right
     while (A[j] < x) {
            j--;
     }
    
     //swap a number lesser than X on left with a number greater than X on right
        if (i < j) {
            int temp = A[i];
            A[i] = A[j];
            A[j] = temp;
            i++;
            j--;
        } else {
            //Now the array is so sorted, that all numbers lesser than X are on right of it and greater than X are to left of it. Hence return position of X
            return j;
        }
     }
    

    //for ascending:

     while(true) {
    
     while (A[i] < x) {
            i++;
     }
    
     while (A[j] > x) {
            j--;
     }
    
        if (i < j) {
            int temp = A[i];
            A[i] = A[j];
            A[j] = temp;
            i++;
            j--;
        } else {
            return j;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I just implemented a base controller on my MVC3 site in order to
Just wondering if there was a nice (already implemented/documented) algorithm to do the following
I just implemented a star rating system on my site, but I'm having a
I just implemented Clone from ICloneable and realized that the event subscriptions from my
I just implemented a best match file search algorithm to find the closest match
I just implemented an algorithm on the GPU that computes the difference btw consecutive
I just implemented the ODCIAggregate Interface to create a custom aggregation function. It works
I just implemented Poisson Disk generation in the plane with this simple algorithm: http://people.cs.ubc.ca/~rbridson/docs/bridson-siggraph07-poissondisk.pdf
I just implemented an image upload to MySQL via PHP. But i want to
I just implemented a orientation change feature - e.g. when the layout changes from

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.