为什么我的选择排序算法没有做到它应该做的(java)?

时间:2017-07-08 20:12:14

标签: java arrays algorithm sorting

在过去的几个小时里,我尝试用Java编写选择排序算法而不查看已完成的代码。我刚刚读了算法是如何工作的(通过文字)。

我不会复制粘贴说明而是描述它(以检查我是否理解它):

- 我们有一个未排序的数组A和一个空数组B(与A具有相同的大小)

- 取出未排序的数组A并找到它的最小元素。找到最小的元素,现在使用未排序的数组A的第一个元素切换此元素

- 将第一个元素(=数组A的最小元素)放入数组B

- 重复,直到我们完成了A

的每个元素

我尝试用Java编写代码:

public class Selectionsort{

    public static void main(String[] args) {
        int[] myArray = {9,6,1,3,0,4,2};
        int[] B = new int[myArray.length];

        for(int i=0; i<myArray.length; i++) {
            B[i]=findMIN(myArray, i);
        }
    }

    static int findMIN(int[] A, int c) {
        int x = A[c];
        while(c<A.length) {
            if(x>A[c]) {
                x=A[c];
            }
            c++;
        }
        return x;
    }
}

但我得到一个奇怪的输出:

0 0 0 0 0 2 2 

6 个答案:

答案 0 :(得分:3)

&#34;找到最小的元素,现在用未排序的数组A&#34;的第一个元素切换这个元素。因此,您必须使用最小元素切换第一个元素。您可以使用以下代码:

static int findMIN(int[] A, int c) {
    int first = c; // The first element's id
    int id = c; // An id of the smallest element
    int x = A[c];
    while(c<A.length) {
        if(x>A[c]) {
            x=A[c];
            id = c;
        }
        c++;
    }

    // Switching the first element with the smallest element
    int tmp = A[first];
    A[first] = A[id];
    A[id] = tmp;

    return x;
}

完美无缺。如果你不理解你的代码为什么能够正常工作,请查看@ VidorVistrom的答案。

答案 1 :(得分:3)

  

为什么我的选择排序算法没有做到它应该做的事情   (JAVA)?

让我们看看发生了什么:

   c         A[c]       Returned X
-----------------------------------
   0          9            0(Since minElement is 0 with index >= 0)
   1          6            0(Since minElement is 0 with index >= 1)
   2          1            0(Since minElement is 0 with index >= 2)
   3          3            0(Since minElement is 0 with index >= 3)
   4          0            0(Since minElement is 0 with index >= 4)
   5          4            2(Since minElement is 2 with index >= 5)
   6          2            2(Since minElement is 2 with index >= 6)

答案 2 :(得分:3)

首先,修复您的findMIN,您应该返回最小元素的索引(并将当前值的元素与最小值进行比较)。像,

static int findMIN(int[] A, int c) {
    int x = c;
    for (; c < A.length; c++) {
        if (A[c] < A[x]) {
            x = c;
        }
    }
    return x;
}

然后我会使用swap方法,比如

static void swap(int[] A, int a, int b) {
    if (a != b) {
        int t = A[a];
        A[a] = A[b];
        A[b] = t;
    }
}

最后,像

一样把它绑在一起
public static void main(String[] args) {
    int[] myArray = { 9, 6, 1, 3, 0, 4, 2 };

    for (int i = 0; i < myArray.length; i++) {
        swap(myArray, i, findMIN(myArray, i));
    }
    System.out.println(Arrays.toString(myArray));
}

而且,我得到了

[0, 1, 2, 3, 4, 6, 9]

答案 3 :(得分:3)

您的findMIN函数始终从myArray索引开始返回ith的最小值。您获得前5个值0,因为myArray[4]包含0。然后,对于最后两位数字,您将获得2,因为从myArray[5]myArray[6]最小值为2

在您的findMIN函数中找到最小值后,您应该将此值与初始索引交换&#39; c&#39;你进入了这个功能。在下一次调用findMIN函数时,它将排除您之前获得的最小值。

由于你不想看到任何实现,我试着用语言说出解决方案。希望它可以帮到你。

答案 4 :(得分:1)

当您找到最低要求时,您不会交换元素。 试试这个修复

static int findMIN(int[] A, int c) {
    int x = A[c];
    int min_index = c;
    for(int i=c; i<A.length; i++) {
        if(x>A[i]) {
            x=A[i];
            min_index = i;
        }
    }
    A[min_index] =  A[c];
    A[c] = A[min_index];
    return x;
}

同时知道选择排序是in place algorithm,因此您不需要单独的空数组进行排序。

答案 5 :(得分:1)

public class Selectionsort {

    public static void main(String[] args) {
        int[] myArray = {9, 6, 1, 3, 0, 4, 2};

        for (int i = 0; i < myArray.length; i++) {

            int pos = findMIN(myArray, i);
            int element = myArray[pos];

            //swap 
            int temp = myArray[i];
            myArray[i] = element;
            myArray[pos] = temp;

        }

        System.out.println(Arrays.toString(myArray));
    }

    //get the postion 
    static int findMIN(int[] A, int c) {
        int x = A[c];
        int position = c;
        while (c < A.length) {
            if (x > A[c]) {
                x = A[c];
                position = c;
            }
            c++;
        }
        return position;
    }
}
相关问题