I am creating quicksort to sort other city nearest to a specific city.
Here is my code:
private static void QuickSort(ArrayList<City> array, int First, int Last, City city ){
int Low,High,MidDistance;
Low = First;
High = Last;
Edge compareEdge = new Edge(array.get((First + Last)/ 2), city);
MidDistance = compareEdge.computeDistance();
do {
compareEdge = new Edge(array.get(Low),city);
while (compareEdge.computeDistance() < MidDistance){
Low += 1;
compareEdge = new Edge(array.get(Low),city);
}
compareEdge = new Edge(array.get(High),city);
while (compareEdge.computeDistance() > MidDistance){
High -= 1;
compareEdge = new Edge(array.get(High),city);
}
if (Low <= High) {
Swap(array,Low,High);
Low = Low + 1;
High = High - 1;
}
} while (Low <= High);
if (First < High){
QuickSort(array, First, High, city);
}
if (Low < Last){
QuickSort(array, Low, Last, city);
}
}
private static void Swap(ArrayList<City>array,int Low, int High){
City tempCtiy = array.get(Low);
array.get(Low).set(array.get(High));
array.get(High).set(tempCtiy);
}
This code is inside my MST class. so when I run it
Exception in thread “main” java.lang.IndexOutOfBoundsException: Index: 49, Size: 49
it happens here :
while (compareEdge.computeDistance() < MidDistance){
Low += 1;
compareEdge = new Edge(array.get(Low),city);
}
Help me to figure out what’s wrong.
It works well when I write in VB.net.
Add the termination condition in
whileonLowas below: