java - 已经排序的数组

标签 java arrays sorting

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

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

我首先尝试制作 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 = 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;        
}

关于java - 已经排序的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26488328/

相关文章:

java - Blackberry JDE 生成错误的 .jad 文件(与编译的 .cod 文件不匹配)

arrays - 从另一个PowerShell中删除一个阵列的内容

java - 如何在执行长时间操作时关闭 Vaadin 8 确认对话框

java - 使用 Apache TIKA 获取页面的内容、关键字、标题

javascript - 循环中的 Array.pop() 真的比 Array.length = 快 50 倍吗

在C程序中将2个数组复制到1个数组

sorting - 重置 Redis 排序集

c - 希尔排序的时间复杂度

ruby - 如何根据ruby中的特定列表对结果进行排序

java - JFrame 图标未在 Ubuntu 12.04 中显示