import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* @author reap.as.i.sow@gmail.com
*
*/
public class Main {
public static class BinarySearchAlgo {
List<Integer> arrays;
public BinarySearchAlgo() {
super();
}
public boolean search(Integer theValue) {
long start = System.currentTimeMillis();
if (binarySearch(0, arrays.size()-1, theValue)) {
System.out.println("binary yes");
}
else {
System.out.println("binary no");
}
System.out.println("binary elaspe : " + (System. currentTimeMillis() - start));
return false;
}
public boolean binarySearch(Integer lowIndex, Integer highIndex, Integer theValue) {
//System.out.println(lowIndex + " " + highIndex);
int theIndex = lowIndex + ((highIndex-lowIndex) / 2);
if (arrays.get(theIndex) == theValue) {
return true;
}
else {
if (lowIndex == highIndex || theIndex == lowIndex || theIndex == highIndex) return false;
}
if (arrays.get(theIndex) > theValue)
return binarySearch(lowIndex, lowIndex + (highIndex-lowIndex)/2, theValue);
else
return binarySearch(lowIndex + (highIndex-lowIndex)/2, highIndex, theValue);
}
public void addRandomInt(int num, int min, int max) {
arrays = new ArrayList<Integer>();
for (int i = 0 ; i < num ; i++) {
Double rand = min + Math.random() * (max-min);
arrays.add(rand.intValue());
}
Collections.sort(arrays);
}
public void check(Integer theValue) {
boolean found = false;
long start = System.currentTimeMillis();
for (int i = 0 ; i < arrays.size() ; i++) {
if (arrays.get(i) == theValue) {
found = true;
break;
}
}
if (found) System.out.println("yes");
else System.out.println("no");
System.out.println("elaspe : " + (System. currentTimeMillis() - start));
}
public enum evalue { BIGGER, SMALLER, EXACT };
}
public static void main(String[] args) {
BinarySearchAlgo binary = new BinarySearchAlgo();
for (int i = 0; i < 100; i++) {
binary.addRandomInt(70000, 0, 100000);
binary.search(90000);
binary.check(90000);
System.out.println("-------------------------");
}
}
}
댓글 없음:
댓글 쓰기