Posted by Pedro on Thursday, October 1, 2009 at 12:29pm.
This is one of the classic workhorse situations of programming, a sort-merge, used just about everywhere. You sort two lists, then merge them.
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
Yes, you have correctly sorted and printed the first list, and presumably the same for the second.
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?
Ouch. Typing too fast. Should test in that else that l1 hasn't exceeded 10. But you should get the idea.
Related Questions
Computer Science - //program that puts two lists in order in one single list ...
Java help please - I am trying to run the following program and am getting this ...
Computer Science-Java - Hi, I am writing an application class for a Lunar ...
Computer Science Computer Programming - Please help! I cannot figure out what I ...
Java programming - can you show me where is the wrong for this program..because ...
programming - Can anyone help me with this really confusing programming problem...
Computer Science - Consider the following application files: /JavaCS1/src/...
Computer Science - The digits 0, 1, and 8 look much the same if rotated 180 ...
Programming - Hey guys, I seem to be having a bit of trouble with my assignement...
programming - A hotels occupancy rate is calculated as follows: Occupancy ...
For Further Reading