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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:03:32+00:00 2026-06-15T15:03:32+00:00

I am having trouble finding a bug in this sorting algorithm. When I pass

  • 0

I am having trouble finding a bug in this sorting algorithm. When I pass an array to the sort method, say 5,6,4,7,1, I get the same array as result. I’ve been going through code but I cannot find where it is going wrong. Disregard SortThread task, it’s an argument that will update the progress bar, but I am not using it right now.

class QuickSort extends Sort {

@Override
ArrayList<Integer> sort(ArrayList<Integer> array, SortThread task) {

    if (array.size() <= 1) {
        return array;
    }

    int middle = (int) Math.ceil((double) array.size() / 2);
    int pivot = array.get(middle);

    ArrayList<Integer> less = new ArrayList<>();
    ArrayList<Integer> greater = new ArrayList<>();

    for (int i = 0; i < array.size(); i++) {
        if (array.get(i) <= pivot) {
            if (i == middle) {
                continue;
            }
            less.add(array.get(i));
        } else {
            greater.add(array.get(i));
        }
    }

    return concatenate(sort(less, task), pivot, sort(greater, task));
}

private ArrayList<Integer> concatenate(ArrayList<Integer> less, int pivot, ArrayList<Integer> greater) {

    ArrayList<Integer> list = new ArrayList<>();

    for (int i = 0; i < less.size(); i++) {
        list.add(less.get(i));
    }

    list.add(pivot);

    for (int i = 0; i < greater.size(); i++) {
        list.add(greater.get(i));
    }

    return list;
}

}

  • 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-15T15:03:33+00:00Added an answer on June 15, 2026 at 3:03 pm

    The others have it right that you are probably printing the original as your code works fine.

    You could try this instead, which will sort it in-situ, thus having less unexpected behaviour and being slightly more efficient. All I have done is made concatenate take a new parameter which is where it should put its results.

    BTW: I would suggest you pull out the if (i == middle) { continue; } to before you compare, this would also be slightly more efficient.

    static class QuickSort {
    
      public static ArrayList<Integer> sort(ArrayList<Integer> array) {
    
        if (array.size() <= 1) {
          return array;
        }
    
        int middle = (int) Math.ceil((double) array.size() / 2);
        int pivot = array.get(middle);
    
        ArrayList<Integer> less = new ArrayList<>();
        ArrayList<Integer> greater = new ArrayList<>();
    
        for (int i = 0; i < array.size(); i++) {
          if (array.get(i) <= pivot) {
            if (i == middle) {
              continue;
            }
            less.add(array.get(i));
          } else {
            greater.add(array.get(i));
          }
        }
    
        return concatenate(sort(less), pivot, sort(greater), array);
      }
    
      private static ArrayList<Integer> concatenate(ArrayList<Integer> less, int pivot, ArrayList<Integer> greater, ArrayList<Integer> list) {
    
        list.clear();
    
        for (int i = 0; i < less.size(); i++) {
          list.add(less.get(i));
        }
    
        list.add(pivot);
    
        for (int i = 0; i < greater.size(); i++) {
          list.add(greater.get(i));
        }
    
        return list;
      }
    } 
    

    you could further improve concatenate to:

        list.clear();
        list.addAll(less);
        list.add(pivot);
        list.addAll(greater);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble finding a list of the changes and bug fixes that have
I am having trouble finding an answer to this. Consider the clipping code below:
I'm having trouble finding a way to do this in a way that doesn't
I'm having trouble finding the right syntax to replace an array of objects nested
I'm having trouble finding out how to do this and the Visual Studio SDK
I'm having trouble finding an algorithm for my problem. I have a grid of
I am having trouble finding resources on this, it may be a keyword thing.
I'm having trouble finding a solution to this via google but I would have
I'm having trouble finding the right solution for this fully fluid layout. I want
Having trouble finding a solution for my situation here. Sorry if this has been

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.