当初使用C语言没有成功的查找算法,今天用java实现一下
代码如下:
package com.li;
import java.util.*;
public class MiddleSort {
public static void main(String[] args) {
// TODO 自动生成的方法存根
@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
int[] number = new int[99999999];
for(int i=0; i<99999999; i++)
{
number[i] = i;
}
int searchNumber = 0; //需要寻找的数字
int findNumber = 0;
while(true)
{
System.out.println("Please input a number");
searchNumber = in.nextInt();
int start = 0;
int end = 99999999;
int middle = (start + end) / 2;
while(true)
{
if(start > end)
{
System.out.println("没有找到这个数字!");
break;
}
else if(searchNumber == number[middle])
{
findNumber = number[middle];
System.out.println(findNumber);
break;
}
else if(searchNumber < number[middle])
{
end = middle - 1;
}
else
{
start = middle + 1;
}
middle = (start + end) / 2;
}
}
}
}
折中查找法只是适用于已经按某种顺序排好的东西