【问题标题】:Java selection sort not working correctlyJava 选择排序无法正常工作
【发布时间】:2015-12-07 20:00:07
【问题描述】:

我刚刚创建了一个名为“Statistics”的 int[] 类,它有两种选择排序方法,用于按升序或降序列出 Statistics 对象 (int[]) 中的整数。当我使用这两种方法中的任何一种时,它们往往会在大约一半的时间里工作,而在另一半的时间里却不工作。以下是我的意思的几个例子:

新运行

测试1 = {2, 5, 3, 7, 8, 9, 6}

Test1.sortDataDsc() 会给我:Test1 = {8, 7, 6, 9, 3, 5, 2}

Test1A = {8, 7, 6, 9, 3, 5, 2}

Test1A.sortDataAsc() 会给我:{2, 5, 3, 6, 7, 8, 9}

新运行

测试1 = {2, 5, 3, 7, 8, 9, 6}

如果我首先执行 Test1.sortDataAsc(),它将正确地对数据进行排序,并且如果我之后执行此操作,也会正确地按降序对其进行排序。

新运行

Test2 = {7, 4, 5, 8, 0, 1}

Test2.sortDataAsc() 会给我:{1, 0, 4, 5, 7, 8}。

然后它将正确地按降序对这些数字进行排序,然后返回到正确的升序。

如果您以相同的顺序输入数字,我尝试过的所有测试用例都是可重复的。如果您更改数字的顺序,则输出可能是正确的,也可能是不同的错误顺序。我已经排除了我能想到的所有可能导致这种情况的问题,并且我在测试用例之间找不到任何相似之处。如果有人在我的代码中看到任何内容,我可以修复或添加以纠正这种情况,我们将不胜感激。

count = 数组中的元素个数

//sortDataAsc Method - Sorts data elements in Statistics array from least to greatest
public void sortDataAsc(){
    int min, temp;
    for(int index = 0; index < count; index++){
        min = index;
        for(int scan = index + 1; scan < count; scan++){
            if(data[scan] < data[min]){
                min = scan;
            }
        temp = data[min];
        data[min] = data[index];
        data[index] = temp;
        }
    }
}

//sortDataDsc Method - Sorts data elements in Statistics array from greatest to least
public void sortDataDsc(){
    int max, temp;
    for(int index = 0; index < count; index++){
        max = index;
        for(int scan = index + 1; scan < count; scan++){
            if(data[scan] > data[max]){
                max = scan;
            }
        temp = data[max];
        data[max] = data[index];
        data[index] = temp;
        }
    }
}

【问题讨论】:

    标签: java sorting


    【解决方案1】:

    尝试将您的代码更改为

    //sortDataAsc Method - Sorts data elements in Statistics array from least to greatest
    public void sortDataAsc(){
        int min, temp;
        for(int index = 0; index < count; index++){
            min = index;
            for(int scan = index + 1; scan < count; scan++){
                if(data[scan] < data[min]){
                    min = scan;
                }
            } // closing parenthesis here
            temp = data[min];
            data[min] = data[index];
            data[index] = temp;
        }
    }
    
    //sortDataDsc Method - Sorts data elements in Statistics array from greatest to least
    public void sortDataDsc(){
        int max, temp;
        for(int index = 0; index < count; index++){
            max = index;
            for(int scan = index + 1; scan < count; scan++){
                if(data[scan] > data[max]){
                    max = scan;
                }
            } // closing parenthesis here
            temp = data[max];
            data[max] = data[index];
            data[index] = temp;
        }
    }
    

    附:对于升序排序,您可以使用

    Arrays.sort(array);
    

    降序排序

    Integer[] arr = {2, 5, 3, 6, 1};
    Arrays.sort(arr, Collections.reverseOrder());
    

    【讨论】:

    • 我还没有真正运行过这个,但我认为你“交换”元素太多次了。内部 for 循环是找到下一个要交换的元素,然后在该循​​环之后与当前索引交换。
    • 您在每次循环的内部循环中交换元素
    • 不,我是说您在 内部 for 循环之后交换。您的修改现在是正确的。
    • 现在我明白你的意思了。我编辑了我对“交换”元素的回答并不多次(正如你的意思)
    • 更改右括号解决了这个问题。感谢您的帮助!
    猜你喜欢
    • 2016-01-24
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 2014-10-31
    • 2017-04-07
    • 2017-01-18
    • 2012-03-29
    • 2018-07-18
    相关资源
    最近更新 更多