【问题标题】:How could I get this Insertion Sort code in alphabetical order?如何按字母顺序获取此插入排序代码?
【发布时间】:2022-01-20 00:48:19
【问题描述】:

我是编码新手,我按字母从最少到最多的顺序排列代码。请帮助我了解如何将代码按字母顺序排列。

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String[] a = new String[] {"bread", "milk", "cheese", "spinach", "apple", "peanuts"};
        System.out.println("Before Sort:" + Arrays.toString(a));
        insertionSort(a);
        System.out.println("After Sort: " + Arrays.toString(a));

        System.out.println();

        a = new String[] { "Allison", "Neha", "Charley", "Jason", "Tyson", "Miles", "Riley" };
        System.out.println("Before Sort:" + Arrays.toString(a));
        insertionSort(a);
        System.out.println("After Sort: " + Arrays.toString(a));


    }

    public static void insertionSort(String[] a) {
        // Move the marker from index 1 to the last index
        for (int i = 1; i < a.length; i++) {
            // Insert the element at the marker to the right position
            insert(a, i);

        }
    }
    public static void insert(String[] a, int marker) {
        String unsortedElement = a[marker];

        // shift other elements to the right to create the correct position
        int correctPosition = marker;
        for (int i = marker - 1; i >= 0; i--) {
            if (a[i].length() > unsortedElement.length()) {
                a[i + 1] = a[i];
                correctPosition--;
            }
            else {
                break; // stop looping
            }
        }
        // Insert the unsorted element to the correct position
        a[correctPosition] = unsortedElement;
    }
}

【问题讨论】:

    标签: java insertion-sort alphabetical


    【解决方案1】:

    您必须在 insert() 方法中更改条件。

    可以使用compareTo方法比较String对象。

    https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html#compareTo(java.lang.String)

    按字典顺序比较两个字符串。比较是基于 字符串中每个字符的 Unicode 值。

    返回: 如果参数字符串等于该字符串,则值为 0;如果此字符串在字典上小于 0,则值小于 0 比字符串参数;如果此字符串是,则值大于 0 按字典顺序大于字符串参数。

    换句话说,您可以想象 compareTo() 方法的作用类似于使用比例对这些字符串进行“加权”。

    如果 'weights' 相等,则 compareTo() 返回 0,如果第一个字符串比第二个字符串“轻”(作为参数传递的字符串)a 'scales' (compareTo()) 会给你一个负值。

    这就是为什么下面代码中的条件写成

    unsortedElement.compareTo(a[i])

        public static void insert(String[] a, int marker) {
            String unsortedElement = a[marker];
    
            // shift other elements to the right to create the correct position
            int correctPosition = marker;
            for (int i = marker - 1; i >= 0; i--) {
                if (unsortedElement.compareTo(a[i]) < 0) {
                    a[i + 1] = a[i];
                    correctPosition--;
                }
                else {
                    break; // stop looping
                }
            }
            // Insert the unsorted element to the correct position
            a[correctPosition] = unsortedElement;
        }
    

    输出:

    Before Sort:[Allison, Neha, Charley, Jason, Tyson, Miles, Riley]
    After Sort: [Allison, Charley, Jason, Miles, Neha, Riley, Tyson]
    

    【讨论】:

      【解决方案2】:

      您可以使用静态方法Arrays.sort(arr)一次对孔数组进行排序

      import java.util.Arrays;
      ...
      
      String[] arrayOfStrs = {"d","a","ca"};
      
      Arrays.sort(arrayOfStrs);
      
      // arrayOfStrs is now ["a","ca","d"]
      

      【讨论】:

      • 我必须使用相同的代码。我会在哪里添加?
      • 你可以把insertionSort(a);替换成Arrays.sort(a);
      • 你的评论与插入排序算法的实现毫无共同之处
      【解决方案3】:

      要从较少到较多的字母排序,首先创建一个使用 String 长度的 Comparator 类:

      public class StringLengthComparator implements Comparator<String> {
        public int compare(String o1, String o2) {
          return Integer.compare(o1.length(), o2.length());
        }
      }
      

      然后在Arrays.sort(...)方法中使用:

      Arrays.sort(a, new StringLengthComparator());
      

      要按字母顺序对它们进行排序,您只需要这样:

      Arrays.sort(a);
      

      【讨论】:

        猜你喜欢
        • 2020-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-25
        • 2012-11-08
        相关资源
        最近更新 更多