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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:39:31+00:00 2026-06-08T15:39:31+00:00

I seem to be having some problems witn my quicksort method. I am trying

  • 0

I seem to be having some problems witn my quicksort method. I am trying to sort an ArrayList of objects using my quicksort method. I am using the Apache POI library to extract data from an excel file and I am adding this data to my arraylist. I have confirmed that my arraylist is not empty by printing out the arraylist before applying the quicksort method.

My problem seems to be that after passing in my arraylist of objects it gets reset to null and the size of the arraylist becomes 0. I got a java IndexOutOfBounds Exception within my quicksort method while trying get an object from the arraylist. Any help would be appreciated, thanks !

Here is my main class :

EDIT I solved my problem using the given solution below, but I still don’t understand why my quicksort function doesn’t work. I would appreciate it if someone could look at the quicksort function and tell me where I am going wrong. Thanks !

public class Test {


private static ArrayList<Object> incom = new ArrayList<Object>();


private static int period;
private static String termination = "yes";
private static int pivotVal;

private static ArrayList<String> treatment_name = new ArrayList();
private static ArrayList<Integer> treatment_cstart = new ArrayList();
private static ArrayList<Integer> treatment_cend = new ArrayList();
private static ArrayList<Integer> treatment_cost = new ArrayList();
private static ArrayList<Integer> bridge_part = new ArrayList();
private static ArrayList<Integer> budget = new ArrayList();

private static Scanner input = new Scanner(System.in);
private static Scanner alt = new Scanner(System.in).useDelimiter("\n");

public static void main(String[] args) 
{   

    processFile();


}

public static void processFile(){
    try {
        POIFSFileSystem fs      =
            new POIFSFileSystem(new FileInputStream("Book2.xls"));
        HSSFWorkbook wb = new HSSFWorkbook(fs);

        HSSFSheet sheet =wb.getSheet("Table0");
        RowProcessor ip = IncomeProcessor.getInstance();
        Object [] incomes = ip.process(sheet);

        for (int i=0; i<incomes.length; i++)
          incom.add(incomes[i]);

        for (int i=0; i<incom.size(); i++)
        {
            Income income = (Income)incom.get(i);
            System.out.println(income.getBridgeID() + " " + income.getDeckState());
        }


          incom = quicksort(incom);

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}



public static ArrayList<Object> quicksort(ArrayList<Object> income){

    int pivot = income.size()/2;
    int samePivotVal = 0;
    ArrayList<Object> greater = new ArrayList<Object>();
    ArrayList<Object> lesser = new ArrayList<Object>();

    Income pivotIncome = (Income) income.get(pivot);
    pivotVal = pivotIncome.getDeckState();

    Income in;
    for(int i=0; i<income.size() ;i++){
        in = (Income)income.get(i);
        if(in.getDeckState() > pivotVal)
            greater.add(in);
        else if(in.getDeckState() < pivotVal)
            lesser.add(in);
        else 
            samePivotVal++;
    }


    lesser = quicksort(lesser);
    for(int i=0; i<samePivotVal; i++)
        lesser.add(pivotIncome);

    greater = quicksort(greater);

    ArrayList<Object> sorted = new ArrayList<Object>();

    for(Object result : lesser)
        sorted.add(result);

    for(Object result : greater)
        sorted.add(result);

    return sorted;
    }



}
  • 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-08T15:39:35+00:00Added an answer on June 8, 2026 at 3:39 pm

    It will be a lot easier (and better) if you use Java’s builtin sorting methods.

    public static void processFile(){ 
        //...
    
        Collections.sort(incom, new IncomComparator());
        //...
    }
    
    
    class IncomComparator implements Comparator<Object> {
        @Override
        public int compare(Object o1, Object o2) {
            /* compare logic goes here
               return a negative number when o1 < o2
                      a positive number when o1 > o2
                      0 when o1 == o2
            */ 
            return 0;
        }
    
    }
    

    or in a more simple way

    public static void processFile(){ 
        //...
    
        Collections.sort(incom, new Comparator<Object>() {
            @Override
            public int compare(Object o1, Object o2) {
               /* compare logic goes here
                   return a negative number when o1 < o2
                      a positive number when o1 > o2
                      0 when o1 == o2
               */ 
               return 0;
            }
        });
        //...
    }
    

    You can see some more examples here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using appengine and seem to be having some problems with url routing
I seem to be having some problems creating a jquery plugin. I am testing
i am having some problems with getting my form to submit. It doesnt seem
I'm trying out java and I seem to be running into some problems. The
i seem to be having some problems with the Vector class in actionscript 3
I seem to be having some problems with something simple here. I have some
I'm quite new to Oracle and seem to having some problems with my query
I seem to be having problems doing some basic parsing of an XML document
I'm using the LUA middleclass library now after some problems and I have a
I am having some problems with the away3D library. I want to apply 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.