Java 关于我的二进制搜索算法的反馈
我写了一个二进制搜索算法,但它似乎和我见过的其他人有点不同。我希望社区能给我一些反馈,告诉我我是否遗漏了什么,或者做了什么错事 二进制搜索:Java 关于我的二进制搜索算法的反馈,java,binary-search,Java,Binary Search,我写了一个二进制搜索算法,但它似乎和我见过的其他人有点不同。我希望社区能给我一些反馈,告诉我我是否遗漏了什么,或者做了什么错事 二进制搜索: import java.util.Arrays; public class BinarySearch { public int[] numbers = new int[] {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; /** * @param num
import java.util.Arrays;
public class BinarySearch {
public int[] numbers = new int[] {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
/**
* @param num
* @param key
*
* Function recursively searches sorted array of integers, finding the specific number (key).
* Search looks at the midpoint of array, checking to see if midpoint is number being sought,
* if not, depending of whether the sought number is greater than, or less than, the midpoint
* the function copies the upper, or lower, half of the array and passes it into a recursive
* function call.
*
*/
public int performSearch(int[] num, int key){
if(num.length == 0){
System.out.println("Array empty");
return 0;
}else{
int mid;
int number=0;
mid = (num.length)/2;
if(key == num[mid]){
number = num[mid];
System.out.println("Found the number " + number);
return number;
}else if((key < num[mid]) && num.length > 1){
num = Arrays.copyOfRange(num, 0, mid);
System.out.println("Low Range: " + Arrays.toString(num));
return performSearch(num, key);
}else if((key > num[mid]) && num.length > 1){
num = Arrays.copyOfRange(num, mid, num.length);
System.out.println("High Range: " + Arrays.toString(num));
return performSearch(num, key);
}else{
System.out.println("Number does not exist in array.");
return 0;
}
//return number;
}
}
/**
* @param args
*/
public static void main(String[] args) {
int key = 22;
BinarySearch bs = new BinarySearch();
int index = bs.performSearch(bs.numbers, key);
System.out.println("Number " + index);
}
}
导入java.util.array;
公共类二进制搜索{
公共整数[]数字=新整数[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
/**
*@param num
*@param-key
*
*函数递归搜索已排序的整数数组,查找特定的数字(键)。
*搜索查看数组的中点,检查中点是否为正在搜索的数字,
*如果不是,则取决于寻求的数字是否大于或小于中点
*该函数复制数组的上半部分或下半部分,并将其传递给递归函数
*函数调用。
*
*/
公共int性能搜索(int[]num,int key){
如果(num.length==0){
System.out.println(“数组为空”);
返回0;
}否则{
int mid;
整数=0;
mid=(num.length)/2;
如果(键==num[mid]){
数字=数字[中间];
System.out.println(“找到编号”+编号);
返回号码;
}如果((键1){
num=Arrays.copyOfRange(num,0,mid);
System.out.println(“低量程:+Arrays.toString(num));
返回performSearch(num,key);
}如果((key>num[mid])&&num.length>1,则为else{
num=Arrays.copyOfRange(num,mid,num.length);
System.out.println(“高量程:+Arrays.toString(num));
返回performSearch(num,key);
}否则{
System.out.println(“数组中不存在数字。”);
返回0;
}
//返回号码;
}
}
/**
*@param args
*/
公共静态void main(字符串[]args){
int键=22;
BinarySearch bs=新的BinarySearch();
int index=bs.performSearch(bs.number,key);
系统输出打印项次(“编号”+索引);
}
}
这是一个避免数组复制的程序
导入java.util.array
公共类二进制搜索{
public int[] numbers = new int[] {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
public int performSearch(int[] num, int key, int startIdx, int endIdx ){
if(num.length == 0){
System.out.println("Array empty");
return 0;
}else{
int mid;
int number=0;
mid = (startIdx+endIdx)/2;
int currentLength = endIdx - startIdx;
if(key == num[mid]){
number = num[mid];
System.out.println("Found the number " + number);
return number;
}else if((key < num[mid]) && currentLength > 1){
System.out.println("Low Range: " + Arrays.toString(num));
return performSearch(num, key, startIdx, mid);
}else if((key > num[mid]) && currentLength > 1){
System.out.println("High Range: " + Arrays.toString(num));
return performSearch(num, key, mid,endIdx);
}else{
System.out.println("Number does not exist in array.");
return 0;
}
//return number;
}
}
/**
* @param args
*/
public static void main(String[] args) {
int key = 10;
BinarySearch bs = new BinarySearch();
int index = bs.performSearch(bs.numbers, key,0,bs.numbers.length-1);
System.out.println("Number " + index);
}
public int[]number=新的int[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
公共int性能搜索(int[]num,int key,int startIdx,int endIdx){
如果(num.length==0){
System.out.println(“数组为空”);
返回0;
}否则{
int mid;
整数=0;
mid=(startIdx+endIdx)/2;
int currentLength=endIdx-startIdx;
如果(键==num[mid]){
数字=数字[中间];
System.out.println(“找到编号”+编号);
返回号码;
}如果((键1){
System.out.println(“低量程:+Arrays.toString(num));
返回performSearch(num、key、startIdx、mid);
}如果((key>num[mid])&¤tLength>1){
System.out.println(“高量程:+Arrays.toString(num));
返回performSearch(num、key、mid、endIdx);
}否则{
System.out.println(“数组中不存在数字。”);
返回0;
}
//返回号码;
}
}
/**
*@param args
*/
公共静态void main(字符串[]args){
int键=10;
BinarySearch bs=新的BinarySearch();
int index=bs.performSearch(bs.numbers,key,0,bs.numbers.length-1);
系统输出打印项次(“编号”+索引);
}
}如果您要返回,您不需要else语句。这不应该在代码审查中吗?@Blaine www.codereview.stackexchange.com这是您可以获得代码反馈的地方。您是否编写了一些单元测试来验证代码?哦。谢谢我来搬。