# Need help merging arraylists and sorting

• March 28th, 2011, 06:21 PM
oliminator
Need help merging arraylists and sorting
First time posting here and I'm looking for a little bit of help. Basically what I need to do is develop an algorithm and than code it.

The situation is I need to create two array lists, one 30 numbers and the other 20 (the array lists are full of random numbers), and add them into one array in order from either lowest to highest, or highest to lowest. I can easily figure out what to do with the algorithm, and I know how to add them together, but the trouble I have is adding them in order from low/high or high/low.

I could easily just use the ArrayList add() method and than sort them, but my professor would like it to sort them as each individual number is added. I don't have any code ATM as I'm just looking for some guidance rather than the answer. Not gonna learn with just the answer. Any help is appreciated. I was looking towards using a for loop but not sure in what way.

Thanks for any help ahead of time
• March 28th, 2011, 07:56 PM
helloworld922
Re: Need help merging arraylists and sorting
This looks almost exactly like the merge sort algorithm. Merge sort - Wikipedia, the free encyclopedia

The main part you need is the merge function. It takes 2 lists that are in sorted order (from you're description it looks like you'll need to sort those two lists first, use which ever method you like best), then goes through each list, remove the smaller (or larger, depending on if you're sorting lower-to-higher or higher-to-lower) of the two elements at the front of the lists, then add that element to the front of your new list. Repeat until both lists are empty. If one list is empty, you can "dump" the remaining sorted items in the other list into the end of your new list.
• March 28th, 2011, 07:57 PM
oliminator
Re: Need help merging arraylists and sorting
Code Java:

```import java.util.*; import java.awt.*;   public class ArrayListRandom { public static void main(String[] args) { //New ArrayLists to store the random integers ArrayList <Integer> list1 = new ArrayList <Integer>(30); ArrayList <Integer> list2 = new ArrayList <Integer>(20); ArrayList <Integer> list = new ArrayList <Integer>(50); //Random generator Random ran = new Random(); int g = ran.nextInt(50); int f = ran.nextInt(50); //Adding the randomly generated numbers needed to start the lists list1.add(g); list2.add(f); //Printing out numbers given System.out.println(g); System.out.println(f); //Empty integers to store for later use int i = 0; int k = 0; int q = 0;   //For loop to store the 30 random numbers in order for (i = 0; i < 30; i++) { g = ran.nextInt(50); int a = 0; System.out.println(g); while ((a < list1.size()) && (g > list1.get(a))) { a++; } list1.add(a,g); }   //For loop to store the 20 random numbers in order for (k = 0; k < 20; k++) { f = ran.nextInt(50); int a = 0; System.out.println(f); while ((a < list2.size()) && (f > list2.get(a))) { a++; } list2.add(a,f); }   for (q = 0; q < 50; q++) { int a = 0; list.add(a,list1.get(0)); while ((a < list.size() && (q > list.get(a)))) { a++; } if (list1.get(a) > list2.get(a)) { list.add(a,list1.get(a)); } //else //{ // list.add(list2.get(a)); //} } System.out.println(list1); System.out.println(list2); System.out.println(list); } }```
• March 28th, 2011, 07:57 PM
oliminator
Re: Need help merging arraylists and sorting
That is the code I have so far.
• March 28th, 2011, 08:03 PM
oliminator
Re: Need help merging arraylists and sorting
Quote:

Originally Posted by helloworld922
This looks almost exactly like the merge sort algorithm. Merge sort - Wikipedia, the free encyclopedia

The main part you need is the merge function. It takes 2 lists that are in sorted order (from you're description it looks like you'll need to sort those two lists first, use which ever method you like best), then goes through each list, remove the smaller (or larger, depending on if you're sorting lower-to-higher or higher-to-lower) of the two elements at the front of the lists, then add that element to the front of your new list. Repeat until both lists are empty. If one list is empty, you can "dump" the remaining sorted items in the other list into the end of your new list.

A lot of that looks like foreign language to me and I was wondering if their was any way I could do it in just a quick for loop, as I posted shortly after you.