这是从优快云的论坛上看到的一道题目,要求使用二分法从一个数组中查询出某特定数字是否存在,若存在,输出其位置。这是使用的非递归的方法实现的。
源码:
package
com.zhaozy.sort;
import
java.util.Scanner;
/**
*
给定一个数组和一个特定的整数,找出整数在数组中的位置
*
*
@author
zhaozy
*
*/
public
class
BinarySearch {
public
static
void
main(String[] args) {
int
[] dataset =
new
int
[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Scanner s =
new
Scanner(System.
in
);
System.
out
.println(
"
请输入要查询的整数:
"
);
int
num = s.nextInt();
int
midIndex = findNum
(dataset, num);
if
(midIndex == -1) {
System.
out
.println(
"
此数不存在!
"
);
}
else
{
System.
out
.println(
"
位置为:
"
+ midIndex);
}
}
public
static
int
findNum(
int
[] dataset,
int
data) {
int
beginIndex = 0;
int
endIndex = dataset.
length
- 1;
int
midIndex = -1;
if
(data < dataset[beginIndex] || data > dataset[endIndex]
|| beginIndex > endIndex) {
return
-1;
}
while
(beginIndex <= endIndex) {
midIndex = (beginIndex + endIndex) / 2;
System.
out
.println(midIndex);
if
(data > dataset[midIndex]) {
beginIndex = midIndex + 1;
}
else
if
(data < dataset[midIndex]) {
endIndex = midIndex - 1;
}
else
{
return
midIndex;
}
}
return
-1;
}
}
整个程序都是使用的数组实现的,说实话,自己以前一直很看不起数组,以为他的使用范围很局限,不像集合那样的灵活,但我发现,我错了,(⊙o⊙)
还有一种是使用递归实现的,网上一直有人说递归的使用降低了代码的执行效率,以为自己没有太多深入的研究,所以两种方法都写上,以后好好研究一下。
使用递归的实现方法:
源码:
package
com.zhaozy.sort;
import
java.util.Scanner;
/**
*
使用递归
*
*
@author
zhaozy
*
*/
public
class
BinarySearch2 {
public
static
void
main(String[] args) {
int
[] dataset =
new
int
[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Scanner scanner =
new
Scanner(System.
in
);
System.
out
.println(
"
请输入要查找的数据:
"
);
int
data = scanner.nextInt();
BinarySearch2 search =
new
BinarySearch2();
int
position = search.search(dataset, data, 0, dataset.
length
- 1);
if
(position == -1) {
System.
out
.println(
"
当前数组中没有此数据!
"
);
}
else
{
System.
out
.println(
"
在数组中的位置为:
"
+ position);
}
}
/**
*
*
@param
dataset
*
@param
data
*
@param
beginIndex
*
@param
endIndex
*
@return
要找的值在数组中的位置
*/
public
int
search(
int
[] dataset,
int
data,
int
beginIndex,
int
endIndex) {
//
取出中间的数据,作为标准进行判断
int
midIndex = (beginIndex + endIndex) / 2;
//
如果中间的数据大于数组的最大值
||
中间的数据小于数组的最小值
||beginIndex>endIndex
直接返回没有找到的信息
if
(data > dataset[endIndex] || data < dataset[beginIndex]
|| beginIndex > endIndex) {
return
-1;
}
if
(data < dataset[midIndex]) {
return
this
.search(dataset, data, beginIndex, midIndex - 1);
}
else
if
(data > dataset[midIndex]) {
return
this
.search(dataset, data, midIndex + 1, endIndex);
}
else
{
return
midIndex;
}
}
}
这是从优快云的论坛上看到的一道题目,要求使用二分法从一个数组中查询出某特定数字是否存在,若存在,输出其位置。
源码:
package
com.zhaozy.sort;
import
java.util.Scanner;
/**
*
给定一个数组和一个特定的整数,找出整数在数组中的位置
*
*
@author
zhaozy
*
*/
public
class
BinarySearch {
public
static
void
main(String[] args) {
int
[] dataset =
new
int
[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Scanner s =
new
Scanner(System.
in
);
System.
out
.println(
"
请输入要查询的整数:
"
);
int
num = s.nextInt();
int
midIndex = findNum
(dataset, num);
if
(midIndex == -1) {
System.
out
.println(
"
此数不存在!
"
);
}
else
{
System.
out
.println(
"
位置为:
"
+ midIndex);
}
}
public
static
int
findNum(
int
[] dataset,
int
data) {
int
beginIndex = 0;
int
endIndex = dataset.
length
- 1;
int
midIndex = -1;
if
(data < dataset[beginIndex] || data > dataset[endIndex]
|| beginIndex > endIndex) {
return
-1;
}
while
(beginIndex <= endIndex) {
midIndex = (beginIndex + endIndex) / 2;
System.
out
.println(midIndex);
if
(data > dataset[midIndex]) {
beginIndex = midIndex + 1;
}
else
if
(data < dataset[midIndex]) {
endIndex = midIndex - 1;
}
else
{
return
midIndex;
}
}
return
-1;
}
}
整个程序都是使用的数组实现的,说实话,自己以前一直很看不起数组,以为他的使用范围很局限,不像集合那样的灵活,但我发现,我错了,(⊙o⊙)