Du befindest dich hier: FSI Informatik » Prüfungsfragen und Altklausuren » Prüfungen im Bachelor-Studium (1. - 5. Semester) » aud » Forendiskussionen » codezuaufgabe6 (Übersicht)
no way to compare when less than two revisions
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen der Seite angezeigt.
— | pruefungen:bachelor:aud:loesungss16:codezuaufgabe6 [22.03.2017 17:24] (aktuell) – angelegt Danplan | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
+ | <code java> | ||
+ | public class Jul16_BinSuche { | ||
+ | public static void main(String[] args) { | ||
+ | Intervall a=new Intervall(-8, | ||
+ | Intervall b=new Intervall(42, | ||
+ | Intervall c=new Intervall(666, | ||
+ | Intervall d=new Intervall(4242, | ||
+ | |||
+ | Intervall[] test={a, | ||
+ | |||
+ | org.junit.Assert.assertNull( | ||
+ | BinaereIntervallSuche.sucheIntervall(test, | ||
+ | |||
+ | org.junit.Assert.assertNull( | ||
+ | BinaereIntervallSuche.sucheIntervall(test, | ||
+ | |||
+ | org.junit.Assert.assertTrue( | ||
+ | BinaereIntervallSuche.sucheIntervall(test, | ||
+ | |||
+ | org.junit.Assert.assertTrue( | ||
+ | BinaereIntervallSuche.sucheIntervall(test, | ||
+ | |||
+ | org.junit.Assert.assertTrue( | ||
+ | BinaereIntervallSuche.sucheIntervall(test, | ||
+ | |||
+ | org.junit.Assert.assertTrue( | ||
+ | BinaereIntervallSuche.sucheIntervall(test, | ||
+ | } | ||
+ | } | ||
+ | |||
+ | class Intervall { | ||
+ | int min, max; | ||
+ | public Intervall(int min, int max) { | ||
+ | this.min = min; | ||
+ | this.max = max; | ||
+ | } | ||
+ | public String toString(){ | ||
+ | return " | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | class BinaereIntervallSuche { | ||
+ | public static Intervall sucheIntervall(Intervall[] is, int v) { | ||
+ | int a = 0, m, e = is.length - 1; // Anfang, Mitte, Ende | ||
+ | |||
+ | while( e >= a ){ | ||
+ | m=(a+e)/ | ||
+ | |||
+ | if(v <= is[m].max | ||
+ | return is[m]; | ||
+ | |||
+ | if(v > is[m].max){ // v liegt rechts | ||
+ | a=m+1; | ||
+ | }else{ // v liegt links | ||
+ | e=m-1; | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | return null; | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </ |