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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:34:26+00:00 2026-06-15T09:34:26+00:00

I’m trying to implement several different types of quicksort in java, but none of

  • 0

I’m trying to implement several different types of quicksort in java, but none of my implementations seem to be working. I’ve looked all over the internet and my code looks very similar to all the code that i’ve found, so I have no idea whats wrong. My code is as follows: (Note that this isn’t complete but I figure if I find whats wrong with one quicksort, the other versions will work as well) Edit: The problem i’m having is that the array doesn’t sort correctly. I run a simple method called isSorted to tell me whether the array was sorted correctly. It works with other sorting methods (insertion sort, heap sort etc.) but reports false when used with my implementation of quicksort

public static void quickSort(int[] A) {
        flag=0;
        quickSortHelper(A, 0, A.length-1);
    }

public static void quickSortHelper(int [] A, int low, int high){
        if(low<high){
            if(flag==0){
            int q=DPartition(A, low,high,A[high]);
            quickSortHelper(A,low,q-1);
            quickSortHelper(A,q+1,high);
        }

public static int DPartition(int [] A, int low, int high,int pivot){
        int p=pivot;
        int i=low;
        int j=high-1;
        while (i<=j){
            while(i<=j && A[i]<=p){
                i=i+1;
            }
            while(j>=i && A[j]>=p){
                j=j-1;
            }
            if (i<j){
            int temp = A[i];
            A[i] = A[j];
            A[j] = temp;
            }

        }
      int temp = A[i];
      A[i] = A[p];
      A[p] = temp;
      return i;
    }
  • 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-15T09:34:27+00:00Added an answer on June 15, 2026 at 9:34 am

    The bug is in your DPartition method. In quick sort, you move in a particular direction, and as soon as swapping is performed, you change the direction you move. But, in the above code, you are moving in both the direction without swapping.

    The first inner-while loop finds the location of swap, but instead of swapping, you started with the next inner-while, which starts moving in opposite direction. That is faulty.

    You should maintain one more variable to store the direction in the array.

    You can try modifying your code like this (Not Tested): –

    // No need to pass `pivot` as parameter. Just use `high`.
    public static int DPartition(int [] A, int low, int high) {  
        int i=low;
        int j=high;
        boolean leftToRight = false;
        boolean rightToLeft = true;
    
        while (i <= j) {   // Iterate till middle
    
            if (leftToRight) {  // Move left to right
    
                while(i <= j && A[i] <= A[j]){
                    i=i+1;  // Move right until condition is satisfied
                }
                if (i < j) {   // If `i` has not moved beyond `j`. Perform Swap
                    swap(i, j, A);   // Pass index for swapping along with array.
                }
                leftToRight = false;   // Toggle to change direction.
                rightToLeft = true;
    
            } else if (rightToLeft) {  // Move right to left.
    
                while(j >= i && A[j] >= A[i]){
                    j=j-1;
                }
                if (j > i) {    // If j has not moved beyond `i`. Perform Swap
                    swap(i, j, A);
                }
                rightToLeft = false;   // Toggle to change the direction
                leftToRight = true;
            } 
        }
        return i;   // Return `index` to split.
    }
    
    public static void swap(int p, int q, int[] a) {
        System.out.println("p = " + p + ", q = " + q);
        int temp = a[p];
        a[p] = a[q];
        a[q] = temp;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.