completed task 4
This commit is contained in:
		
							parent
							
								
									e1ea1d2e75
								
							
						
					
					
						commit
						519bc165dd
					
				
							
								
								
									
										98
									
								
								GDP_Gesamt/src/Uebungen/Uebung5/Aufgabe4.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										98
									
								
								GDP_Gesamt/src/Uebungen/Uebung5/Aufgabe4.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,98 @@ | ||||
| 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,6234562,4562,4562,56245,62456,2456,24562,1,1345,13451,435,1345,1345,134,51345,134513,451,345}; | ||||
|         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,modulo; | ||||
|         long lastdate=0; | ||||
|         while(true){ | ||||
|             Date date = new Date(); | ||||
|             long deltaT = date.getTime()-lastdate; | ||||
|             if(deltaT>timeout) { | ||||
|                 lastdate = date.getTime(); | ||||
|                 modulo = (rangeMax - rangeMin) % 2; | ||||
|                 center = rangeMin + (rangeMax - rangeMin) / 2 + modulo; | ||||
|                 infoGraphic( value, center,  rangeMin,  rangeMax, modulo, inputArray); | ||||
|                 if (value == inputArray[center]) { | ||||
|                     res = center; | ||||
|                     break; | ||||
|                 } | ||||
|                 else if (value == rangeMax){ | ||||
|                     res = rangeMax; | ||||
|                     break; | ||||
|                 } | ||||
|                 else if (value == rangeMin){ | ||||
|                     res = rangeMin; | ||||
|                     break; | ||||
|                 } | ||||
|                 else if (value < inputArray[center]) | ||||
|                     rangeMax = center; | ||||
|                 else if (value > inputArray[center]) | ||||
|                     rangeMin = 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==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(" MOD: "+modulo); | ||||
|     } | ||||
|     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; | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user