[英]Java Binary search not working
I have an insertion search in the main and from there I call the BinarySearch method to search for a number within the array that has already been sorted. 我在主目录中有一个插入搜索,然后从那里调用BinarySearch方法来搜索数组中已经排序的数字。 I try to compile it I get errors here is my main with the insertion sort.
我尝试编译它,但出现错误,这是插入排序的主要内容。
public class test5
{
public static void main(String[] args)
{
// original order or numbers
System.out.println("Original order of numbers:");
int nums[] = {26, 45, 56, 12, 78, 74, 39, 22, 5, 90, 87, 32, 28, 11, 93, 62, 79, 53, 22, 51};
for(int x = 0; x < nums.length; x++){
System.out.print(nums[x] + " ");
}
// local variables
int unsortedValue; // The first unsorted value
int scan; // used to scan the array
int swapCount = 0;
// the other loop steps the index variable through
// each subscript in the array, starting at 1. This
// is because element 0 is considered already sorted.
for(int index = 1; index < nums.length; index++)
{
// The first element outside the sorted subset is
// nums[index]. Store the value of this elementt
// in unsortedValue.
unsortedValue = nums[index];
// Start the scan at the subscript of the first element
// into its proper position within the sorted subset.
scan = index;
// Move the first element outside the sorted subset
// into its proper position within the sorted subset.
while(scan > 0 && nums[scan-1] > unsortedValue)
{
nums[scan] = nums[scan -1];
scan--;
}
// Insert the unsorted value in its proper position
// within the sorted subset.
nums[scan] = unsortedValue;
swapCount++;
}
// print out results of swap and swapCount
System.out.println();
System.out.println("Insertion sort: ");
for(int index = 0; index < nums.length; index++){
System.out.print(nums[index] + " ");
}
System.out.println();
System.out.println("The swap count is: " + swapCount);
BinarySearch(nums);
}
Here is the method for the binary search 这是二进制搜索的方法
public static int BinarySerach(String[] array, String value)
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter target: ");
int target = nums.nextInt();
int index = -1;
int left = 0;
int right = nums.length - 1;
int middle;
while(left <= right){
middle = (left + right)/2;
if(nums[middle] == target){
index = middle;
break;
} else if (nums[middle] > target) {
right = middle - 1;
}else{
left = middle + 1;
}
}
if(index == -1){
System.out.println("element not found");
}else{
System.out.println("element found at index " + index);
}
}
} }
Errors I get are cannot find symbol. 我得到的错误是找不到符号。
You have declared your nums
array locally inside your main
method, so it's inaccessible anywhere else, eg your BinarySearch
method. 您已经在
main
方法中本地声明了nums
数组,因此在其他任何地方都无法访问它,例如BinarySearch
方法。 The scope is only within the main
method. 范围仅在
main
方法内。
Declare it static
outside of all methods so it can be accessed from any method: 在所有方法之外将其声明为
static
,以便可以从任何方法进行访问:
public class test5
{
static int nums[] = {26, 45, 56, 12, 78, 74, 39, 22, 5, 90, 87, 32, 28, 11, 93, 62, 79, 53, 22, 51};
public static void main(String[] args)
{
...
Additionally, 另外,
nextInt
on an int[]
(nums). int[]
(数字)上调用nextInt
。 It looks like you meant to call it on Scanner
object: int target = keyboard.nextInt();
Scanner
对象上调用它: int target = keyboard.nextInt();
BinarySearch
- remove them. BinarySearch
任何参数-请删除它们。 Also remove passing nums
in your call to BinarySearch
. nums
在您的来电BinarySearch
。 BinarySerach
instead of BinarySearch
. BinarySerach
方法,而不是BinarySearch
。 BinarySearch
method, but you aren't assigning the return of that method to anything either; BinarySearch
方法返回任何东西,但是也没有将该方法的返回赋值给任何东西。 just make the return type void
. void
。 import java.io.*;
class BinarySearch{
public static void main(String args[])throws IOException{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader (isr);
int a[]={7, 10, 12, 34, 45,51, 60, 78, 81, 92};
int key, low=0, high=a.length-1, mid=0;
System.out.print("Enter the integer to search:"); key=Integer.parseInt(br.readLine());
while(low<=high){
mid=(low+high)/2;
if (key==a[mid])
break;
else if(key<a[mid])
high=mid-1;
else
low=mid+1;
}
if(low<=high)
System.out.println(key+" found at index "+mid);
else
System.out.println(key+" not found");
}
}``
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.