Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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_Sorting_Duplicates_Selection Sort - Fatal编程技术网

Java 选择排序从两端开始依次递增 我已经编写了一个选择类的修改版本,在这里我考虑数组的最小值和最大值,并将它们放在两端

Java 选择排序从两端开始依次递增 我已经编写了一个选择类的修改版本,在这里我考虑数组的最小值和最大值,并将它们放在两端,java,sorting,duplicates,selection-sort,Java,Sorting,Duplicates,Selection Sort,算法是这样工作的 1. Find the minimum and the maximum value in the list. 2. Swap the minimum value with the value in the first position. 3. Swap the maximum value with the value in the last position. 4. Repeat the steps above for the remainder of the list (s

算法是这样工作的

1. Find the minimum and the maximum value in the list.
2. Swap the minimum value with the value in the first position.
3. Swap the maximum value with the value in the last position.
4. Repeat the steps above for the remainder of the list 
(starting at the second position and ending at the second to 
 last position and narrowing the range of positions examined 
 from both ends of the array each time).
不幸的是,上面显示了具有重复值的数组的意外结果

比如说,

[9, 37, 12, 1, 13, 31, 5, 37, 36, 29, 19, 22, 20, 15, -1, 23]
被归类为

[-1, 1, 5, 9, 12, 13, 15, 19, 20, 22, 23, 29, 31, 37, 36, 37]
事实上,这里的主要问题是,除了简单地针对重复项之外,该算法通常没有对数组后半部分中的元素进行适当的排序

这是我的伪代码

    int i=0;
    while(i<=(arr.length-i-1)) {
      int minIndex = i;
      int maxIndex=arr.length-i-1; 
      for (int j = i+1; j <=arr.length-i-1; j++) {

       if (arr[j] <=arr[minIndex]) {
         minIndex = j;      
         } 
       if(arr[j]>=arr[maxIndex]){
          maxIndex = j; 
         }
      }
      swap(arr, i, minIndex);
      swap(arr, (arr.length-i-1), maxIndex); 
    i++;
    }

在内部for循环的终止条件中有一个off by one错误

  for (int j = i+1; j <=arr.length-i-1; j++) {

<代码> >(int j=i+1;j<p>ok),问题是最大值在迭代的最小位置开始。第二次考虑问题数组中的循环:

-1,37,12,1,13,31,5,23,36,29,19,22,20,15,9,37
i是1,len-i-1是14。循环之后,maxindex是1,minIndex是3

因此,交换1(i)和3(minIndex):

然后是14(len-i-1)和1(maxIndex):

哦,基本上,你需要同时进行两次交换


编辑在两个重叠交换的情况下,并行性并没有真正的帮助,因为每个交换都希望在一个数组插槽中放入不同的值;您必须解决冲突。我认为@codaddict的解决方案很好。

问题发生在
I
恰好是
maxIndex
时。要解决这个问题您是否需要添加:

swap(arr, i, minIndex);
if(i == maxIndex) {
     maxIndex = minIndex;
}
swap(arr, (arr.length-i-1), maxIndex);

我认为你做的交换太多了——也就是说,你在做交换,而不管你是否真的找到了一个新的最小值或最大值。此外,你不需要同时做这两个测试(最小值和最大值,因为你不能同时做这两个测试,除非是在平凡的相等情况下,在这种情况下,它们的顺序并不重要……)

我认为以下是对您的算法的更好描述:

int arrayLength = arr.length;
for (int i=0; i<arrayLength; i++){
  for (int j=i+1; j<arrayLength; j++){
    int v = arr[j];
    if (v < arr[i]){
      swap(arr, i, j);
    } else if (v > arr[(arrayLength -1)]){
      swap(arr, (arrayLength -i-1), j);
    }
  }
}
int arraylelength=arr.length;

对于(inti=0;i以及在评论中,您的第一次交换会做得很好,但是您的第二次交换会产生问题

在maxindex变量位于i的情况下,您正确地放置了最小项,但丢失了最大项。这可以通过添加额外的规定来解决,该规定检查
if(i==maxterm)
,并通过正常进行第一次交换和第二次交换maxterm和minterm位置来处理这种特殊情况。从0遍历数组到
数组是没有用的。长度-1


我建议从0遍历到(array.length-1)/2,由于您在两侧排序。结束部分已排序,开始部分也已排序,为什么还要进行额外的迭代?我已经编写了一个完全相同的程序,其中包含我建议的更正,还可以检查数字是否已排序,并立即终止循环。

您可以发布完整的代码吗?我认为没有错误在你上面展示的内容中。你验证了你的交换函数了吗?你可以发布它吗?我可以…但我不能可视化它。@LadiesMan217-你能在你的数组中再添加一个-1,看看它是否对最小值有相同的作用吗?@mjbnz我应该在上面代码的哪个步骤中添加它?@LadiesMan217-不,添加到你的输入数组中,这样你就有两个minumum value.Nope.这是错误的。如果i=0,那么上面的表达式工作得很好。我尝试了你的修改,但没有任何效果。这不是问题-他只会再次尝试最后一个索引-这应该不会影响结果。宾果-这是我的潜意识看到的,但我无法想象它。我如何做平行swap?似乎很难完成这里也有一个问题。我用你的方法尝试了这个
[25,38,19,38,36,3,-2,27,7,29,41,3,-2,14,-2,23]
,结果是
[-2,-2,-2,3,3,7,14,23,19,25,27,29,36,38,41]
。问题出在
23,19,25
哦,我的错误。我从
I+1
开始内环,而不是
I
。等等,你能试试
奇数
输入吗,15可能是吗?我想我用奇数输入得到了更糟糕的结果。对于15号,输入是
[41,34,29,0,27,21,35,29,9,30,37,38,34,4,38]
输出是
[0,9,4,21,27,29,29,30,34,34,35,37,38,38,41]
用你的方法。现在问题在上半部分,这里是
0,9,4
@ladiesMan217:我相信你对女士很好,请你也做好复印工作:P@codaddict的解决方案确实有效。它解决了这个问题-如果第一次交换移动了最大值,请更改“maxIndex”以跟随它。简单有效请避免使用“…”替换所有标点符号。句号、逗号等也很酷!并提高可读性。
-1,9,12,37,13,31,5,23,36,29,19,22,20,15,1,37
swap(arr, i, minIndex);
if(i == maxIndex) {
     maxIndex = minIndex;
}
swap(arr, (arr.length-i-1), maxIndex);
int arrayLength = arr.length;
for (int i=0; i<arrayLength; i++){
  for (int j=i+1; j<arrayLength; j++){
    int v = arr[j];
    if (v < arr[i]){
      swap(arr, i, j);
    } else if (v > arr[(arrayLength -1)]){
      swap(arr, (arrayLength -i-1), j);
    }
  }
}
int arrayLength = arr.length;
for (int i=0; i<arrayLength; i++){
  for (int j=i+1; j<arrayLength; j++){
    int v = arr[j];
    if (v < arr[i]){
      swap(arr, i, j);
    } 
  }
}