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 08-02-2007, 07:53 AM
Member
 
Join Date: Jul 2007
Posts: 8
imran_khan is on a distinguished road
problem with recursive binary search program
Hi,
I am trying to create a recursive binary search program as follows

import java.io.*;
class SearchArray
{
int a[];
public static int binarySearch(int[] a,int ser)
{
System.out.println("Elements of the array are");
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
int len = a.length;
int beg =a[0];
int end =a[len];
int mid = (beg + end)/2;
if(a[mid] > ser)
{
for(i=0;i<mid;i++)
int k[i] = a[i];
return binarySearch(k,ser); //trying to call recursively
}
else if(a[mid] < ser)
{
for(i=0;i<=mid+1;i++)
int k[i] = a[i]; //giving error
return binarySearch(k,ser); //trying to call recursively

}
else
System.out.println(mid);
}
}
class Result
{
public static void main(String args[])
{
SearchArray ob = new SearchArray();
int a[] = {1,2,4,5,8,9,10,11,13};
int b = 5;
ob.binarySearch(a,b);
}
}

DETAILS:
1)I am passing the array a and search element b from Result class.
2)returning and calling the method binarySearch within the same class.
---->The error it is showing is as follows
C:\java_programs>javac Result.java
Result.java:19: '.class' expected
int k[i] = a[i];
^
Result.java:19: not a statement
int k[i] = a[i];
^
Result.java:25: '.class' expected
int k[i] = a[i];
^
Result.java:25: not a statement
int k[i] = a[i];
^
4 errors

can anyone pls rectify this error and tell me what conceptual mistake I am doing in this program?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-02-2007, 08:49 AM
Senior Member
 
Join Date: Jul 2007
Posts: 130
cruxblack will become famous soon enough
Err, sorry for asking bit stupid, but, whats a recursive binary search means?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-02-2007, 09:01 AM
Member
 
Join Date: Jul 2007
Posts: 8
imran_khan is on a distinguished road
binary search is an algorithm through which we can search any element present in an array(which is sorted) by using divide and conquer approach for example
we have an array
{1,3,6,7,8,9,13,18,19}
we want to search an element 7 searching element means finding its position in the array.In binary search first we divide the array
mid = (beg+end)/2
mid will give us the mid element 8.
then we compare it with search element.
for more info on binary search refer Binary search algorithm - Wikipedia, the free encyclopedia
for my query I feel that the error is related to :
as I am storing one arry to another using for loop.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-02-2007, 04:08 PM
Member
 
Join Date: Aug 2007
Posts: 6
palindrome is on a distinguished road
You have to declare the int[] k array first - you can't declare array elements as if they were variables.
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
Problem with my first Struts program....please help me sireesha Web Frameworks 4 04-04-2008 07:33 AM
binary search tranceluv New To Java 10 01-14-2008 08:13 PM
Problem with my program HelloWorld trill New To Java 1 08-05-2007 06:32 PM


All times are GMT +3. The time now is 02:29 PM.


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