【问题标题】:BubbleSort Algorithm Returning Undesired Results冒泡排序算法返回不想要的结果
【发布时间】:2017-05-21 13:35:51
【问题描述】:

这是我对冒泡排序算法的实现。

 import java.util.Arrays;

 public class BubbleSort {
    public static void main(String[] args) {
        int[] unsorted = {1,3,5,6,2};
        System.out.println(Arrays.toString(unsorted));
        bubbleSort(unsorted);
        System.out.println(Arrays.toString(unsorted));
    }

    public static void bubbleSort(int[] unsorted){
        int i;
        int j;
        int temp;
        for (i = 0; i < unsorted.length; i++) {
            for (j = 1; j < unsorted.length-1; j++) {
                if (unsorted[i] > unsorted[j]) {
                    temp = unsorted[i];
                    unsorted[i] = unsorted[j];
                   unsorted[j] = temp;
                 }
             }
        }
   }
 }

这是输出:

[1, 3, 5, 6, 2]
[1, 6, 5, 3, 2]

这显然是错误的,但我的逻辑似乎没问题。

【问题讨论】:

  • 你的第二个循环应该开始for (j = i+1; ...
  • 这样做会输出相同的未排序数组 [1, 3, 5, 6, 2] [1, 3, 5, 6, 2]
  • 去掉同一行的-1
  • 第二个循环中的停止条件应该从:j &lt; unsorted.length-1改为:j &lt; unsorted.length
  • 好吧,这行得通....现在我需要了解为什么行得通

标签: java bubble-sort


【解决方案1】:

您的两个问题都与这一行有关:

for (j = 1; j < unsorted.length-1; j++) {

你不想从第一个元素循环,你想从 i 之后的第一个元素循环:

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

你还需要一路走到最后:

for (j = i+1; j < unsorted.length; j++) {

【讨论】:

  • 你能帮我理解为什么从 j=1 循环与 j=i+1 不同。由于 j=1 意味着它从数组的第二个值开始循环,这也应该没问题。
  • 在外循环的第一次迭代中,它们是相同的——所以你猜对了一半!但是,在外部循环的后续迭代中,在您的示例中,J 始终等于 1。在我的示例中,首先 J 等于 1,然后是 2,然后是 3,依此类推。
  • @user3059625 所以如果你说j=1而不是j=i+1,你就是说即使你在i=3j=1而不是j=4。由于您正在执行嵌套 for 循环,因此可能很容易混淆,但您正在通过内部 for 循环进行迭代,然后递增 i。第一次通过时,只要您的逻辑正确,您就可以保证数组的第一个值是最低的,因此 i 会递增,您无需担心。你可以担心下一个值。
  • 感谢 Michael 和 Orin,我现在清楚地看到了区别。 :)
【解决方案2】:
here is the correct code

> import java.util.Arrays;

 public class BubbleSort {
    public static void main(String[] args) {
        int[] unsorted = {1,3,5,6,2};
        System.out.println(Arrays.toString(unsorted));
        bubbleSort(unsorted);
        System.out.println(Arrays.toString(unsorted));
    }

    public static void bubbleSort(int[] unsorted){
        int i;
        int j;
        int temp;
        for (i = 0; i < unsorted.length-1; i++) {
            for (j = i+1; j < unsorted.length; j++) {
                if (unsorted[i] > unsorted[j]) {
                    temp = unsorted[i];
                    unsorted[i] = unsorted[j];
                   unsorted[j] = temp;
                 }
             }
        }
   }
 }

the second loop begins after i so j alway start at i+1 and end at unsorted.length while i end at unsorted.length-1

【讨论】:

  • 虽然这是正确的,但请尝试更深入地解释它为什么会这样工作。仅仅给出答案并不能帮助 OP 学习任何东西,如果他没有学到任何东西,他总是会来 SO 寻求答案。 :)
  • 好吧,这个算法取表中的第一个元素 (i),从 (j) 上的下一个元素开始移动,寻找比第一个更大的数字。这就是为什么我一直走到 unsorted.length-1 的原因,因为如果你到达表格的末尾,那么 j 将不再出现在表格中。 i 移动到第二个元素,因为在第一个循环之后,第一个元素已经在它的位置,所以不需要再上它等等
【解决方案3】:

我在我的电脑里保存了冒泡排序算法,是这个(不复制到新数组):

public static void bubbleSort (int[] v) {
      for (int i=0; i<v.length-1; i++)
         for (int j=v.length-1; j>i; j--)
            if (v[j-1]>v[j]) {
               int aux = v[j-1];
               v[j-1] = v[j];
               v[j] = aux;   
            }
   }

【讨论】:

    【解决方案4】:

    一些事情 1)您可以在循环范围内声明 i 和 j 2) 尝试在两次迭代中保持一致 0 -> 长度 3)小于然后登录 i

    public static void bubbleSort(int[] unsorted) {
        for (int i = 0; i < unsorted.length; i++) {
            for (int j = 0; j < unsorted.length; j++) {
                if (unsorted[i] < unsorted[j]) {
                    int temp = unsorted[i];
                    unsorted[i] = unsorted[j];
                    unsorted[j] = temp;
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-19
      • 1970-01-01
      • 2023-03-28
      • 2013-09-04
      • 2013-11-02
      • 1970-01-01
      • 2016-11-01
      • 2014-12-30
      相关资源
      最近更新 更多