Java 为什么这种合并排序会给出不正确的结果?
我的任务是使用用户填充的int数组合并两个数组,我们必须假设用户最多有10000个输入,用户输入负数停止。然后将数组从最小到最大排序并打印出来。起初我认为这很容易,但当我完成时,我开始得到如下输出:Java 为什么这种合并排序会给出不正确的结果?,java,arrays,sorting,Java,Arrays,Sorting,我的任务是使用用户填充的int数组合并两个数组,我们必须假设用户最多有10000个输入,用户输入负数停止。然后将数组从最小到最大排序并打印出来。起初我认为这很容易,但当我完成时,我开始得到如下输出: Enter the values for the first array, up to 10000 values, enter a negative number to quit: 1 3 5 -1 Enter the values for the second array, up
Enter the values for the first array, up to 10000 values, enter a negative number to quit: 1
3
5
-1
Enter the values for the second array, up to 10000 values, enter a negative number to quit
2
4
6
-1
First Array:
1
3
5
Second Array:
2
4
6
Merged Array:
6 1 2 3 4 5
正如你所看到的,六是不合适的地方,我不知道如何修复它。这是源代码,我已经包括了丰富的意见,因为我真的希望你们能帮助我尽你们的能力。如果可以使用相同的精确技术而不在代码中实现新的技术和方法,请这样做。我知道java中有一些方法可以在一行中完成所有这一切,但它是用于更基本级别的赋值
import java.util.Scanner;
public class Merge
{
public static void main(String [] args)
{
Scanner scan = new Scanner(System.in);
int [] first = new int[10000]; //first array, assume 10k inputs max
int [] second = new int[10000]; //first array, assume 10k inputs max
boolean legal = true; //WILL IMPLIMENT LATER
int end = 0; // set how many elements to put in my "both" array
int end2 = 0;// set how many elements to put in my "both" array
System.out.print("Enter the values for the first array, up to 10000 values, enter a negative number to quit");
//get values
for(int i = 0; i<first.length; i++)
{
first[i] = scan.nextInt(); //fill first with user input
if(first[i] <0) //if negative number, stop loop
{
end = i; //get position of end of user input
break;
}
}
System.out.println("Enter the values for the second array, up to 10000 values, enter a negative number to quit");
for(int i = 0; i<second.length; i++) //exact same as the first get values loop
{
second[i] = scan.nextInt();
if(second[i] <0)
{
end2 = i;
break;
}
}
System.out.print("First Array:\n");
for(int i = 0; i<first.length; i++) //print first array
{
if(i == end) //this prevents from printing thousands of zeros, only prints values that user inputed
break;
System.out.println(first[i] + " ");
}
System.out.print("Second Array:\n");
for(int i = 0; i<second.length; i++) //same as printing first array
{
if(i == end2)
break;
System.out.println(second[i] + " ");
}
int [] both = new int[(end)+(end2)]; //instanciate an int array to hold only inputted values from first[] and second[]
int [] bothF = new int[(end)+(end2)]; //this is for my simple sorter algotithm loop
for(int i = 0; i<both.length; i++) //fill both with the first array that was filled
{
both[i] = first[i];
}
int temp = end; // see below
for(int i = 0;i<both.length; i++) //fill array with the second array that was filled(starting from the end of the first array so that the first set is not overwritten
{
if(temp<both.length){ //this prevents an out of bounds
both[temp] = second[i];
temp++;}
}
//simple sorting algorithm
for(int d = both.length -1;d>=0;d--)
{
for(int i = 0; i<both.length; i++)
{
if(both[d]<both[i])
{
bothF[d] = both[d];
both[d] = both[i];
both[i] = bothF[d];
}
}
}
System.out.println("Merged Array:"); //print the results
for(int i = 0; i<both.length; i++)
{
System.out.print(both[i] + " ");
}
//System.out.println("ERROR: Array not in correct order");
}
import java.util.Scanner;
公共类合并
{
公共静态void main(字符串[]args)
{
扫描仪扫描=新扫描仪(System.in);
int[]first=new int[10000];//第一个数组,假设最大输入为10k
int[]second=new int[10000];//第一个数组,假设最大输入为10k
boolean legal=true;//稍后将执行
int end=0;//设置在我的“两个”数组中放置多少个元素
int end2=0;//设置要在我的“两者”数组中放置多少个元素
System.out.print(“输入第一个数组的值,最多10000个值,输入负数退出”);
//获取价值
对于(inti=0;i当然,你可以这样做
for (int i = 0; i < end; i++) {
both[i] = first[i];
}
for (int i = 0; i < end2; i++) {
both[i + end] = second[i];
}
// simple sorting algorithm
for (int d = both.length - 1; d >= 0; d--) {
for (int i = 0; i < d; i++) {
if (both[i] > both[d]) {
int t = both[d];
both[d] = both[i];
both[i] = t;
}
}
}
当然,你可以这样做
for (int i = 0; i < end; i++) {
both[i] = first[i];
}
for (int i = 0; i < end2; i++) {
both[i + end] = second[i];
}
// simple sorting algorithm
for (int d = both.length - 1; d >= 0; d--) {
for (int i = 0; i < d; i++) {
if (both[i] > both[d]) {
int t = both[d];
both[d] = both[i];
both[i] = t;
}
}
}
你的排序算法有问题
它类似于选择排序,因为您可以选择两个元素,如果它们不合适,就交换它们。但是,您不会在应该停止比较的时候停止比较:当索引d
小于索引i
时,基于arr[d]>arr[i]
的比较和交换不再有效
内部循环应以i=d
终止
你的逻辑是这样的:
在第d个循环中,位于d+1
和右侧的元素被正确排序(较大的数字)。这在开始时是正确的,因为在最右侧元素的右侧有0个元素被正确排序
在每个外部循环(使用d
计数器)上,将d个最大元素槽
与每个未排序的元素进行比较,如果另一个元素较大,则交换
这足以对数组进行排序,但如果您开始将d个最大元素槽
与其右侧已排序的元素进行比较,则槽中的数字最终将超过应有的数字。因此,当到达d
时,内部循环应终止。您的排序算法有故障
它类似于选择排序,因为您可以选择两个元素,如果它们不合适,就交换它们。但是,您不会在应该停止比较的时候停止比较:当索引d
小于索引i
时,基于arr[d]>arr[i]
的比较和交换不再有效
内部循环应以i=d
终止
你的逻辑是这样的:
在第d个循环中,位于d+1
和右侧的元素被正确排序(较大的数字)。这在开始时是正确的,因为在最右侧元素的右侧有0个元素被正确排序
在每个外部循环(使用d
计数器)上,将d个最大元素槽
与每个未排序的元素进行比较,如果另一个元素较大,则交换
这足以对数组进行排序,但如果您开始将第d个最大的元素槽与其右侧已排序的元素进行比较,则槽中的数字将超过应有的数字。因此,当到达d
时,内部循环应终止。首先,我将从一些建议开始:
1.给end1和end2初始值作为数组长度
打印部分-而不是中断循环-循环直到i==end(如果第一部分没有更改它,它将保持数组长度)
一个建议是在用户输入上使用一个“while”语句来完成阅读部分(看起来更干净,然后打破循环-但也可以像你一样完成)
尝试使用更多功能
现在来看主要问题——为什么不将两个数组中的数字插入到join数组中,以保持它们的排序
指南:
- 为每个数组保留一个标记
- 如果arr1[marker1]>arr2[marker2],则迭代新的联接数组
将arr2[marker2]插入当前位置的关节阵列。
将1添加到marker2中。然后将相反的值添加到marker2中
(别忘了选择如果两个参数相等会发生什么情况)
这是可以实现的,因为数组首先被排序
练习得开心!首先,我将从一些建议开始:
1.给end1和end2初始值作为数组长度
打印部分-而不是中断循环-循环直到i==end(如果第一部分没有更改它,它将保持数组长度)
一个建议是在用户输入上使用一个“while”语句来完成阅读部分(看起来更干净,然后打破循环-但也可以像你一样完成)
尝试使用更多功能
现在来看主要问题——为什么不将两个数组中的数字插入到join数组中,以保持它们的排序
指南:
- 为每个数组保留一个标记
- 如果arr1[marker1]>arr2[marker2],则迭代新的联接数组
将arr2[marker2]插入当前位置的关节阵列。
将1添加到marker2中。然后将相反的值添加到marker2中
(别忘了选择如果两个参数相等会发生什么情况)
这是可以实现的,因为arra
for(int d = both.length -1;d>=0;d--)
{
for(int i = 0; i<both.length; i++)
{
if(both[d]<both[i])
{
int temp = both[d];
both[d] = both[i];
both[i] = temp;
printArray(both);
}
}
}
[9, 8, 7, 6]=
-> 6879
-> 6789
-> 6798
-> 6978
-> 9678