Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 反向选择排序数组_Java_Arrays_Algorithm_Selection Sort - Fatal编程技术网

Java 反向选择排序数组

Java 反向选择排序数组,java,arrays,algorithm,selection-sort,Java,Arrays,Algorithm,Selection Sort,我在这里的选择排序贯穿数组的其余部分,查找最小值,然后将其交换到前面。我想更改算法,使其在剩余部分也查找最大值,然后将其交换到后面,以便同时从前面和后面建立一个排序列表 public void selectionSort(String[ ] data){ // for each position, from 0 up, find the next smallest item // and swap it into place for (int place=0; pla

我在这里的选择排序贯穿数组的其余部分,查找最小值,然后将其交换到前面。我想更改算法,使其在剩余部分也查找最大值,然后将其交换到后面,以便同时从前面和后面建立一个排序列表

public  void selectionSort(String[ ] data){
    // for each position, from 0 up, find the next smallest item 
    // and swap it into place
    for (int place=0; place<data.length-1; place++){
        int minIndex = place;
        for (int sweep=place+1; sweep<data.length; sweep++){
            if (data[sweep].compareTo(data[minIndex]) < 0)
                minIndex=sweep;
        }
        swap(data, place, minIndex);
    }
}
public void selectionSort(字符串[]数据){
//对于每个位置,从0开始,查找下一个最小的项
//然后把它换到合适的位置

对于(int-place=0;place您只需更改,它将按相反顺序排序:

在方法
selectionSort()中

方法
testSorted()

但若您需要更改顺序,以便它必须从数组的后面开始排序,它将如下所示:

    public static void selectionSort(String[ ] data){
    // for each position, from 0 up, find the next smallest item
    // and swap it into place
    for(int place=data.length-1; place >= 1; place--){
        int maxIndex= place;
        for(int sweep = place-1; sweep >= 0; sweep--){
            if(data[sweep].compareTo(data[maxIndex]) > 0){
                maxIndex = sweep;
            }
        }
        swap(data, place, maxIndex);
    }

您只需更改,它将按相反顺序排序:

在方法
selectionSort()中

方法
testSorted()

但若您需要更改顺序,以便它必须从数组的后面开始排序,它将如下所示:

    public static void selectionSort(String[ ] data){
    // for each position, from 0 up, find the next smallest item
    // and swap it into place
    for(int place=data.length-1; place >= 1; place--){
        int maxIndex= place;
        for(int sweep = place-1; sweep >= 0; sweep--){
            if(data[sweep].compareTo(data[maxIndex]) > 0){
                maxIndex = sweep;
            }
        }
        swap(data, place, maxIndex);
    }

反转结果如何?
java.util.Arrays.reverse(array);
向我们展示您已经尝试过的内容。我看不出有人试图自己编写代码。这几乎是一种冒泡排序……但您为什么要首先尝试优化它?当然是针对较小的O(n^2)“算法,复杂度越低越好。”安德烈亚斯,我已经尝试过了there@LouisWasserman我只是想学习如何做,这样我就可以测试它们并比较分配的性能。我是新手,所以我想看看我所做的是否正确,以及如何反转结果?
java.util.Arrays.reverse(array)
向我们展示您已经尝试过的内容。我看不出有人试图自己编写代码。这几乎是一种冒泡行为……但您为什么要首先尝试优化它?当然是针对较小的O(n^2)“算法,复杂度越低越好。”安德烈亚斯,我已经尝试过了there@LouisWasserman我只是想学习如何做,这样我就可以测试它们,并比较作业的表现。我是新手,所以我想看看我所做的是否正确。谢谢。我只是想知道我所做的是否正确。这个答案颠倒了排序顺序,但这不是问题所要求的。问题说明原始代码将最小值移到前面,并且要求将最大值移到后面。移动最小值→ 前和/或最大→ 返回,不改变顺序。好的,谢谢。我只是想知道我所做的是否正确。这个答案颠倒了排序顺序,但这不是问题所要求的。问题说明原始代码将最小值移到了前面,并且要求将最大值移到后面。移动min→ 前和/或最大→ 返回,不改变顺序。
if (data[sweep].compareTo(data[minIndex]) > 0)
if (data[i].compareTo(data[i-1]) > 0)
    public static void selectionSort(String[ ] data){
    // for each position, from 0 up, find the next smallest item
    // and swap it into place
    for(int place=data.length-1; place >= 1; place--){
        int maxIndex= place;
        for(int sweep = place-1; sweep >= 0; sweep--){
            if(data[sweep].compareTo(data[maxIndex]) > 0){
                maxIndex = sweep;
            }
        }
        swap(data, place, maxIndex);
    }