【问题标题】:Sorting an array of string data using the insertion sort method使用插入排序方法对字符串数据数组进行排序
【发布时间】:2014-05-11 13:01:47
【问题描述】:

我在使用插入对字符串数组进行排序时遇到问题。

当我编译以下代码时:

public class Project1 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String names[]=new String[5];
        int size=names.length;
        System.out.println("Enter the 5 car manufacturers: ");
        //Load Array
        for (int i = 0; i < 5; i++) {
            names[i] = input.nextLine();            
        }

        //Print descending order list
        String[] descSort;
        descSort=bubbleSortDesc(names);
        System.out.println("Car manufacturers listed sorted in descending order (via BubbleSort): ");
        for (int x=0; x < names.length; x++) {
            System.out.println(names[x]);
        }

        //Print ascending order list
        insertionSortAsc(names, size);
        System.out.println("Car manufacturers listed sorted in ascending order (via InsertionSort): ");
        for (int z=0; z < names.length; z++) {
            System.out.println(names[z]);
        }
    }¨

    public static String[] bubbleSortDesc(String[] names) {
        String temp;
        int passNum, i, result;
        for (passNum=1; passNum <= 4; passNum++) {
            for (i = 0; i<=(4-passNum); i++) {
                result=names[i].compareToIgnoreCase(names[i+1]);
                if(result<0) {
                    temp=names[i];
                    names[i]=names[i+1];
                    names[i+1]=temp;
                }
            }
        }
        return names;
    }

    public static void insertionSortAsc(String[] names, int i) {
        String temp = names[i];
        int j = i-1;
        while (j >= 0 && names[j].compareToIgnoreCase(temp) > 0) {
            names[j+1]=names[j];
            j--;
        }
        names[j+1]=temp; 
    }

    public static void insertionSort(String[] names, int n) {
        for(int i = 1; i<n; i++) {
            insertionSortAsc(names, i);
        }
    }
}

它给了我错误:

cannot find symbol- method insert(java.lang.String[], int)

我怀疑这与我们被告知要使用我们的书作为代码参考的事实有关,但该书仅处理 int 类型的数据排序,并且没有对字符串数据进行排序的示例。

感谢任何帮助。

编辑:修复错误后,程序编译并执行,但在输入数据后它崩溃并给我以下错误

java.lang.ArrayIndexOutofBoundsException:
5

这个错误突出显示String temp = names[i]这一行

【问题讨论】:

    标签: java arrays sorting insertion-sort


    【解决方案1】:
    public static void insertionSort(int... arr) {
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] >= arr[i - 1])
                continue;
    
            int j = i - 1;
    
            for (; j >= 0; j--)
                if (arr[j] < arr[i])
                    break;
    
            int tmp = arr[i];
            System.arraycopy(arr, j + 1, arr, j + 2, i - j - 1);
            arr[j + 1] = tmp;
        }
    }
    

    【讨论】:

      【解决方案2】:

      这将按照您的意愿工作:

      public static void insertionSortAsc(String[] names, int n)
      {
          for(int i = 1; i<n; i++)
          {
              insert(names, i);
          }
      }
      
      public static void insert(String[] names, int i)
      {
          String temp = names[i];
          int j = i - 1;
      
          while (j >= 0 && names[j].compareToIgnoreCase(temp)  > 0)
          {
              names[j + 1]= names[j];
              j--;
          }
          names[j + 1] = temp;
      }
      

      【讨论】:

        【解决方案3】:

        您尚未定义名为 insert 的方法。

        【讨论】:

        • 从书上的方式来看,我的印象是这是一种预加载的方法,感谢您的输入。
        • 可能是,但您仍然需要将其添加到您的类路径或重新实现它,可能是后者。
        • 输入数据后对新问题有什么意见吗?
        • @user3385542 什么?我不明白你刚才说的。
        • 我做了一个解释新问题的编辑,但基本上在执行程序并输入 5 个字符串后,它崩溃并出现错误消息“java.lang.ArrayIndexOutofBoundsException:5”,我想知道是否您知道导致该错误消息的原因。
        最近更新 更多