탐색할 리스트를 반으로 나누어 찾는 데이터가 있을만한 곳을 찾아낸다.
package algorithms.search;
import java.util.ArrayList;
import java.util.Collections;
public class BinarySearch {
public boolean searchFunc(ArrayList<Integer> dataList, Integer searchItem) {
if (dataList.size() == 1 && dataList.get(0) == searchItem) {
return true;
}
if (dataList.size() == 1 && dataList.get(0) != searchItem) {
return false;
}
if (dataList.size() == 0) {
return false;
}
Integer mid = dataList.size() / 2;
if (dataList.get(mid) == searchItem) {
return true;
} else {
if (dataList.get(mid) > searchItem) {
return this.searchFunc(new ArrayList<>(dataList.subList(0, mid)), searchItem);
} else {
return this.searchFunc(new ArrayList<>(dataList.subList(mid, dataList.size())), searchItem);
}
}
}
public static void main(String[] args) {
ArrayList<Integer> testData = new ArrayList<>();
for (int i = 0; i < 100; i++) {
testData.add((int) (Math.random() * 100));
}
Collections.sort(testData);
System.out.println("testData");
System.out.println(testData);
BinarySearch binarySearch = new BinarySearch();
System.out.println(binarySearch.searchFunc(testData, 2));
}
}