Java:组合排序函数计算出错误的结果
Java: combined sorting-functions calculate wrong results
我有一个程序可以生成 6 行数字(整数数组)。
我将输出传递给另一个程序,该程序使用 BubbleSort 算法对其进行排序并将其写入文本文件。
如果第一个程序在没有通过的情况下使用,它工作正常,没有重复的数字没有零。但是当排序时有重复的数字,甚至我看到了零,零的情况我无法重现 atm,但出现了两次数字。它可能与 multithreading/parallel 处理或执行它的环境有关,并且由 amd 多核 win 10 主机和 deb jessie 来宾组成。
java LottoArray | java BubbleSort>test2.txt
//终端
test2.txt
2 13 16 20 27 40
9 14 17 21 25 41
6 11 11 19 27 44
4 10 25 34 39 47
11 12 17 36 44 48
1 15 23 31 39 40
3 22 22 23 33 45
1 25 26 26 35 49
11 14 24 25 41 49
6 6 14 17 38 46
4 19 19 28 35 39
正如您所看到的,最后一行之前的行中的 6 是双倍的,22 和 11。
public class LottoArray{
public static void main (String [] args){
for(int o=0;o<=10;o++){
int Reihe [] = new int [6];
int zahl;
int j=0;
int i= 0;
while(j<Reihe.length){
zahl = (int) (Math.random()*50);
boolean schonda = false;
while ( i<j){
if(Reihe[i]== zahl)
schonda=true;
i++;
}
if(schonda==false && zahl !=0){
Reihe[j]=zahl;
j++;}
}
for(int z=0;z<6;z++){
System.out.print(Reihe[z]+" ");
}
System.out.println();
}
}
}
public class BubbleSort {
public static void main(String args[]) {
int arr[]= new int[6];
while(!StdIn.isEmpty()){
for(int i=0;i<6;i++)
arr[i]= StdIn.readInt();
boolean getauscht;
do {
getauscht= false;
for (int i=0; i<arr.length-1; i++) {
if ( arr[i] > arr[i+1]) {
int tmp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = tmp;
getauscht = true;
}
}
}while(getauscht);
for (int i=0; i<arr.length; i++)
System.out.print(arr[i]+" " );
System.out.println();
}
}
}
如果我使用不带 bubbleSort 的代码并将输出流式传输到文本文件中,则没有重复的数字也没有零,因为这应该是不可能的,因为我对条件进行了编码 if(schonda==false && zahl !=0)
15 2 20 5 26 34
13 6 15 33 12 37
44 17 16 23 40 25
25 47 10 43 40 44
25 29 3 30 10 41
32 1 23 35 43 28
9 34 28 32 33 25
5 46 31 16 25 9
9 13 16 18 40 5
29 15 16 2 16 15
34 33 44 13 43 48
有人遇到过这种不应该出现的数字吗?
您的问题出在 LottoArray 的这个块中:
int j=0;
int i= 0;
while(j<Reihe.length){
zahl = (int) (Math.random()*50);
boolean schonda = false;
while ( i<j){
if(Reihe[i]== zahl)
schonda=true;
i++;
}
if(schonda==false && zahl !=0){
Reihe[j]=zahl;
j++;
}
}
- 第一次从上面进入
while (i<j){
循环(第一个元素),i和j都是0,所以循环没有执行。
- 第二次(查第二个数),
i
为0,j
为1,于是循环执行,增加i
。
- 第三次(检查第三个数),
i
是1,j
是2。
- 其余相同,
i
总是 j-1
。
这是一个错误,因为您没有开始检查第一个元素。我想你只是运气不好地使用 BubbleSort 得到重复项,因为错误不存在。
要解决此问题,请在第一个 while
内初始化 i
,与 schonda
var 相同的位置,而不是在 j
之上。
我有一个程序可以生成 6 行数字(整数数组)。 我将输出传递给另一个程序,该程序使用 BubbleSort 算法对其进行排序并将其写入文本文件。 如果第一个程序在没有通过的情况下使用,它工作正常,没有重复的数字没有零。但是当排序时有重复的数字,甚至我看到了零,零的情况我无法重现 atm,但出现了两次数字。它可能与 multithreading/parallel 处理或执行它的环境有关,并且由 amd 多核 win 10 主机和 deb jessie 来宾组成。
java LottoArray | java BubbleSort>test2.txt
//终端
test2.txt
2 13 16 20 27 40
9 14 17 21 25 41
6 11 11 19 27 44
4 10 25 34 39 47
11 12 17 36 44 48
1 15 23 31 39 40
3 22 22 23 33 45
1 25 26 26 35 49
11 14 24 25 41 49
6 6 14 17 38 46
4 19 19 28 35 39
正如您所看到的,最后一行之前的行中的 6 是双倍的,22 和 11。
public class LottoArray{
public static void main (String [] args){
for(int o=0;o<=10;o++){
int Reihe [] = new int [6];
int zahl;
int j=0;
int i= 0;
while(j<Reihe.length){
zahl = (int) (Math.random()*50);
boolean schonda = false;
while ( i<j){
if(Reihe[i]== zahl)
schonda=true;
i++;
}
if(schonda==false && zahl !=0){
Reihe[j]=zahl;
j++;}
}
for(int z=0;z<6;z++){
System.out.print(Reihe[z]+" ");
}
System.out.println();
}
}
}
public class BubbleSort {
public static void main(String args[]) {
int arr[]= new int[6];
while(!StdIn.isEmpty()){
for(int i=0;i<6;i++)
arr[i]= StdIn.readInt();
boolean getauscht;
do {
getauscht= false;
for (int i=0; i<arr.length-1; i++) {
if ( arr[i] > arr[i+1]) {
int tmp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = tmp;
getauscht = true;
}
}
}while(getauscht);
for (int i=0; i<arr.length; i++)
System.out.print(arr[i]+" " );
System.out.println();
}
}
}
如果我使用不带 bubbleSort 的代码并将输出流式传输到文本文件中,则没有重复的数字也没有零,因为这应该是不可能的,因为我对条件进行了编码 if(schonda==false && zahl !=0)
15 2 20 5 26 34
13 6 15 33 12 37
44 17 16 23 40 25
25 47 10 43 40 44
25 29 3 30 10 41
32 1 23 35 43 28
9 34 28 32 33 25
5 46 31 16 25 9
9 13 16 18 40 5
29 15 16 2 16 15
34 33 44 13 43 48
有人遇到过这种不应该出现的数字吗?
您的问题出在 LottoArray 的这个块中:
int j=0;
int i= 0;
while(j<Reihe.length){
zahl = (int) (Math.random()*50);
boolean schonda = false;
while ( i<j){
if(Reihe[i]== zahl)
schonda=true;
i++;
}
if(schonda==false && zahl !=0){
Reihe[j]=zahl;
j++;
}
}
- 第一次从上面进入
while (i<j){
循环(第一个元素),i和j都是0,所以循环没有执行。 - 第二次(查第二个数),
i
为0,j
为1,于是循环执行,增加i
。 - 第三次(检查第三个数),
i
是1,j
是2。 - 其余相同,
i
总是j-1
。
这是一个错误,因为您没有开始检查第一个元素。我想你只是运气不好地使用 BubbleSort 得到重复项,因为错误不存在。
要解决此问题,请在第一个 while
内初始化 i
,与 schonda
var 相同的位置,而不是在 j
之上。