简体   繁体   English

运行代码时,当第二个数组应按升序或降序排序时,排序方法会打印相同的确切数组编号

[英]When running code, sorting method prints same exact array numbers when the second array should be sorted in ascending or descending order

The code below is in the selectionSorting method which is just a specific way of sorting.下面的代码在selectionSorting方法中,它只是一种特定的排序方式。 I think the problem is somewhere here but I'm not sure how to fix it.我认为问题出在此处,但我不确定如何解决。

if(orderAscending.isSelected()) {
    boolean sorted = false;
    int counting = arrayInt.length-1;
    int errorCounter = 1;
    int tempNum;
    while(sorted == false) {
        if(errorCounter == 0) {
            sorted = true;
        }else {
            errorCounter = 0;
        }
        if(arrayInt[counting] < arrayInt[counting - 1] && sorted != true) {
            tempNum = arrayInt[counting];
            arrayInt[counting] = arrayInt[counting - 1];
            arrayInt[counting - 1] = tempNum;
            errorCounter++;
            counting--;
        }
        if (counting == 0) {
            counting  = arrayInt.length-1;
        }
    }
    String sOutput = "";
    for(int i = 0; i < arrayInt.length-1; i++) {
        sOutput = sOutput + arrayInt[i] + "\n";
    }
    sortedOutput.setText(sOutput);

Output Output

输出

Your while loop should only include this code您的 while 循环应仅包含此代码

if(orderAscending.isSelected()) {
            boolean sorted = false;
            int counting = arrayInt.length-1;
            int errorCounter = 1;
            int tempNum,secondLoop;
            while(counting>0) {
                
               secondLoop=0;
             while(secondLoop<=counting){
                if(arrayInt[counting] < arrayInt[secondLoop]) {
                    tempNum = arrayInt[counting];
                    arrayInt[counting] = arrayInt[secondLoop];
                    arrayInt[secondLoop] = tempNum;
                }
              secondLoop++;
             }
                    counting--;
            }
            String sOutput = "";
            for(int i = 0; i < arrayInt.length-1; i++) {
                sOutput = sOutput + arrayInt[i] + "\n";
            }
            sortedOutput.setText(sOutput);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM