4/22/10
1
Clausthal
C G C C G C
Informatik II Suchen
G. Zachmann
Clausthal University, Germany zach@in.tu-clausthal.de
G. Zachmann Informatik 2 - SS 10 Suche 2
C G C C G C
Gegeben ist eine Menge von Datensätzen {A1,...,An}
Gesucht sind die Datensätze, deren Schlüssel (Key) = A[i].key
Betrachte ab jetzt o.B.d.A. nur noch die Folge der Keys, d.h., die Ai sind die Keys der Datensätze
Allgemeine Spezifikation für das Suchproblem:
Eingabe: Array A, gesuchtes Element x
Ausgabe: Index i mit A[i] = x, falls x in A
Problemstellung
4/22/10
2
G. Zachmann Informatik 2 - SS 10 Suche 3
C G C C G C
Wenn nichts über die (An-)Ordnung der Elemente in der Datenstruktur bekannt ist, kann nur die lineare Suche benutzt werden:
Besuche der Reihe nach die Elemente aus dem Container, bis ein Element mit der gewünschten Eigenschaft gefunden wurde, oder alle besucht wurden
Beispiel: suche gegebene Telefonnummer in einem Telefonbuch
Aufwand
worst case: n Schleifendurchläufe
average case:
Lineare Suche
G. Zachmann Informatik 2 - SS 10 Suche 4
C G C C G C
Nutze die totale Ordnung auf den Elementen
Annahme: die n Elemente seien nun aufsteigend sortiert (geordnet), d.h.
Beispiel: suche nach einem Namen im Telefonbuch
Lösungsstrategie: "Intervallhalbierung"
Schlage Telefonbuch in der Mitte auf, vergleiche gesuchten Namen mit einem Namen auf der Seite
Entscheide, in welcher Hälfte des Telefonbuches sich der gesuchte Name befindet (halbiert den Suchraum grob gerechnet)
Wiederhole Verfahren mit dieser Hälfte, bis richtige Seite gefunden ist
Binäre Suche
4/22/10
3
G. Zachmann Informatik 2 - SS 10 Suche 5
C G C C G
Beispiel C
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 : Index
12 17 23 24 31 32 36 37 42 47 53 55 67 67 87 89 91 91 93 Min=0, Max=18
12 17 23 24 31 32 36 37 42 Min=0, Max=8
32 36 37 42 Gesucht:
x=32
Min=5, Max=8
32 Gefunden!
G. Zachmann Informatik 2 - SS 10 Suche 6
C G C C G
Der rekursive Algorithmus … C
def binsearch_start( A, key ):
""" Return index i such that 0 <= i <= len(A) and A[i] == key, or, if no such i exists, returns -1. """
if len(A) == 0: return -1 if len(A) == 1:
if A[0] == key: return 0 else: return -1
return binsearch_work( A, key, 0, len(A)-1 ) def binsearch_work( A, key, l, r ):
""" The work horse for binsearch_start. """
m = (l + r) / 2 if key < A[m]:
return binsearch_work( A, key, l, m-1 ) if key > A[m]:
return binsearch_work( A, key, m+1, r ) return m
def binsearch_work( A, key, l, r ):
""" The work horse for binsearch_start. """
if r < l:
return -1 # k not found m = (l + r) / 2
if key < A[m]:
return binsearch_work( A, key, l, m-1 ) if key > A[m]:
return binsearch_work( A, key, m+1, r ) return m
4/22/10
4
G. Zachmann Informatik 2 - SS 10 Suche 7
C G C C G
… und nicht-rekursiv C
def binsearch( A, key ):
l = 0
r = len(A) - 1 while l <= r:
m = (l + r) / 2 if key < A[m]:
r = m - 1 elif key > A[m]:
l = m + 1 else:
return m return -1
G. Zachmann Informatik 2 - SS 10 Suche 8
C G C C G C
Annahme:
Beispiel:
Worst case: #Schleifendurchläufe =
Mittlere Anzahl Schleifendurchläufe:
Analyse
4 3
4 2
4 3
4 1
4 3
4 2
4 3
4
1.1 2.2
4.3 8.4 Summe
Schleifendurchläufe
n= 2k −1
k = log(n+ 1) ≈ �logn�