Reputation:
Writing a code that should read two already sorted arrays from a user of the user's defined length, then sort them into one still-sorted list. For some reason I can't get it to sort past the first three digits? It looks like it might be something really simple that I'm missing but I've been fiddling with it for a while and not finding anything. Any ideas?
Sample Run:
How long are the lists?: 3
Enter list A:
1: 1.0
2: 2.0
3: 3.0
Enter list B:
1: 2.0
2: 3.0
3: 4.0
Merged List: 1.0, 2.0, 2.0, 3.0, 3.0, 4.0
code:
import java.util.Scanner; //import scanner
public class project2 {
public static void main (String[] args){
Scanner input = new Scanner(System.in); //scanner for input
int a = 0;
System.out.println("How long are the lists? ");
System.out.print("(The lists should be the same length): ");
a = input.nextInt();
double [] lista = new double [a]; //create array
double [] listb = new double [a]; //create array
int count=1;
System.out.println("Enter numbers for list A:");
for(int j = 0; j < a ; j++){ //user input numbers loop into array "list"
System.out.print(count + ": ");
lista[j] = input.nextDouble();
count++;
}
int acount = 1;
System.out.println("Enter numbers for list B:");
for(int j = 0; j < a ; j++){ //user input numbers loop into array "list"
System.out.print(acount + ": ");
listb[j] = input.nextDouble();
acount++;
}
System.out.println(); // print the original lista inputed by user
System.out.print("Your list A: ");
for(int j=0; j<a; j++){
System.out.print(lista[j] + " ");
}
System.out.println(); // print the original listb inputed by user
System.out.print("Your list B: ");
for(int j=0; j<a; j++){
System.out.print(listb[j] + " ");
}
project2.merge(lista, listb);
System.out.println(); // print the new merged listc
System.out.print("Merged List: ");
for(int j=0; j<a; j++){
System.out.print(project2.merge(lista, listb)[j] + ", ");
}
}
public static double [] merge ( double [] list1, double [] list2){
double [] listc = new double [list1.length + list2.length]; //create array
int i = 0, j = 0, k = 0;
while (i < list1.length && j < list2.length)
{
if (list1[i] < list2[j])
{
listc[k] = list1[i];
i++;
}
else
{
listc[k] = list2[j];
j++;
}
k++;
}
while (i < list1.length)
{
listc[k] = list1[i];
i++;
k++;
}
while (j < list2.length)
{
listc[k] = list2[j];
j++;
k++;
}
return listc;
}
}
Upvotes: 0
Views: 680
Reputation: 44071
Why do you not just use the JDK-method java.util.Arrays#sort(double[])
instead of reinventing the wheel?
Upvotes: 0
Reputation: 178293
You are merging two arrays of length a
, into an array of length 2 * a
, but you only print out the first a
numbers. Modify your for
loop condition that prints out the merged array:
for(int j=0; j< 2*a; j++){
Also, every time you call merge
, you merge the arrays again. Just merge it once, then refer to the array when printing the contents. This version uses the new merged array's length
to stop the loop.
double[] listc = project2.merge(lista, listb);
System.out.println(); // print the new merged listc
System.out.print("Merged List: ");
for(int j=0; j < listc.length; j++){
System.out.print(merged[j] + ", ");
}
Upvotes: 3
Reputation: 4843
Hint: Since you are merging two sorted lists into one (output) sorted list, consider a loop that you execute for the size of the output list .. and put into the next output slot the next appropriate value from each of the two input lists.
Inside that loop, what would you want to compare to see which value you get next? How would you handle exhausting one list before the other? How can you generalize your program so the two input lists do not need to be the same length?
(no code for homework)
Upvotes: 2