【问题标题】:Already sorted array已经排序的数组
【发布时间】:2014-10-21 13:48:21
【问题描述】:

我有一个包含用户输入的数组。该程序是关于冒泡排序、选择排序和插入排序的。第一次冒泡,第二次选择,然后是插入排序。

我无法解决问题。当代码运行选择排序时,数组已经被冒泡排序了。

我最初尝试制作 2 个临时数组以在选择和插入排序时使用“源数组”,但这些数组再次通过冒泡排序重新排列。 (我不明白为什么)

有没有办法单独对我的数组进行排序,或者我必须让它们成为方法?顺便说一句,我也在计算交换和比较。谢谢!

    System.out.println("• Please enter the number of elements in the Sorting Bag:");
    length = input.nextInt();
    System.out.println("• The number of elements: " + length);

    int[] SorBag = new int[length];
    int[] SorBag2 = new int[length];
    int[] SorBag3 = new int[length];

    System.out.println("• Please enter the elements of Sorting Bag:");
    for (int i = 0; i < SorBag.length ; i++) {
        SorBag[i] = input.nextInt();
    }

    SorBag2 = SorBag;
    SorBag3 = SorBag;

    System.out.print("• Elements in the Sorting Bag are:");
    for (int j = 0; j < SorBag.length; j++) {
        System.out.print(" " + SorBag[j]);
    }
    System.out.println("");
    System.out.println("");

    //Bubble Sort    
    for (int i = 1; i < SorBag.length; i++) {
        for (int j = 0; j < SorBag.length - i; j++) {
            BComparison++;
            if (SorBag[j] > SorBag[j + 1]) {
                BSwaps++;
                temp1 = SorBag[j + 1];
                SorBag[j + 1] = SorBag[j];
                SorBag[j] = temp1;
            }
        }
    }
    System.out.print("• Bubble Sort:");
    for (int k = 0; k < SorBag.length; k++) {
        System.out.print(" " + SorBag[k] + " ");
    }
    System.out.print("Comparisons: " + BComparison + " Swaps: " + BSwaps);
    System.out.println(" ");

    //Selection Sort
    for (int i = 0; i < SorBag2.length; i++) {
        min = i;

        for (int j = i + 1; j < SorBag2.length; j++) {
            SComparison++;

            if (SorBag2[j] < SorBag2[min]) {
                min = j;
            }

            if (min != i) {

                temp2 = SorBag2[i];
                SorBag2[i] = SorBag2[min];
                SorBag2[min] = temp2;
                SSwaps++;
            }
        }
    }

    System.out.print("• Selection Sort:");
    for (int k = 0; k < SorBag2.length; k++) {
        System.out.print(" " + SorBag2[k] + " ");
    }
    System.out.print("Comparisons: " + SComparison + " Swaps: " + SSwaps);
    System.out.println(" ");

    //Insertion Sort
    for (int i = 1; i < SorBag3.length; i++) {

        int j = 0;

        while (j > i && SorBag3[j] < SorBag3[j - 1]) {

            temp3 = SorBag3[j];
            SorBag3[j] = SorBag3[j - 1];
            SorBag3[j - 1] = temp3;

            ISwaps++;

            j--;
        }

        IComparison++;
    }
    System.out.print("• Insertion Sort:");
    for (int k = 0; k < SorBag3.length; k++) {
        System.out.print(" " + SorBag3[k] + " ");
    }
    System.out.print("Comparisons: " + IComparison + " Swaps: " + ISwaps);
    System.out.println(" ");

}
}

【问题讨论】:

  • 您要添加代码的相关部分,对吧?
  • 你的代码在哪里?
  • 现在你上传了你的代码,你可以看到,由于 SorBag2 和 3 指向 SorBag,它们也已经被排序了。
  • 不要在 main 方法中编写所有内容,而是编写单独的方法并从 main() 调用它们。另外,对于 Sorbag​​2 和 Sorbag​​3,编写 for 循环手动将 sorbag​​ 的元素复制到 2 和 3,这样指针就不会指向内存中的同一个地方。
  • 如果不想制作方法,就制作循环手动将 sorbag​​ 的每个元素复制到 sorbag​​2 和 sorbag​​3 中。

标签: java arrays sorting


【解决方案1】:

SorBag2 = SorBagSorBag3 = SorBagSorBag 的引用复制到其他两个数组,而不是只复制数据。所以而不是:

System.out.println("• Please enter the elements of Sorting Bag:");
for (int i = 0; i < SorBag.length ; i++) {
    SorBag[i] = input.nextInt();
}

SorBag2 = SorBag;
SorBag3 = SorBag;

试试这个:

System.out.println("• Please enter the elements of Sorting Bag:");
for (int i = 0; i < SorBag.length ; i++) {
    int nextInt = intput.nextInt();
    SorBag[i] = nextInt;
    SorBag2[i] = nextInt;
    SorBag3[i] = nextInt;        
}

【讨论】:

  • 没问题。如果这回答了您的问题,please accept it as the answer。另外,建议:使用sorBag 而不是SorBag。这不是必须的,但在大多数编程语言中,变量/字段名称以小写开头。
猜你喜欢
  • 2016-05-18
  • 1970-01-01
  • 1970-01-01
  • 2013-08-09
  • 1970-01-01
  • 1970-01-01
  • 2018-06-28
  • 2020-12-20
  • 2013-11-22
相关资源
最近更新 更多