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;
}
}