Informatik II Übung 09
Informatik II Übung 09
Informatik II Übung 09
Sie wollen auch ein ePaper? Erhöhen Sie die Reichweite Ihrer Titel.
YUMPU macht aus Druck-PDFs automatisch weboptimierte ePaper, die Google liebt.
Lösung U8.A1c – Binäre Suche (mit Faktor 3)<br />
0<br />
3<br />
1<br />
7<br />
2 3 4 5 6 7 8 9 10 11 12 13 14<br />
17 25 33 47 56 62 65 66 68 70 78 89 92<br />
li mi re<br />
mi = (re-li)/3 + li;<br />
Fazit: wenn ich nach kleinen Zahlen suche ist diese Methode schneller, im<br />
Durchschnitt aber schlechter (tiefer Baum)<br />
<strong>Informatik</strong> <strong>II</strong> - <strong>Übung</strong> 9<br />
3<br />
Lösung U8.A1d – BinarySearch<br />
! Sub-arrays immer zu kopieren ist keine gute Idee<br />
" lieber eine eigene Methode erstellen, die zusätzlich zwei int-Werte, begin und<br />
end als Parameter nimmt:<br />
public Value find(ArrayList haystack, Key needle)<br />
{<br />
return findRecursive(haystack, needle, 0, haystack.size());<br />
}<br />
private Value findRecursive( ArrayList haystack,<br />
Key needle, int begin, int end)<br />
{<br />
}<br />
...<br />
Eclipse DEMO<br />
<strong>Informatik</strong> <strong>II</strong> - <strong>Übung</strong> 9 4