Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-15-2008, 11:24 PM
Member
 
Join Date: Apr 2008
Posts: 28
fireball2008 is on a distinguished road
Send a message via AIM to fireball2008
how to right a program that find kth number in two sorted array?
There is two sorted array A, and B, how to write a program that runs in log(n)^2 that compute the kth value in the union of this two array.
Any idea is welcome.
I know it has something to do with using binary search.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-16-2008, 01:06 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
What have you done so far!!!!

kind regards,
sukatoa
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-16-2008, 01:24 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,610
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Yes, using binary search you can find the union of two arrays. So, as sukatoa says, what you have done up to now?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-16-2008, 11:04 PM
Member
 
Join Date: Apr 2008
Posts: 28
fireball2008 is on a distinguished road
Send a message via AIM to fireball2008
I only know how to do it in linear time, I have no idea how to do it in log(n)^2
for linear time:
public int find(int k, int j, int i){
int temp = A[i]
int temp2 = B[j]
if(A[i]>B[j])
j++;
else
i++;
if((i+j)==k)
{if(A[i]>B[j])
return A[i];
else
return B[j];}
else
return find(k, i, j);}}

call find(k, 0, 0);

but that is linear time I need log(n)^2 time.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-17-2008, 05:41 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
Take a look at this....

Have some experiments on it,

update us,
sukatoa
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-18-2008, 06:48 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,610
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
For sorted array you can make a simple binary search as follows. I've try it and seems working fine. But you have to use a sorted array

Code:
public class BunarySearch { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int[] myArray = {1, 3, 5, 6, 8}; System.out.println("5 is found at " + binarySearch(myArray, 8)); } private static int binarySearch(int[] array, int lookFor) { int high = array.length; int low = -1; int temp; while((high - low) > 1) { temp = (high + low) >>> 1; if(array[temp] > lookFor) { high = temp; } else { low = temp; } } if(low == -1 || array[low] != lookFor) return -1; else return low; } }
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 04-18-2008, 07:09 PM
Member
 
Join Date: Apr 2008
Posts: 28
fireball2008 is on a distinguished road
Send a message via AIM to fireball2008
I know the binary search, but how to search two arrays at same time without combine the elements together.


I think I got some ideas

first pick a value in array A and note down it's index, and binary search in array B.
and some how the returning index of B + index of A = K, if this is true, then return the larger element is that right?
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 04-21-2008, 05:44 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,610
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Quote:
Originally Posted by fireball2008 View Post
I know the binary search, but how to search two arrays at same time without combine the elements together.
why don't you impossible. I mean If you know the binary search, as I do select the comparing value form one array and do the binary search with other array. Do it for the length of the first array.

Quote:
Originally Posted by fireball2008 View Post

first pick a value in array A and note down it's index, and binary search in array B.
and some how the returning index of B + index of A = K, if this is true, then return the larger element is that right?

You are adding index of two array elements, then what is variable K?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 04-22-2008, 05:21 AM
Member
 
Join Date: Apr 2008
Posts: 28
fireball2008 is on a distinguished road
Send a message via AIM to fireball2008
Quote:
Originally Posted by Eranga View Post
why don't you impossible. I mean If you know the binary search, as I do select the comparing value form one array and do the binary search with other array. Do it for the length of the first array.

but would that run in n log n runtime. each array has n elements. I need (log n)^2 runtime.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Could not find the main class, program will exit. aryubi New To Java 22 12-03-2008 12:28 AM
Sorting, Searching, and Inserting into a sorted array Java Tip java.lang 0 04-14-2008 10:39 PM
Find nth root of a number perito New To Java 1 03-03-2008 08:51 AM
Recursive Method ==> find minimum value from array NatNat New To Java 1 02-16-2008 11:10 PM
Error: Could Not Find Main Class. Program Will Exit silvia New To Java 1 07-19-2007 06:58 PM


All times are GMT +3. The time now is 05:45 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org