Java 中的 char 数组的选择排序

Selection sort on an array of char in Java

尝试通过将字符串转换为 char 数组来对字符串进行选择排序,这一切对我来说似乎都是正确的,但它并没有完全对数组进行排序,returns 半排序数组。

public class Anagram {
    public static void main(String[] args) {
        String str1 = "ksdvbjksbjdjkk";
        int minInd,
        min,
        currChar;
        char temp;
        char str1arr[] = str1.toCharArray();

        for (int i = 0; i < str1arr.length; i++) {
            minInd = i;
            min = str1.charAt(i);
            for (int j = i; j < str1arr.length; j++) {
                currChar = str1.charAt(j);
                if (currChar < min) {
                    minInd = j;
                }
            }
            temp = str1arr[minInd];
            str1arr[minInd] = str1arr[i];
            str1arr[i] = temp;
        }
        for (char e: str1arr) System.out.print(e);
    }
}

输出:

jkbsbdkvdjjkks

错误出在你的算法逻辑上。应该是这样的:

  1. 在数组上从 arr[1] 迭代到 arr[n]。
  2. 将当前元素(键)与其前身进行比较。
  3. 如果关键元素小于其前身,将其与 之前的元素。将更大的元素向上移动一个位置 为交换的元素制作 space。

前两步你做对了,但对于第三步(嵌套循环),你应该朝相反的方向走,如果元素大于键,则用你的键交换元素。

可以参考这篇文章,有更好的解释:https://www.geeksforgeeks.org/insertion-sort/ 一个算法的逻辑摘自这篇文章

public class Test {
    public static void main(String[] args) {
        String str1 = "ksdvbjksbjdjkk";
        int minInd,
        min,
        currChar;
        char temp;
        char str1arr[] = str1.toCharArray();

        for (int i = 0; i < str1arr.length; i++) {
            minInd = i;
            min = str1arr[i];    // 1 mistake
            for (int j = i; j < str1arr.length; j++) {
                currChar = str1arr[j];    // 1 mistake
                if (currChar < min) {
                    minInd = j;
                    min = currChar;    // 2 mistake
                }
            }
            temp = str1arr[minInd];
            str1arr[minInd] = str1arr[i];
            str1arr[i] = temp;
        }

        for (char e : str1arr)
            System.out.print(e);
        System.out.println();
    }
}
  1. str1始终不变;但str1arr每一轮for循环都改变;
  2. min 需要将最小字符从 i 更新为 j.

试试这个代码。

public class SELSORT{
static void selectionSort(char a[],int n)
{
    for(int i = 0; i < n - 1; i++)
    {
        int min_index = i;
        char t1 = a[i];
        String mst= String.valueOf(t1);
        for(int j = i + 1; j < n; j++)
        {
            String t=String.valueOf(a[j]);
            if(t.compareTo(mst) < 0)
            {
                mst = String.valueOf(a[j]);
                min_index = j;
            }
        }

        if(min_index != i)
        {
            char temp = a[min_index];
            a[min_index] = a[i];
            a[i] = temp;
        }
    }
}

public static void main(String args[])
{
    String test="asdqwdfnshvbsudgfuyqsadbcsabcuwnbcsnm";
    char a[] = test.toCharArray();
    int n = a.length;
    System.out.println("Given array is");
    for(int i = 0; i < n; i++)
    {
        System.out.print(a[i]+" ");
    }
    System.out.println();
    selectionSort(a, n);
    System.out.println("Sorted array is");

    for(int i = 0; i < n; i++)
    {
        System.out.print(a[i]+" ");
    }
}

}