105 lines
3.8 KiB
Java
105 lines
3.8 KiB
Java
package Uebungen.Uebung5;
|
|
|
|
import java.util.Date;
|
|
import java.util.Arrays;
|
|
public class Aufgabe4 {
|
|
|
|
public static void main(String[] args) {
|
|
int timeout = 150; // milliseconds
|
|
int[] testValues = {2,34,134,4,53,11,87,142,742,115,00,411,61,33,23456,2346,12,4562,4562,56245,62456,2456,24562};
|
|
Arrays.sort(testValues);
|
|
for (int value:testValues){
|
|
int result = binarySearch(testValues,value, timeout);
|
|
System.out.println("Found: "+testValues[result]+" at index " + result);
|
|
}
|
|
}
|
|
public static int binarySearch(int[] inputArray, int value,int timeout){
|
|
System.out.println("Searching for " + value);
|
|
printArray(inputArray);
|
|
int res=-1;
|
|
int rangeMax=inputArray.length-1, rangeMin=0,center=0;
|
|
int diff;
|
|
long lastdate=0;
|
|
while(res < 0){
|
|
Date date = new Date();
|
|
long deltaT = date.getTime()-lastdate;
|
|
if(deltaT>timeout) {
|
|
lastdate = date.getTime();
|
|
diff = (rangeMax - rangeMin) / 2; // intermediate value
|
|
if (diff==0) {
|
|
if (value == inputArray[center]) {
|
|
res = center;
|
|
}
|
|
else {
|
|
res = center+1;
|
|
center +=1; // not needed
|
|
}
|
|
}
|
|
else {
|
|
center = rangeMin + diff;
|
|
}
|
|
|
|
infoGraphic(value, center, rangeMin, rangeMax, rangeMax, inputArray);
|
|
if (value >= inputArray[center]) {
|
|
rangeMin = center;
|
|
} else {
|
|
rangeMax = center;
|
|
}
|
|
}
|
|
}
|
|
return res;
|
|
}
|
|
public static void printArray(int[] inputArray){
|
|
int maxlength=maxLength(inputArray);
|
|
System.out.print("I|");
|
|
for(int i=0; i<inputArray.length;i++){
|
|
System.out.print(fixedLengthString(Integer.toString(i), maxlength));
|
|
}
|
|
System.out.println();
|
|
System.out.print("V|");
|
|
for(int i=0; i<inputArray.length;i++){
|
|
System.out.print(fixedLengthString(Integer.toString(inputArray[i]), maxlength));
|
|
}
|
|
System.out.println();
|
|
}
|
|
public static void infoGraphic(int value, int center, int min, int max, int modulo, int[] array){
|
|
int maxlength=maxLength(array);
|
|
System.out.print("P|");
|
|
for(int i=0; i<array.length;i++){
|
|
if(i==max&i==center)
|
|
System.out.print(fixedLengthString("C,<", maxlength, false));
|
|
else if(i==min&i==center)
|
|
System.out.print(fixedLengthString(">,C", maxlength, false));
|
|
else if(i==center)
|
|
System.out.print(fixedLengthString("C", maxlength, false));
|
|
else if(i==max)
|
|
System.out.print(fixedLengthString("<", maxlength, false));
|
|
else if(i==min)
|
|
System.out.print(fixedLengthString(">", maxlength, false));
|
|
else
|
|
System.out.print(fixedLengthString(" ", maxlength, false));
|
|
}
|
|
System.out.println("center: "+center+" MOD: "+modulo+" min: "+min+" max:"+max);
|
|
}
|
|
public static String fixedLengthString(String string, int length){
|
|
return fixedLengthString( string, length, true);
|
|
}
|
|
|
|
public static String fixedLengthString(String string, int length,boolean brackets) {
|
|
if(brackets) return String.format("%1$"+length+ "s|", string);
|
|
else return String.format("%1$"+(length+1)+ "s", string);
|
|
|
|
}
|
|
public static int maxLength(int[] inputArray){
|
|
int res=0;
|
|
for(int number:inputArray){
|
|
int length = (int) (Math.log10(number) + 1);
|
|
if(length>res)
|
|
res=length;
|
|
}
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|