使用二进制搜索查找元素的输出未按预期获得?
Output for finding element using binary search not getting as expected?
Given a sorted array.We need to find the element x in the array using binary search.
我的方法:
我按如下方式实现了二分查找。
检查了3种情况1)是否中间元素==x
2)元素是否小于中间element.Then,减少搜索space到end=mid-1
3)元素是否小于中间element.Then,减少查找space为start=mid+1
我使用了 start<=end 以便万一剩下一些元素或者 start==end.Then,在那种情况下只剩下一个元素。
我实现了这个但是我没有得到 ExpectedOutput
Can anyone guide me why?
@Edit
My Main Method
public static void main(String s[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of testcases");
int T=sc.nextInt();
for(int i=0;i<T;i++)
{
System.out.println("Enter the size of array");
int n=sc.nextInt();
int ar[]=new int[n];
System.out.println("Enter the elements of array");
for(int j=0;j<n;j++)
{
//System.out.println("Enter the elements of array");
ar[i]=sc.nextInt(); //the elements should be entered in sorted order
}
System.out.println("Enter the element to be searched");
int x=sc.nextInt();
int a=binarySearch(ar,x);
System.out.println("the element is at index"+"::"+a);
}
sc.close();
}
public static int binarySearch(int[] ar,int x)
{
int start=0,end=ar.length-1;
int mid=0;
while(start<=end)
{
mid=start+(end-start)/2; //To avoid Overflow
if(ar[mid]==x)
return mid;
else if(x<ar[mid])
{
end=mid-1;
}
else
{
start=mid+1;
}
}
return -1;
}
Input Element Searched ExpectedOutput MyOutput
5 8 3 -1
6
7
8
重要的是,这个算法只适用于排序数组。问题如下
在下面的代码中,您输入了 ar[i]
而不是 ar[j]
for(int j=0;j<n;j++)
{
//System.out.println("Enter the elements of array");
ar[i]=sc.nextInt(); //the elements should be entered in sorted order
}
Given a sorted array.We need to find the element x in the array using binary search.
我的方法:
我按如下方式实现了二分查找。
检查了3种情况1)是否中间元素==x
2)元素是否小于中间element.Then,减少搜索space到end=mid-1
3)元素是否小于中间element.Then,减少查找space为start=mid+1
我使用了 start<=end 以便万一剩下一些元素或者 start==end.Then,在那种情况下只剩下一个元素。
我实现了这个但是我没有得到 ExpectedOutput
Can anyone guide me why?
@Edit
My Main Method
public static void main(String s[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of testcases");
int T=sc.nextInt();
for(int i=0;i<T;i++)
{
System.out.println("Enter the size of array");
int n=sc.nextInt();
int ar[]=new int[n];
System.out.println("Enter the elements of array");
for(int j=0;j<n;j++)
{
//System.out.println("Enter the elements of array");
ar[i]=sc.nextInt(); //the elements should be entered in sorted order
}
System.out.println("Enter the element to be searched");
int x=sc.nextInt();
int a=binarySearch(ar,x);
System.out.println("the element is at index"+"::"+a);
}
sc.close();
}
public static int binarySearch(int[] ar,int x)
{
int start=0,end=ar.length-1;
int mid=0;
while(start<=end)
{
mid=start+(end-start)/2; //To avoid Overflow
if(ar[mid]==x)
return mid;
else if(x<ar[mid])
{
end=mid-1;
}
else
{
start=mid+1;
}
}
return -1;
}
Input Element Searched ExpectedOutput MyOutput
5 8 3 -1
6
7
8
重要的是,这个算法只适用于排序数组。问题如下
在下面的代码中,您输入了 ar[i]
而不是 ar[j]
for(int j=0;j<n;j++)
{
//System.out.println("Enter the elements of array");
ar[i]=sc.nextInt(); //the elements should be entered in sorted order
}