数据:[1, 4, 6, 13, 14, 23, 30, 45, 58, 67, 76, 89, 99]
输入键:6
二进制搜索:
1 step index - 0 : 1, not found !
2 step index - 1 : 4, not found !
3 step index - 2 : 6, founded !!
数据:[1, 4, 6, 13, 14, 23, 30, 45, 58, 67, 76, 89, 99]
输入键:89
二进制搜索:
1 step index - 0 : 45, not found !
2 step index - 1 : 58, not found !
3 step index - 2 : 67, not found !
4 step index - 3 : 76, not found !
5 step index - 4 : 89, founded !!
Index- 1 = 4, data founded !
~~ FINISHED SEARCHED ~~
问题在于我无法逐步打印过程。帮我完成代码。
Index - 10 = 4, data founded !
~~FINISHED SEARCHED~~
这是我的源代码:
package binarysearch;
import java.util.Scanner;
import java.util.Arrays;
public class BinarySearch {
public static void main(String [] args) throws Exception{
Scanner input = new Scanner (System.in);
int[] data = {1, 4, 6, 13, 14, 23, 30, 45, 58, 67, 76, 89, 99};
boolean again = true;
String choose;
while(again == true){
System.out.println("Data : "+Arrays.toString(data));
System.out.print("Input key : ");
int find = input.nextInt();
System.out.println("");
int repeat= 0;
int start= 0;
int end= data.length - 1;
int mid;
while(start <= end){
mid= (start+ end) / 2;
if(find== data[mid]){
break;
}else if(find> data[mid]){
start= mid + 1;
}else{
end= mid- 1;
}
}
if(awal > akhir){
System.out.println("Index - "+start+" = "+find+", data not founded");
}else{
System.out.println("Index - "+start+" = "+find+", data founded !");
}
System.out.println("~~ FINISHED SEARCHED ~~");
System.out.println("");
System.out.print("DO YOU WANT EXIT ? [y] / [x] ? ");
choose= input.next();
if(choose.equals("Y") || choose.equals("y")){
again = true;
}else if(choose.equals("X") || choose.equals("x")){
again= false;
System.exit(0);
System.out.println("SEARCHING IS FINISHED, THANK YOU");
}else{
System.out.println("INVALID INPUT!!");
again= false;
System.exit(0);
}
System.out.println("");
}
}
}
答案 0 :(得分:0)
您的示例并不是二元搜索算法。二进制搜索总是在&#34;搜索区域&#34;的中间开始。请考虑阅读:http://rosettacode.org/wiki/Binary_search。
实施:
class BinarySearchRecursive {
public static int binarySearch(int[] haystack, int needle, int lo, int hi, int step) {
if (hi < lo) {
return -1;
}
int guess = (hi + lo) / 2;
if (haystack[guess] > needle) {
System.out.println("step " + step + " index: " + haystack[guess] + " not found.");
return binarySearch(haystack, needle, lo, guess - 1, ++step);
} else if (haystack[guess] < needle) {
System.out.println("step " + step + " index: " + haystack[guess] + " not found.");
return binarySearch(haystack, needle, guess + 1, hi, ++step);
}
System.out.println("step " + 1 + " index: " + haystack[guess] + " found.");
return guess;
}
public static void main(String[] args) {
int[] haystack = {1, 4, 6, 13, 14, 23, 30, 45, 58, 67, 76, 89, 99};
int needle = 6;
int index = binarySearch(haystack, needle, 0, haystack.length, 0);
if (index == -1) {
System.out.println(needle + " is not in the array");
} else {
System.out.println(needle + " is at index " + index);
}
}
}
答案 1 :(得分:0)
你的代码对我来说似乎没问题。重写了一下,以帮助您调试:
int[] data = {1, 4, 6, 13, 14, 23, 30, 45, 58, 67, 76, 89, 99};
int find = 67;
int start = 0;
int end= data.length - 1;
int mid = 0;
System.out.println("Find - "+find);
while (start <= end) {
mid = (start + end) / 2;
if(find == data[mid]) {
break;
} else if(find > data[mid]) {
start = mid + 1;
} else {
end = mid - 1;
}
System.out.println("Index - "+mid+" - val - "+data[mid]+" - not found");
}
if (data[mid] == find) {
System.out.println("Index - "+start+" - val - "+find+", data found");
} else {
System.out.println("Data not found");
}