Computers

posted by .

//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

  • Computers -

    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

  • Computers -

    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?

  • Computers -

    Ouch. Typing too fast. Should test in that else that l1 hasn't exceeded 10. But you should get the idea.

Respond to this Question

First Name
School Subject
Your Answer

Similar Questions

  1. Java help please

    I am trying to run the following program and am getting this error: java.util.InputMismatchException import java.io.*; import java.util.*; public class Pre2 { static Scanner console = new Scanner(System.in); public static void main(String[] …
  2. Java

    1 public class testOperators 2 { 3 public static void main(String[] args) 4 { 5 int x; 6 int y = 12 7 double z = 13.0; 8 x = 14 9 System.out.println(“x + y + z = “ + (x + y +z)); 10 x += y; 11 y--; 12 z++; 13 z *= x; 14 System.out.println(“x …
  3. Computer Science

    //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; …
  4. Computer Science-Java

    Hi, I am writing an application class for a Lunar Calendar I created in a service class and I have to make sure it is able to detect that the year entered is not in the range of values(I have done this in the service class using a …
  5. Java Panel Problem

    I am supose to draw a grid and center it with a 10 pix border all the way around it. I have done this but no matter what I do I cannot get a 10 pix border. Here is my code. You will notice i have added 20 to the setsize param to account …
  6. computer science

    Im trying to create an arraylist that's sorted but I have no idea how to sort the numbers. I cant use the array.sort method here is my code: public class Testing { private int [] data; private int count; private static int defCapacity …
  7. programming

    m trying to create an arraylist that's sorted but I have no idea how to sort the numbers. I cant use the array.sort method here is my code: public class Testing { private int [] data; private int count; private static int defCapacity …
  8. Computer Programming

    I already posted this question but I gave it a try I'm not sure if I'm on the right track: Make a util class with three methods. First method's name is add, that returns the result by adding two numbers. Second method's name is multiply, …
  9. Java programming

    Okay so I am rewrite the program and it's still not coming right can anyone help me see what's going wrong here please I have to turn this in by tmw so here the program The following program is supposed to read two numbers from a file …
  10. Java programming

    Hi, I'm trying to figure out how to implement toString() methods into the JavaTest and H2ClassB classes so that the output would be 4 3 7 5 99 3: import java.util.ArrayList; public class JavaTest { ArrayList <JavaTest> list = …

More Similar Questions