The Artima Developer Community
Sponsored Link

Java Answers Forum
merging two sorted arrays into a thrid one

9 replies on 1 page. Most recent reply: Oct 17, 2003 2:14 AM by John Channing

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 9 replies on 1 page
Omar

Posts: 7
Nickname: wildheart
Registered: Oct, 2003

merging two sorted arrays into a thrid one Posted: Oct 12, 2003 1:10 AM
Reply to this message Reply
Advertisement
hello everyone. am omar. i like computers. am new to this forum and i've got sooooo many questions to ask you. java is a prg language that i dont like much but well, i have to learn about it. future and all that shit. anywyz, i'll b posting a lot of questions and enquiries this week. man, have i got a shit load to learn about
here's my first question
i've got two integer sorted arrays in ascending order. am supposed to merge these into a third array WITHOUT using a sorting algorithm. so this means that i cant just throw them in a third array and then sort it. nope, that wont work. plus i dont want to have duplicates. if first is 1,2,3 and second is 0,1,10 then third must b 0,1,2,3,10. i've tried so many ways but i just cant get it right.
plz keep it simple ppl. am still new this prg language. :) thank you


Vu Che

Posts: 8
Nickname: bluegreen
Registered: Oct, 2003

Re: merging two sorted arrays into a thrid one Posted: Oct 12, 2003 2:19 AM
Reply to this message Reply
You can use a Hashtable object obtain your goal. Try this snippet:

public int[] mergeArrays(int[] firstarray, int[] secondarray){
Hashtable hash = new Hashtable();
for (int i=0; i<firstarray.length; i++)
hash.put(new Integer(firstarray[i]), new Integer(firstarray[i]));
for (int i=0; i<secondarray.length; i++)
hash.put(new Integer(secondarray[i]), new Integer(secondarray[i]));
Enumeration enum = hash.keys();
int intArrayResult = new int[firstarray.length + secondarray.length];
int index=0;
while (enum.hasNext()){
intArrayResult[index] = (Integer)enum.nextElement();
index++;
}
return intArrayResult
}

Omar

Posts: 7
Nickname: wildheart
Registered: Oct, 2003

Re: merging two sorted arrays into a thrid one Posted: Oct 12, 2003 2:34 AM
Reply to this message Reply
thanx for the reply
but you see, the book that am learning this in (the chp am at now) doesnt have anything about hash. isnt there any other way?
but thank you for ur reply.

Omar

Posts: 7
Nickname: wildheart
Registered: Oct, 2003

Re: merging two sorted arrays into a thrid one Posted: Oct 12, 2003 2:36 AM
Reply to this message Reply
nor does it talk about enumeration

Omar

Posts: 7
Nickname: wildheart
Registered: Oct, 2003

Re: merging two sorted arrays into a thrid one Posted: Oct 12, 2003 7:05 AM
Reply to this message Reply
sorry. i meant TWO SORTED LISTS AND MERGE THEM INTO A THIRD LIST

Vu Che

Posts: 8
Nickname: bluegreen
Registered: Oct, 2003

Re: merging two sorted arrays into a thrid one Posted: Oct 13, 2003 5:01 AM
Reply to this message Reply
Hash and Enumeration are just the means we use to get your goal. If u don't want to study new things, its ok, but how can you handle your problems in general with a huge lack of tools.

by the way, if u want to use the code i gave, there is a small wrong at the while loop, change this way:

enum.hasNext() --> enum.hasMoreElements()
enum.next() --> enum.nextElement()

Joe Parks

Posts: 107
Nickname: joeparks
Registered: Aug, 2003

Re: merging two sorted arrays into a thrid one Posted: Oct 13, 2003 5:20 AM
Reply to this message Reply
What book are you using?

martin cohen

Posts: 3
Nickname: mjcohen
Registered: Oct, 2003

Re: merging two sorted arrays into a thrid one Posted: Oct 13, 2003 4:50 PM
Reply to this message Reply
It seems to me that the OP wants to scan sequentially through the two input arrays, choosing the currint minimum, and put that into the output array.

Dijkstra discusses this in section 16 of his (marvelous) "A Discipline of Programming", and Knuth does it in vol 3, section 5.2.4.

The basic thing is that, if the input arrays have size m and n, the result takes O(m+n) operations, since an output is produced from every comparison.

Using a hashtable for this when the inputs are already sorted seems an example of pessimization.

Omar

Posts: 7
Nickname: wildheart
Registered: Oct, 2003

Re: merging two sorted arrays into a thrid one Posted: Oct 15, 2003 1:29 AM
Reply to this message Reply
thank you all
u want to know wat book am studying, check the post i made when i talked about POST FIX TO INFIX

John Channing

Posts: 17
Nickname: drc
Registered: Jun, 2003

Re: merging two sorted arrays into a thrid one Posted: Oct 17, 2003 2:14 AM
Reply to this message Reply
Take a look at the class Arrays in the java.util package. It has a lot of methods that you will find useful. ;)
John

Flat View: This topic has 9 replies on 1 page
Topic: One of Bruce Eckel's examples Previous Topic   Next Topic Topic: Modelling a Many-to-Many Relationship

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use