Java二分法查找数组元素

时间:2021-07-30 00:33:22

Array类:

package chapter5;

import java.util.Random;

public class Array {
public static int[] generateArray() {
System.out.println("generate Array... ");
int[] group500;
group500 = new int[10];
System.out.println("generate Array successful... ");
System.out.println("the Array length is: " + group500.length);
return group500;
}

public static int[] generateArray(int length) {
System.out.println("generate Array... ");
int[] group500;
group500 = new int[length];
System.out.println("generate Array successful... ");
System.out.println("the Array length is: " + group500.length);
return group500;
}

public static int[] setSortDate(int[] array) {
System.out.println("the Array is:");
for (int i = 1; i <= array.length; i++) {
array[i - 1] = i;
System.out.print(array[i] + " ");
}
return array;
}
public static int[] setSortDate(int[] array, int[] arrayData) {
System.out.println("the Array is:");
for (int i = 0; i < array.length; i++) {
array[i] = arrayData[i];
System.out.print(array[i] + " ");
}
return array;
}

public static int[] setRadomDate(int[] array) {
System.out.println("the Array is:");
for (int i = 0; i < array.length; i++) {
array[i] = new Random().nextInt(array.length) + 1;
for (int j = 0; j < i + 1; j++) {
if ((i != j) && array[i] == array[j])
array[i] = new Random().nextInt(array.length) + 1;
}
}
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
return array;
}

public static void sort(int[] array) {
System.out.println();
System.out.println("now we will sort it as 1~9:");

for (int i = 0; i < array.length; i++) {
// int k;
int tmp;
// k = i;
for (int j = i + 1; j < array.length; j++) {
if (array[i] > array[j]) {
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}

}
System.out.println("now the result is: ");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}

public static String search(int[] array, int beSearchedNum) {
// TODO Auto-generated method stub
System.out.println();
System.out.println(beSearchedNum + " wanted be search!");
System.out.println("now we will search its index");
int min = 0;
int max = array.length - 1;
int mid = (int) ((min + max) / 2);
int searchTimes = 0;
int searchTimesMax = array.length;
String result = null;
String sorry = "Sorry, the system can't find ur Num!";
if (array[min] <= beSearchedNum && array[max] >= beSearchedNum) {

while (min <= max) {
if (array[mid] == beSearchedNum) {
result = "It is: " + mid;
return result;
} else if (array[mid] > beSearchedNum) {
searchTimes++;
max--;
} else if (array[mid] < beSearchedNum) {
searchTimes++;
max++;
}
mid = (int) ((min + max) / 2);
if (searchTimes == searchTimesMax) {
result = sorry;
break;
}

}
} else {
result = sorry;

}
return result;
}
}

// 如果不使用集合,请仿照下面的写法
class ProduceRandom {
public static void main(String[] args) {
Integer[] intArray = getRandomArray(100, 200, 10);
for (Integer i : intArray) {
System.out.print(i + " ");
}
}

/**
* @param MIN
* :下界
* @param MAX
* :上界
* @param SUM
* :总个数
* @return:不同整数的整型数组
*/
public static Integer[] getRandomArray(final int MIN, final int MAX,
final int SUM) {
Integer[] intArray = new Integer[SUM];
for (int counter = 0; counter < 10; counter++) { // 记录已经产生的随机数个数
Integer temp = (int) (Math.random() * (MAX - MIN)) + MIN;// 假设只产生整数,不产生浮点数等其他类型的数
int index = temp % SUM;// 因为没有使用集合,为了提高匹配效率,仿真哈希算法,简单的产生下标
while (intArray[index] != null) {
if (intArray[index] == temp)
break;// 值相同时也复制到那个堆内存,相当于什么都没做
index = (index + 1) % SUM;
}
intArray[index] = temp;
}
return intArray;
}
}




main方法类:

package chapter5;

public class Dichotomy {
public static void main(String[] args) {

int[] array = Array.generateArray(10);
array = Array.setRadomDate(array);
Array.sort(array);
String result = Array.search(array, 3);
System.out.println(result);
}
}

运行结果:

generate Array... 
generate Array successful...
the Array length is: 10
the Array is:
2 6 8 1 3 4 4 1 9 5
now we will sort it as 1~9:
now the result is:
1 1 2 3 4 4 5 6 8 9
3 wanted be search!
now we will search its index
It is: 3

没查到的运行结果:

package chapter5;

public class Dichotomy {
public static void main(String[] args) {

int[] array = Array.generateArray(10);
array = new int[] { 2, 3, 3, 5, 5, 6, 7, 8, 9, 10 };
array = Array.setSortDate(array, array);
Array.sort(array);
String result = Array.search(array, 4);
System.out.println(result);
}
}


generate Array... 
generate Array successful...
the Array length is: 10

now we will sort it as 1~9:
now the result is:
2 3 3 5 5 6 7 8 9 10
4 wanted be search!
now we will search its index
Sorry, the system can't find ur Num!