public class Sorting
{
public static void main(String[] args)
{
int j=10;
int[] arr=new int[10];
for(int i=0;i<arr.length;i++)
{
arr[i]=j;
j+=10;
}
int num=Integer.parseInt(args[0]);
bsearch(arr,0,10,num);
}
static void bsearch(int[] arr2,int first,int last,int key)
{
while(first<=last)
{
int mid=(first+last)/2;
if(key==arr2[mid])
{
System.out.println("Found the key");
}
else if(key<arr2[mid])
{
last=mid-1;
}
else if(key>arr2[mid])
{
first=mid+1;
}
else
{
System.out.println("Did not find the key");
}
}
}
}
aditya027 0 Newbie Poster
Recommended Answers
Jump to PostPlease copy and paste any error messages here.
If no errors, explain your problem.
Jump to PostYour array is size 10. You are passing in the 'last' number as 10 while your sort seems to look for size 11 (0~10). Remember that array index is 0-index, not 1-index. You need to pass in a number less than size by 1 or do your loop up to …
All 6 Replies
NormR1 563 Posting Sage Team Colleague
aditya027 0 Newbie Poster
NormR1 563 Posting Sage Team Colleague
Taywin 312 Posting Virtuoso
aditya027 0 Newbie Poster
NormR1 563 Posting Sage Team Colleague
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.