Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
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 01-10-2008, 09:12 PM
Member
 
Join Date: Jan 2008
Posts: 5
tranceluv is on a distinguished road
binary search
hey i got confused with this and maybe someone can help me...

i was experimenting about sorting a randomly generated array of 100 integers with binary search.. however i have some trouble..

heres d code:

class iamangry
{

public int binarySearch(int arr[], int key, int n)
{
int low = 0;
int ri = (arr[n]+ 1);
int mid;

do{ mid = ((low + ri)/2);
if(key < arr[mid]){
ri = (mid - 1);
}
else low = mid + 1;
}
while (key == arr[mid]);
if(low < ri){
return -1;
}
else return mid;
}



public static void main(String[] args){
int x = 100;
int[]arr = binarySearch[x];
for(int n = 0; n < a.length; n++){
arr[n] = 1 + (int)(Math.random() *100);
}
for(int t=1; t<=100; t++){
System.out.print(""+arr[t]+" ");
}
}
}



the problem seems to be actually in: int[]arr = binarySearch[x]; ...

any help please? thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-11-2008, 09:29 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 769
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
One thing you need to understand about Binary Search, is that it is a search function for some value or object, thus your array is assumed to be already sorted. You're looking for some value within in the array and trying to discover its location.

I altered your code to form the below, although not thoroughly tested.. I hope you get the idea.
Code:
public class BinarySearch { public int binarySearch(int arr[], int key, int size) { int low = 0; int mid = 0; int high = size; while (low <= high) { mid = ((low + high) / 2); if (key < arr[mid]) { high = (mid - 1); } else { low = mid + 1; } } if (low < high) { return -1; } else { return mid; } } public static void main(String[] args){ int SIZE = 20; int[] arr = new int[SIZE]; int key = 5; for (int i = 0, k = 0; i < SIZE; i++, k++) { arr[k] = i; //(1 + (int)(Math.random() * 100)); } BinarySearch bs = new BinarySearch(); for (int j = 0; j <= arr.length - 1; j++) { System.out.println(arr[j]); } System.out.println("Array size: " + arr.length); System.out.println("Value found at index " + bs.binarySearch(arr, key, SIZE)); } }
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on July 27, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-11-2008, 11:16 PM
Member
 
Join Date: Jan 2008
Posts: 5
tranceluv is on a distinguished road
ah ok ic..

so how can i sort an array of randomly generated numbers?

cheers
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-11-2008, 11:20 PM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 769
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Quote:
Originally Posted by tranceluv View Post
ah ok ic..

so how can i sort an array of randomly generated numbers?

cheers
Check the API for an ArrayList, fill it with your random values... upon which I believe you have to use Collections.sort() to sort. Someone will correct me if I'm wrong.

Best of luck!
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on July 27, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-11-2008, 11:33 PM
Member
 
Join Date: Jan 2008
Posts: 24
afsina is on a distinguished road
i am assuming you are doing binary search code because you want to practice. Because obviously Arrays.binarySearch(...) does the thing.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-12-2008, 10:32 PM
Member
 
Join Date: Jan 2008
Posts: 5
tranceluv is on a distinguished road
Quote:
Originally Posted by afsina View Post
i am assuming you are doing binary search code because you want to practice. Because obviously Arrays.binarySearch(...) does the thing.
can you help me out using Arrays.binarySearch(...) pls? tks
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-12-2008, 11:08 PM
Member
 
Join Date: Jan 2008
Posts: 24
afsina is on a distinguished road
well, did you read the java doc?
there is also a handy "sort" method there. first show me what do you have, then we can analyze the problem.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 01-12-2008, 11:13 PM
Member
 
Join Date: Jan 2008
Posts: 24
afsina is on a distinguished road
By the way, leanring the internals of binary search algorithm is a very important thing, i strongly suggest understanding the way it works first (this is one of the favorite questions asked in job interviews even maybe by Google). But if you are developing an application that requires binary search, by all means use the built in Facilities in Java, like Arrays, Collections etc.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 01-13-2008, 03:13 PM
Member
 
Join Date: Jan 2008
Posts: 5
tranceluv is on a distinguished road
Quote:
Originally Posted by afsina View Post
well, did you read the java doc?
there is also a handy "sort" method there. first show me what do you have, then we can analyze the problem.
well i thought that this method could sort the random-number array

public int binarySearch(int arr[], int key, int size) {
int low = 0;
int mid = 0;
int high = size;

while (low <= high) {
mid = ((low + high) / 2);

if (key < arr[mid]) {
high = (mid - 1);
} else {
low = mid + 1;
}
}

if (low < high) {
return -1;
} else {
return mid;
}
}

actually i'm just starting to learn what sorting is and binary search seems one of the most efficient
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 01-13-2008, 05:10 PM
Member
 
Join Date: Jan 2008
Posts: 24
afsina is on a distinguished road
No, binary search is used for finding an element in an already sorted array quickly. For example if you have a sorted integer array like this:
a[]={1,2,4,5,6,7,9,10,11,12,14,16,19,21,22}
binary search can find the position of "19" in only three steps. However, as you see the array is already sorted.

there are several sorting algorithms, if you want to learn ebout them, you can start with easy ones, such as selection sort, bubble sort or insertion sort. there are more complicated sort algorithms also such as quick sort, merge sort, radix sort or heap sort.

Java platform uses a modified merge sort, and qick sort for its sort facilities. There are Arrays.sort() for arrays, and Collections.sort() for Lists available. But i got that you are in the learning phase, not sur eif you want to use them.

Make a search in wikipedia about search algorithms or binary search, you can see several different implementations in various languages in the links area. Or Google it. For binary search or sort, you can also check how java is doing it by checking the source.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 01-14-2008, 08:13 PM
Member
 
Join Date: Jan 2008
Posts: 5
tranceluv is on a distinguished road
Quote:
Originally Posted by afsina View Post
No, binary search is used for finding an element in an already sorted array quickly. For example if you have a sorted integer array like this:
a[]={1,2,4,5,6,7,9,10,11,12,14,16,19,21,22}
binary search can find the position of "19" in only three steps. However, as you see the array is already sorted.

there are several sorting algorithms, if you want to learn ebout them, you can start with easy ones, such as selection sort, bubble sort or insertion sort. there are more complicated sort algorithms also such as quick sort, merge sort, radix sort or heap sort.

Java platform uses a modified merge sort, and qick sort for its sort facilities. There are Arrays.sort() for arrays, and Collections.sort() for Lists available. But i got that you are in the learning phase, not sur eif you want to use them.

Make a search in wikipedia about search algorithms or binary search, you can see several different implementations in various languages in the links area. Or Google it. For binary search or sort, you can also check how java is doing it by checking the source.
hey thanks alot i managed it through bubble sort alghorithm


thanks people
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
Can anybody help with cuncurrent binary search tree guys) danylo Threads and Synchronization 1 04-23-2008 07:22 PM
Binary Search in Java Java Tip Algorithms 0 04-15-2008 08:43 PM
Use recursion to convert binary to... coco Advanced Java 1 08-07-2007 08:46 AM
problem with recursive binary search program imran_khan New To Java 3 08-02-2007 04:08 PM
Binbot Binary Newsreader 1.1b3 levent Java Announcements 0 07-27-2007 02:18 PM


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


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