//program that puts two lists in order in one single list with all the numbers in order
import java.util.Scanner;
public class listOrder {
public static void main (String [] args){
int [] list1;
int [] list2;
int [] list3;
int temp;
list1 = new int [10];
list2 = new int [10];
list3 = new int [20];
for (int i = 0; i < 10; i++)
{
list1[i] = (int) (Math.random () * 100)+1;
}
for (int j = 0; j < 10; j++)
{
for (int i = 0; i < 10; i++)
{
if (list1[i] > list1[j])
{
temp = list1[i];
list1[i] = list1[j];
list1[j] = temp;
}
}
}
for (int i = 0; i < 10; i++)
{
System.out.print(" list1[" + i + "] = " + list1[i] + " / ");
}
System.out.println (" ");
for (int i = 0; i < 10; i++)
{
list2[i] = (int) (Math.random () * 100)+1;
}
for (int j = 0; j < 10; j++)
{
for (int i = 0; i < 10; i++)
{
if (list2[i] > list2[j] )
{
temp = list2[i];
list2[i] = list2[j];
list2[j] = temp;
}
}
}
so i got these two lists... how do i join them in list3[]?
they are already worked in a way so the get in order from least to greatest...
list3 will have the numbers from list1 and list2, joined will be 20 numbers....
plz help me, thanks
3 answers
The basic idea is this:
Establish a pointer to the first (lowest) entry in each list, then
Loop while any entries unmerged:
1. Compare the value you're pointing to in list 1 against the value you're pointing to in list2.
2. If list1 is lower than list2, then
a) copy the list1 entry to the next available position in list3
b) increment the list1 pointer
c) increment the list3 pointer
... and loop through again.
Imagine you have two random halves of a deck of cards, each half already sorted aces first, etc. Then you woul look at the top card of each stack, and put the lower of the two in your new pile. Keep doing this: look at the top card on each stack, and put the lower of the two on your nw pile. Eventually all the cards will be in the new pile, in order.
Pseudocode looks something like:
l1 = 1 // pointer to list1
l2 = 1 // pointer to list2
l3 = 1 // pointer to list3
do (
if l2 = 10 or list1(l1) <= list2(l2) then // take a number from list1
{
list3(l3)=list1(l1)
l1++
l3++
}
else // take a number from list2
{
list3(l3)=list2(l2)
l2++
l3++
}
end if
} while l1 < 10 and l2 < 10
What you need to do now is to merge the two lists. Here is a possible pseudocode, if your teacher has not already covered this aspect.
1. declare and define three int index pointers i1, i2 and i3 to the beginning of each array (element zero).
2. Merge
do
_ if(list1(i1) less than list2(i2))
_ then
__ list3(i3)=list1(i1); increment i1 & i3;
_ else
__ list3(i3)=list2(i2); increment i2 & i3
_ endif
while i1<10 and i2<10
if i1==10 then copy rest of list2 to list3
else copy rest of list1 to list3
Just information for you:
Your sorting algorithm is called a bubble sort. The indices of the inner loop do not need to start from zero. If you do this:
for (int j = 0; j < 10-1; j++)
{
for (int i = j+1; i < 10; i++)
...
You will reduce the sorting work by half.
Also, since you have two arrays to sort, it would be normal to make a method for sorting, so you do not repeat the loop, and better still, when you have a change to make, you do not have to do it two times.
You have declared a scanner, but you don't seem to need it. Are you supposed to read from a file for lists 1 and 2?