In this program, I am sorting Olympic medals using mergeSort.
Something seems a little off with my code since sometimes it will give me an java.lang.ArrayIndexOutOfBoundsException and sometimes, it won't.
For a little background explanation:
I have a method that randomly generates Olympic countries and their medals won in a scoreboard. Returns a String[] array of results in the form of:
CAN 1 1 1
USA 1 1 2
GBR 0 0 1
CHN 0 0 2
However the scoreboard needs to be organized y descending medals of gold, silver and bronze. So it would have to be like:
USA 1 1 2
CAN 1 1 1
CHN 0 0 2
GBR 0 0 1
Using bubblesort and quicksort to sort the board works fine, but mergesort does not. At times it will be fine, but more often of the time it gives me ArrayIndexOutOfBoundsException.
public static void main(String[] args) {
Olympic_Results score = new Olympic_Results();
//print a return value of an array
String[] countries = score.OlympicResult(7); //input how many game results
mergeSort(countries, 0, countries.length - 1);
for (String value:countries)
System.out.println(value);
}
public static void mergeSort(String array[], int lo, int n) {
int low = lo;
int high = n;
if (low >= high) {
return;
}
int middle = (low + high) / 2;
mergeSort(array, low, middle);
mergeSort(array, middle + 1, high);
int end_low = middle;
int start_high = middle + 1;
while ((lo <= end_low) && (start_high <= high)) {
if ((array[low].substring(4,8)).compareTo(array[high].substring(4,8)) > 0) {
low++;
}
else {
String Temp = array[start_high];
for (int k = start_high - 1; k >= low; k--) {
array[k + 1] = array[k];
}
array[low] = Temp;
low++;
end_low++;
start_high++;
}
}
}
Any idea why this code isn't working properly? Thank you!
I think you go down too deep in dividing the array into parts:
if (low >= high) {
return;
}
Try to start fixing it with stopping here when length is 1.
if (high - low <=1) {
return;
}
BTW, if the length is 2 then you can compare the values in place and return them already sorted immediately.
UPD
You seem to add too many variables with similar names and lost yourself in them :)..
This does not look correct :
while (( lo <= end_low) && (start_high <= high)) {
Two major errors:
You keep two variables with similar names, but different purpose. As a result the while
loop test the lo
value, despite never changing it (the loop works with low
variable).
The if
inside while
should compare the initial items of two sorted series to choose one of them. Your series start at low
and start_high
, so you should compare array[low]
to array[start_high]
, but you compare it to array[high]
instead. If array[high]
data happens to be the smallest in the subinterval being sorted, then the low
value will get incremented past the array size (and while
condition doesn't catch it, as it tests lo
instead).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.