【问题标题】:Java insertion sort method for strings not working字符串的Java插入排序方法不起作用
【发布时间】:2021-11-17 03:40:23
【问题描述】:

对于我的一部分作业,我应该创建一个插入排序方法,该方法从一个数组中获取字符串并将它们排序到一个忽略重复项的新空数组中。我的代码在使用非常小的字符串测试数组时有些工作,但只要有两个单词具有相同的首字母,它就不能正常工作。它首先自动将第一个字符串从原始数组移动到空数组的第一个位置,然后通过与之比较对剩余值进行排序。我知道我在某个地方需要另一个循环,但我很难将它拼凑起来

public class Main {
    public static void main(String[] args) {
        String[] words = {"big", "ALL", "zebra", "END", "great", "FOX", "quit", "episode", "zebra", "big", "all", "1"};
        System.out.println("Unsorted array: " + Arrays.toString(words));
        stringInsertionSort(words);
}

public static void stringInsertionSort(String array[]) {
    String[] sortedArray = new String[array.length];
    sortedArray[0] = array[0];

    int sortedElements = 1;
    for (int i = 1; i < array.length; i++) {
        if (Arrays.asList(sortedArray).contains(array[i])) {
            break;
        } else {

            int current = i;
            int k = i - 1;
                if ((array[i].compareTo(sortedArray[k]) < 0)) {
                    sortedArray[k + 1] = sortedArray[k];
                    sortedArray[k] = array[i];
                    
                    
                } else {
                    sortedArray[k + 1] = array[i];
           
                }
        }
    }
    System.out.println("Sorted array:   " + Arrays.toString(sortedArray));
}

}

【问题讨论】:

  • 我还不确定代码,但你不应该继续而不是中断吗?
  • 我设置了中断来阻止任何重复的被排序
  • 您想在找到重复项时停止排序,还是只想跳过重复项?我认为 break 会退出 for 循环并在找到重复项时停止排序。
  • 啊不,我不希望它在找到重复项时停止;我以为休息后它只会运行 else{ } 部分,然后继续重复 for 循环

标签: java sorting insertion


【解决方案1】:

我不能 100% 确定这是否是您正在寻找的以及该解决方案是否正确,但我希望它能让您了解您应该做什么 - 我试图解释一下。

private void stringInsertionSort(String[] array) {
        if (array.length == 0) {
            throw new IllegalArgumentException("The array is empty!.");
        }

        String[] sortedArray = new String[array.length];
        sortedArray[0] = array[0];

        int wall = 1;// indicates the wall - before the wall all elements are unique and sorted.

        for (int i = 1; i < array.length; i++) {
            sortedArray[wall] = array[i]; // for the moment put the element inside the sorted array as the last element.
            int rightPosition = wall; // indicates the right position in the sorted array of the current element at hand. If negative, it's a duplicate
            for (int j = wall; j > 0; j--) {
                if (sortedArray[j - 1].compareTo(sortedArray[wall]) == 0) { // duplicate
                    rightPosition = -1;
                    break; // exit from the inner loop
                } else if (sortedArray[j - 1].compareTo(sortedArray[wall]) < 0) {
                    rightPosition = j - 1;
                }
            }
            if (rightPosition < 0) { // duplicate,remove the element from the sorted array.
                sortedArray[wall] = null;
            }else{ // not a duplicate, shift the element and put the element in the right spot
                for (int j = wall; j > rightPosition; j--) {
                    sortedArray[j] = sortedArray[j - 1];
                }
                sortedArray[rightPosition] = array[i];
                wall++; //forward the wall one position
            }
        }
        
        System.out.println(Arrays.toString(sortedArray));
    }

我不知道您正在进行的某种练习是否需要这样做,但如果您想要更简单的解决方案 - 使用 Java Streams,代码如下:

Stream.of(words).distinct().sorted().collect(Collectors.joining(","))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-25
    • 1970-01-01
    • 2018-03-17
    • 1970-01-01
    相关资源
    最近更新 更多