Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.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/3/arrays/13.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_Sorting - Fatal编程技术网

Java 已排序数组

Java 已排序数组,java,arrays,sorting,Java,Arrays,Sorting,我有一个包含用户输入的数组。该程序是关于冒泡排序,选择排序和插入排序。第一个气泡,第二个选择,然后是插入排序 我解决不了一个问题。当代码运行到选择排序时,数组已按气泡排序 起初我尝试制作两个临时数组,以便在选择和插入排序时使用“源数组”,但这些数组再次通过冒泡排序重新排列。(我不明白为什么) 有没有办法对我的数组进行单独排序,或者我必须使它们成为方法?顺便说一句,我正在计算互换和比较。谢谢 System.out.println("• Please enter the number of

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

我解决不了一个问题。当代码运行到选择排序时,数组已按气泡排序

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

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

    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(" ");

}
}
System.out.println(“•请输入分拣袋中的元件数量:”);
length=input.nextInt();
System.out.println(“•元素数:”+长度);
int[]SorBag=新的int[长度];
int[]SorBag2=新的int[长度];
int[]SorBag3=新的int[长度];
System.out.println(“•请输入分拣袋的元素:”);
对于(int i=0;iSorBag[j+1]){
BSwaps++;
temp1=SorBag[j+1];
SorBag[j+1]=SorBag[j];
SorBag[j]=temp1;
}
}
}
系统输出打印(“•气泡排序:”);
对于(int k=0;ki&SorBag3[j]
SorBag2=SorBag
SorBag3=SorBag
SorBag
的引用复制到其他两个数组,而不是仅复制数据。因此,不是:

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(“•请输入分拣袋的元素:”);
对于(int i=0;i
试试这个:

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;        
}
System.out.println(“•请输入分拣袋的元素:”);
对于(int i=0;i
SorBag2=SorBag
SorBag3=SorBag
SorBag
的引用复制到其他两个数组,而不是仅复制数据。因此,不是:

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(“•请输入分拣袋的元素:”);
对于(int i=0;i
试试这个:

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;        
}
System.out.println(“•请输入分拣袋的元素:”);
对于(int i=0;i
您将添加代码的相关部分,对吗?您的代码在哪里?现在您已经上传了代码,once可以看到,由于SorBag2和3指向SorBag,它们也将被排序。与其在main方法中编写所有内容,不如编写单独的方法,然后从main()调用它们。另外,对于Sorbag2和Sorbag3,请编写for循环以手动将sorbag的元素复制到2和3,这样指针就不会指向内存中的同一位置。如果不想创建方法,请创建循环以手动将sorbag的每个元素复制到Sorbag2和Sorbag3。您将添加代码的相关部分,对吗?你的代码在哪里?既然你上传了你的代码,once可以看到,由于SorBag2和3指向SorBag,它们也将被排序。与其在main方法中写入所有内容,不如编写单独的方法并从main()调用它们。另外,对于Sorbag2和Sorbag3,请编写for循环以手动将sorbag的元素复制到2和3,这样指针就不会指向内存中的同一位置。如果不想创建方法,请创建循环以手动将sorbag的每个元素复制到Sorbag2和Sorbag3。没问题。如果这回答了你的问题。另外,建议:使用
sorBag
而不是
sorBag
。这不是必须的,但在大多数编程语言中,变量/字段名以小写开头。没问题。如果答案是否定的