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,15);
 + Intervall b=new Intervall(42,47);
 + Intervall c=new Intervall(666,999);
 + Intervall d=new Intervall(4242,4711);
 +
 + Intervall[] test={a,b,c,d};
 +
 + org.junit.Assert.assertNull(
 + BinaereIntervallSuche.sucheIntervall(test,41));
 +
 + org.junit.Assert.assertNull(
 + BinaereIntervallSuche.sucheIntervall(test,4712));
 +
 + org.junit.Assert.assertTrue(
 + BinaereIntervallSuche.sucheIntervall(test,44).equals(b));
 +
 + org.junit.Assert.assertTrue(
 + BinaereIntervallSuche.sucheIntervall(test,666).equals(c));
 +
 + org.junit.Assert.assertTrue(
 + BinaereIntervallSuche.sucheIntervall(test,-7).equals(a));
 +
 + org.junit.Assert.assertTrue(
 + BinaereIntervallSuche.sucheIntervall(test,4711).equals(d));
 + }
 +}
 +
 +class Intervall {
 + int min, max;
 + public Intervall(int min, int max) {
 + this.min = min;
 + this.max = max;
 + }
 + public String toString(){
 + return "min("+min+") max("+max+")";
 + }
 +
 +}
 +
 +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)/2;
 +
 + if(v <= is[m].max  && v >= is[m].min) // volltreffer
 + return is[m];
 +
 + if(v > is[m].max){ // v liegt rechts
 + a=m+1;
 + }else{ // v liegt links
 + e=m-1;
 + }
 +
 + }
 +
 + return null;
 + }
 +
 +}
 +</code>