简体   繁体   English

Java - 使用字符串合并排序

[英]Java - Merge sort with Strings

In this program, I am sorting Olympic medals using mergeSort. 在这个程序中,我正在使用mergeSort对奥运奖牌进行排序。

Something seems a little off with my code since sometimes it will give me an java.lang.ArrayIndexOutOfBoundsException and sometimes, it won't. 我的代码似乎有点不对劲,因为有时它会给我一个java.lang.ArrayIndexOutOfBoundsException,有时,它不会。


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: 以下列形式返回结果的String []数组:

CAN 1 1 1 CAN 1 1 1

USA 1 1 2 美国1 1 2

GBR 0 0 1 GBR 0 0 1

CHN 0 0 2 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 美国1 1 2

CAN 1 1 1 CAN 1 1 1

CHN 0 0 2 CHN 0 0 2

GBR 0 0 1 GBR 0 0 1

Using bubblesort and quicksort to sort the board works fine, but mergesort does not. 使用bubblesort和quicksort对电路板进行排序工作正常,但mergesort没有。 At times it will be fine, but more often of the time it gives me ArrayIndexOutOfBoundsException. 有时它会很好,但更常见的是它给我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. 当长度为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. 顺便说一句,如果长度是2,那么你可以比较适当的值并返回它们已经立即排序。

UPD 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)) { while(( lo <= end_low)&&(start_high <= high)){

Two major errors: 两个主要错误:

  1. 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). 因此, while循环测试lo值,尽管从未改变它(循环使用low变量)。

  2. The if inside while should compare the initial items of two sorted series to choose one of them. if里面while应该比较两个分类系列最初的项目选择其中之一。 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. 你的系列从lowstart_high开始,所以你应该将array[low]array[start_high]进行比较,但你array[start_high]它与array[high]进行比较。 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). 如果array[high]数据恰好是最小的子区间进行排序,则low的值会得到增加超过阵列的大小(和while条件不抓住它,因为它测试lo代替)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM