Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

84
LS 2 / Informatik Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

description

Datenstrukturen, Algorithmen und Programmierung 2 (DAP2). Organisatorisches. Vorlesung DAP2 Dienstag 12-14 c.t. Donnerstag 14-16 c.t. Zu meiner Person Christian Sohler Fachgebiet: Komplexitätstheorie und effiziente Algorithmen Lehrstuhl 2, Informatik. Organisatorisches. Übungen - PowerPoint PPT Presentation

Transcript of Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

Page 1: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

Page 2: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

2

Organisatorisches

Vorlesung DAP2 Dienstag 12-14 c.t. Donnerstag 14-16 c.t.

Zu meiner Person Christian Sohler Fachgebiet: Komplexitätstheorie und effiziente Algorithmen Lehrstuhl 2, Informatik

Page 3: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

3

Übungen Montag: 14-16, 16-18 Dienstag: 10-12, 14-16, 16-18 Mittwoch: 12-14, 14-16, 16-18 Donnerstag: 12-14, 16-18

Zum Teil mehrere parallele Gruppen Anmeldung über AsSESS Beginn der Anmeldung: Dienstag im Laufe des Tages Anmeldeschluss: Donnerstag, 18 Uhr Änderungen der Übungsgruppe: Bis Montag 10 Uhr

Organisatorisches

Page 4: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

4

Übungen Präsenzübungen/ Heimübungen abwechselnd Abgabe Heimübung: Freitag 10 Uhr Briefkästen im Pav.6 Zulassung zur Klausur (Studienleistung Übung; Teil 1):

- Teilnahme an den Übungen - 50% der Übungspunkte

Max. 3 Personen pro Übungsblatt

Organisatorisches

Page 5: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

5

Organisatorisches

Übungen Praktikum Für Studierende des Bachelorstudiengangs verpflichtend Bachelor Elektrotechnik/Informationstechnik und Informations- und

Kommunikationstechnik hat eigenes Praktikum Termine: Mittwoch 12-14, 14-16, 16-18 Donnerstag 8-10, 10-12, 12-14 Freitag 8-10, 10-12

Anmeldung über AsSESS (ab Dienstag; Anmeldeschluss Do. 18 Uhr; Änderungen bis Montag 10 Uhr)

Page 6: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

6

Organisatorisches

Übungen Praktikum Heimübungen, Präsenzübungen Zulassung zur Klausur (Studienleistung Praktikum): 8 von insgesamt 14 Punkten bei den 7 Präsenzaufgaben 7 von insgesamt 12 Punkten bei den 6 Heimaufgaben Anwesenheit bei den Übungen

Sonstiges Schüler -> Tobias Marschall Poolräume können außerhalb der Veranstaltungszeiten immer genutzt werden Weitere Informationen auf der Webseite

http://ls11-www.cs.uni-dortmund.de/teaching/dap2praktikum

Page 7: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

7

Organisatorisches

Tests 1. Test: 18. Mai 2. Test: mal schauen Einer der beiden Test muss mit 50% der Punkte bestanden werden

(Studienleistung Übung; Teil 2)

Page 8: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

8

Organisatorisches

Bei Fragen Meine Sprechzeiten: Mo 14-15 Uhr oder nach der Vorlesung Organisatorische Fragen an Vorlesung: Melanie Schmidt ([email protected]) Christiane Lammersen ([email protected])

Praktikum: Tobias Marshall ([email protected])

Außerdem INPUD Forum

Page 9: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

9

Organisatorisches

Klausurtermine 10.8. 17-21 Uhr 21.9. 17-21 Uhr

Weitere Infos Vorlesungsseite

http://ls2-www.cs.tu-dortmund.de/lehre/sommer2010/dap2/ Oder von der Startseite des LS 2 -> Teaching -> DAP2

Page 10: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

10

Einige Hinweise/Regeln

Klausur Eine Korrelation mit den Übungsaufgaben ist zu erwarten

Laptops Sind in der Vorlesung nicht zugelassen

Page 11: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

11

Literatur

Skripte Kein Vorlesungsskript Skripte der vergangenen Jahre

Bücher Cormen, Leisserson, Rivest: Introduction to Algorithms, MIT Press Kleinberg, Tardos: Algorithm Design, Addison Wesley

WWW Kurs „Introduction to Algorithms“ am MIT. Online Material (Folien, Video und

Audio Files!) http://ocw.mit.edu/OcwWeb/Electrical-Engineering-and-Computer-Science/6-

046JFall-2005/CourseHome/

Page 12: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

12

Was ist ein Algorithmus?

Informale Definition Ein Algorithmus ist eine eindeutige Handlungsvorschrift zur Lösung von

Instanzen eines Problems in endlich vielen Schritten.

Bemerkung Es gibt viele Beschreibungen desselben Algorithmus Algorithmus ist unabhängig von der Programmiersprache!!! Die Definition wird im nächsten Semester formalisiert

Page 13: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

13

Was ist ein Algorithmus?

Beispiel Problem: Maximumsuche Instanz: Menge A={a ,…,a } von n Zahlen (als Feld A gegeben) Ausgabe: Index i der größten Zahl in A

Algorithmus-Max-Search(Array A)

1. max 1

2. for j 2 to length(A) do

3. if A[j] > A[max] then max j

4. return max

1 n

Page 14: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

14

Was ist eine Datenstruktur?

Informale Definition Eine Datenstruktur ist eine Anordnung von Daten, die die Ausführung von

Operationen (z.B. Suchen, Einfügen, Löschen) unterstützt.

Einfache Beispiele Feld sortiertes Feld Liste

Page 15: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

15

Lernziele

Bewertung von Algorithmen und Datenstrukturen- Laufzeitanalyse- Speicherbedarf- Korrektheitsbeweise

Kenntnis grundlegender Algorithmen und Datenstrukturen- Sortieren- Wörterbücher- Graphalgorithmen

Kenntnis grundlegender Entwurfsmethoden- Teile und Herrsche- gierige Algorithmen- dynamische Programmierung

Page 16: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

16

Motivation

Beispiele für algorithmische Probleme Internetsuchmaschinen Berechnung von Bahnverbindungen Optimierung von Unternehmensabläufen Datenkompression Computer Spiele Datenanalyse

Alle diese Bereiche sind (immer noch) Stoff aktueller Forschung im Bereich Datenstrukturen und Algorithmen

Page 17: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

17

Motivation

Problembeschreibung Ein m x n- Gitter heißt c-färbbar, wenn man seine Knoten mit c Farben so färben

kann, dass kein am Gitter orientiertes achsenparalleles Rechteck alle Eckknoten in derselben Farbe hat

Aufgabe: Finde eine 4-Färbung für ein 17x17 Gitter (289$ Problem)

Beispiel:(4x4 Gitter)

http://blog.computationalcomplexity.org/2009/11/17x17-challenge-worth-28900-this-is-not.html

Page 18: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

18

Motivation

Problembeschreibung Ein m x n- Gitter heißt c-färbbar, wenn man seine Knoten mit c Farben so färben

kann, dass kein am Gitter orientiertes achsenparalleles Rechteck alle Eckknoten in derselben Farbe hat

Aufgabe: Finde eine 4-Färbung für ein 17x17 Gitter (289$ Problem)

Beispiel:(4x4 Gitter)

Die vier unterlegten Knotendürfen z.B. nicht alle dieselbe Farbe haben

http://blog.computationalcomplexity.org/2009/11/17x17-challenge-worth-28900-this-is-not.html

Page 19: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

19

Motivation

Problembeschreibung Ein m x n- Gitter heißt c-färbbar, wenn man seine Knoten mit c Farben so färben

kann, dass kein am Gitter orientiertes achsenparalleles Rechteck alle Eckknoten in derselben Farbe hat

Aufgabe: Finde eine 4-Färbung für ein 17x17 Gitter (289$ Problem)

Beispiel:(4x4 Gitter)

http://blog.computationalcomplexity.org/2009/11/17x17-challenge-worth-28900-this-is-not.html

4x4 Gitter ist 4-färbbar!Geht es besser?

Page 20: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

20

Motivation

Problembeschreibung Ein m x n- Gitter heißt c-färbbar, wenn man seine Knoten mit c Farben so färben

kann, dass kein am Gitter orientiertes achsenparalleles Rechteck alle Eckknoten in derselben Farbe hat

Aufgabe: Finde eine 4-Färbung für ein 17x17 Gitter (289$ Problem)

Beispiel:(4x4 Gitter)

http://blog.computationalcomplexity.org/2009/11/17x17-challenge-worth-28900-this-is-not.html

Ja! 4x4 Gitter ist 2-färbbar!

Page 21: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

21

Motivation

17x17 Problem Es ist z.Z. nicht möglich, das 17x17 Problem mit einem Rechner zu lösen Warum ist dieses Problem so schwer zu lösen? Es gibt sehr viele Färbungen!

Fragen/Aufgaben Können wir die Laufzeit eines Algorithmus vorhersagen? Können wir bessere Algorithmen finden?

Page 22: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

22

Algorithmenentwurf

Anforderungen Korrektheit Effizienz (Laufzeit, Speicherplatz)

Entwurf umfasst1. Beschreibung des Algorithmus/der Datenstruktur

2. Korrektheitsbeweis

3. Analyse von Laufzeit und Speicherplatz

Page 23: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

23

Algorithmenentwurf

Warum mathematische Korrektheitsbeweise? Fehler können fatale Auswirkungen haben (Steuerungssoftware in

Flugzeugen, Autos, AKWs) Fehler können selten auftreten („Austesten“ funktioniert nicht)

Der teuerste algorithmische Fehler? Pentium bug (> $400 Mio.) Enormer Image Schaden Trat relativ selten auf

Page 24: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

24

Algorithmenentwurf

Warum Laufzeit/Speicherplatz optimieren? Riesige Datenmengen durch Vernetzung (Internet) Datenmengen wachsen schneller als Rechenleistung und Speicher Physikalische Grenzen Schlechte Algorithmen versagen häufig bereits bei kleinen und mittleren

Eingabegrößen

Page 25: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

25

1. Teil der Vorlesung – Grundlagen der Algorithmenanalyse

Inhalt Wie beschreibt man einen Algorithmus? Wie beweist man die Korrektheit eines Algorithmus? Rechenmodell Laufzeitanalyse

Page 26: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

26

Pseudocode

Beschreibungssprache ähnlich wie C, Java, Pascal, etc… Hauptunterschied: Wir benutzen immer die klarste und präziseste

Beschreibung Manchmal kann auch ein vollständiger Satz die beste Beschreibung sein Wir ignorieren Software Engineering Aspekte wie

- Modularität- Fehlerbehandlung

Page 27: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

27

Sortieren

Problem: Sortieren Eingabe: Folge von n Zahlen (a ,…,a ) Ausgabe: Permutation (a‘ ,…,a‘ ) von (a ,…, a ), so dass a‘ a‘ … a‘

Beispiel: Eingabe: 15, 7, 3, 18, 8, 4 Ausgabe: 3, 4, 7, 8, 15, 18

1

1

n

n

1 n 1 n2

Page 28: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

28

Insertion Sort

InsertionSort(Array A)1. for j 2 to length[A] do2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Beschreibung des Algorithmus in Pseudocode (kein C, Java, etc.)

Page 29: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

29

Insertion Sort

InsertionSort(Array A)1. for j 2 to length[A] do2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Pseudocode• Schleifen (for, while, repeat)

Beschreibung des Algorithmus in Pseudocode (kein C, Java, etc.)

Page 30: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

30

InsertionSort(Array A)1. for j 2 to length[A] do2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Pseudocode• Schleifen (for, while, repeat)

Insertion Sort

Beschreibung des Algorithmus in Pseudocode (kein C, Java, etc.)

Page 31: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

31

InsertionSort(Array A)1. for j 2 to length[A] do2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Pseudocode• Zuweisungen durch

Insertion Sort

Beschreibung des Algorithmus in Pseudocode (kein C, Java, etc.)

Page 32: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

32

InsertionSort(Array A)1. for j 2 to length[A] do2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Pseudocode• Variablen (z.B. i, j, key) sind lokal definiert

Insertion Sort

Beschreibung des Algorithmus in Pseudocode (kein C, Java, etc.)

Page 33: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

33

InsertionSort(Array A)1. for j 2 to length[A] do2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Pseudocode• Keine Typdeklaration, wenn Typ klar aus dem Kontext

Insertion Sort

Beschreibung des Algorithmus in Pseudocode (kein C, Java, etc.)

Page 34: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

34

InsertionSort(Array A)1. for j 2 to length[A] do2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Pseudocode• Zugriff auf Feldelemente mit [.]

Insertion Sort

Beschreibung des Algorithmus in Pseudocode (kein C, Java, etc.)

Page 35: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

35

InsertionSort(Array A)1. for j 2 to length[A] do2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Pseudocode• Verbunddaten sind typischerweise als Objekte organisiert• Ein Objekt besteht aus Attributen oder Ausprägungen• Beispiel: Feld wird als Objekt mit Attribut Länge betrachtet

Insertion Sort

Beschreibung des Algorithmus in Pseudocode (kein C, Java, etc.)

Page 36: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

36

InsertionSort(Array A)1. for j 2 to length[A] do2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Pseudocode• Beispiel: Objekt ist Graph G mit Knotenmenge V• Auf die Ausprägung V von Graph G wird mit V[G] zugegriffen

Insertion Sort

Beschreibung des Algorithmus in Pseudocode (kein C, Java, etc.)

Page 37: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

37

InsertionSort(Array A)1. for j 2 to length[A] do2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Pseudocode• Objekte werden als Zeiger referenziert, d.h. für alle Ausprägungen f

eines Objektes x bewirkt y x das gilt: f[y] = f[x].

Insertion Sort

Beschreibung des Algorithmus in Pseudocode (kein C, Java, etc.)

Page 38: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

38

InsertionSort(Array A)1. for j 2 to length[A] do2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Pseudocode• Blockstruktur durch Einrücken

Insertion Sort

Beschreibung des Algorithmus in Pseudocode (kein C, Java, etc.)

Page 39: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

39

InsertionSort(Array A)1. for j 2 to length[A] do2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Pseudocode• Bedingte Verzweigungen (if then else)

Insertion Sort

Beschreibung des Algorithmus in Pseudocode (kein C, Java, etc.)

Page 40: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

40

InsertionSort(Array A)1. for j 2 to length[A] do2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Pseudocode• Prozeduren „call-by-value“ ; jede aufgerufene Prozedur erhält neue Kopie der

übergebenen Variable• Die lokalen Änderungen sind nicht global sichtbar• Bei Objekten wird nur der Zeiger kopiert (lokale Änderungen am Objekt global

sichtbar)

Insertion Sort

Beschreibung des Algorithmus in Pseudocode (kein C, Java, etc.)

Page 41: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

41

InsertionSort(Array A)1. for j 2 to length[A] do2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Pseudocode• Rückgabe von Parametern durch return

Insertion Sort

Beschreibung des Algorithmus in Pseudocode (kein C, Java, etc.)

Page 42: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

42

InsertionSort(Array A)1. for j 2 to length[A] do2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Pseudocode Kommentare durch

Insertion Sort

Beschreibung des Algorithmus in Pseudocode (kein C, Java, etc.)

Page 43: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

43

InsertionSort(Array A)1. for j 2 to length[A] do2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Beispiel

Insertion Sort

8 15 3 14 7 6 18 19

Page 44: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

44

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Insertion Sort

8 15 3 14 7 6 18 19

1 nj

Page 45: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

45

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Insertion Sort

8 15 3 14 7 6 18 19

1 nj

key

Page 46: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

46

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Insertion Sort

8 15 3 14 7 6 18 19

i nj

key

Page 47: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

47

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Insertion Sort

8 15 3 14 7 6 18 19

i nj

key

Page 48: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

48

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Insertion Sort

8 15 3 14 7 6 18 19

i nj

key

Page 49: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

49

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Insertion Sort

8 15 3 14 7 6 18 19

1 nj

Page 50: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

50

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Insertion Sort

8 15 3 14 7 6 18 19

1 nj

key

Page 51: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

51

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Insertion Sort

8 15 3 14 7 6 18 19

1 nji

key=3

Page 52: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

52

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Insertion Sort

8 15 3 14 7 6 18 19

1 nj

key=3

i

Page 53: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

53

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Insertion Sort

8 15 15 14 7 6 18 19

1 nj

key=3

i

Page 54: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

54

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Insertion Sort

8 15 15 14 7 6 18 19

i nj

key=3

Page 55: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

55

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Insertion Sort

8 15 15 14 7 6 18 19

i nj

key=3

Page 56: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

56

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Insertion Sort

8 8 15 14 7 6 18 19

i nj

key=3

Page 57: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

57

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Insertion Sort

8 8 15 14 7 6 18 19

1 nj

key=3

i

Page 58: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

58

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Insertion Sort

8 8 15 14 7 6 18 19

1 nji

key=3

Page 59: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

59

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Insertion Sort

3 8 15 14 7 6 18 19

1 nj

key=3

i

Page 60: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

60

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Insertion Sort

3 8 15 14 7 6 18 19

1 nj

Sortiert

Page 61: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

61

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Insertion Sort

3 8 15 14 7 6 18 19

1 nj

Sortiert key

Page 62: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

62

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-14. while i>0 and A[i]>key do5. A[i+1] A[i]6. i i-17. A[i+1] key

Insertion Sort

3 8 15 14 7 6 18 19

1 nj

Sortiert key=14

Page 63: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

63

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-1 verschiebe alle Elemente aus4. while i>0 and A[i]>key do A[1…j-1], die größer als key 5. A[i+1] A[i] sind eine Stelle nach rechts6. i i-1 7. A[i+1] key

Insertion Sort

3 8 15 14 7 6 18 19

1 nj

Sortiert key=14

j-1

Page 64: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

64

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-1 verschiebe alle Elemente aus4. while i>0 and A[i]>key do A[1…j-1], die größer als key 5. A[i+1] A[i] sind eine Stelle nach rechts6. i i-1 7. A[i+1] key

Insertion Sort

3 8 15 14 7 6 18 19

1 nj

Sortiert key=14

j-1

Page 65: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

65

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-1 verschiebe alle Elemente aus4. while i>0 and A[i]>key do A[1…j-1], die größer als key 5. A[i+1] A[i] sind eine Stelle nach rechts6. i i-17. A[i+1] key

Insertion Sort

3 8 15 15 7 6 18 19

1 nj

key=14

j-1i

Page 66: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

66

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-1 verschiebe alle Elemente aus4. while i>0 and A[i]>key do A[1…j-1], die größer als key 5. A[i+1] A[i] sind eine Stelle nach rechts6. i i-17. A[i+1] key

Insertion Sort

3 8 15 15 7 6 18 19

1 nj

key=14

i+1i

Page 67: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

67

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-1 verschiebe alle Elemente aus4. while i>0 and A[i]>key do A[1…j-1], die größer als key 5. A[i+1] A[i] sind eine Stelle nach rechts6. i i-17. A[i+1] key Speichere key in Lücke

Insertion Sort

3 8 14 15 7 6 18 19

1 nj

key=14

i+1i

Page 68: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

68

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-1 verschiebe alle Elemente aus4. while i>0 and A[i]>key do A[1…j-1], die größer als key 5. A[i+1] A[i] sind eine Stelle nach rechts6. i i-17. A[i+1] key Speichere key in Lücke

Insertion Sort

3 8 14 15 7 6 18 19

1 nj

Sortiert

Page 69: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

69

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-1 verschiebe alle Elemente aus4. while i>0 and A[i]>key do A[1…j-1], die größer als key 5. A[i+1] A[i] sind eine Stelle nach rechts6. i i-17. A[i+1] key Speichere key in Lücke

Insertion Sort

3 8 14 15 7 6 18 19

1 nj

Sortiert key=7

Page 70: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

70

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-1 verschiebe alle Elemente aus4. while i>0 and A[i]>key do A[1…j-1], die größer als key 5. A[i+1] A[i] sind eine Stelle nach rechts6. i i-17. A[i+1] key Speichere key in Lücke

Insertion Sort

3 8 14 15 7 6 18 19

1 nj

Sortiert key=7

Page 71: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

71

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-1 verschiebe alle Elemente aus4. while i>0 and A[i]>key do A[1…j-1], die größer als key 5. A[i+1] A[i] sind eine Stelle nach rechts6. i i-17. A[i+1] key Speichere key in Lücke

Insertion Sort

3 8 8 14 15 6 18 19

1 nj

key=7

Page 72: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

72

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-1 verschiebe alle Elemente aus4. while i>0 and A[i]>key do A[1…j-1], die größer als key 5. A[i+1] A[i] sind eine Stelle nach rechts6. i i-17. A[i+1] key Speichere key in Lücke

Insertion Sort

3 8 8 14 15 6 18 19

1 nj

key=7

Page 73: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

73

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-1 verschiebe alle Elemente aus4. while i>0 and A[i]>key do A[1…j-1], die größer als key 5. A[i+1] A[i] sind eine Stelle nach rechts6. i i-17. A[i+1] key Speichere key in Lücke

Insertion Sort

3 7 8 14 15 6 18 19

1 nj

key=7

Page 74: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

74

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-1 verschiebe alle Elemente aus4. while i>0 and A[i]>key do A[1…j-1], die größer als key 5. A[i+1] A[i] sind eine Stelle nach rechts6. i i-17. A[i+1] key Speichere key in Lücke

Insertion Sort

3 7 8 14 15 6 18 19

1 nj

Sortiert

Page 75: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

75

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-1 verschiebe alle Elemente aus4. while i>0 and A[i]>key do A[1…j-1], die größer als key 5. A[i+1] A[i] sind eine Stelle nach rechts6. i i-17. A[i+1] key Speichere key in Lücke

Insertion Sort

3 7 8 14 15 6 18 19

1 nj

key=6Sortiert

Page 76: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

76

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-1 verschiebe alle Elemente aus4. while i>0 and A[i]>key do A[1…j-1], die größer als key 5. A[i+1] A[i] sind eine Stelle nach rechts6. i i-17. A[i+1] key Speichere key in Lücke

Insertion Sort

3 7 8 14 15 6 18 19

1 nj

key=6Sortiert

Page 77: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

77

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-1 verschiebe alle Elemente aus4. while i>0 and A[i]>key do A[1…j-1], die größer als key 5. A[i+1] A[i] sind eine Stelle nach rechts6. i i-17. A[i+1] key Speichere key in Lücke

Insertion Sort

3 7 7 8 14 15 18 19

1 nj

key=6

Page 78: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

78

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-1 verschiebe alle Elemente aus4. while i>0 and A[i]>key do A[1…j-1], die größer als key 5. A[i+1] A[i] sind eine Stelle nach rechts6. i i-17. A[i+1] key Speichere key in Lücke

Insertion Sort

3 7 7 8 14 15 18 19

1 nj

key=6

Page 79: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

79

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-1 verschiebe alle Elemente aus4. while i>0 and A[i]>key do A[1…j-1], die größer als key 5. A[i+1] A[i] sind eine Stelle nach rechts6. i i-17. A[i+1] key Speichere key in Lücke

Insertion Sort

3 6 7 8 14 15 18 19

1 nj

key=6

Page 80: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

80

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-1 verschiebe alle Elemente aus4. while i>0 and A[i]>key do A[1…j-1], die größer als key 5. A[i+1] A[i] sind eine Stelle nach rechts6. i i-17. A[i+1] key Speichere key in Lücke

Insertion Sort

3 6 7 8 14 15 18 19

1 nj

Sortiert

Page 81: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

81

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-1 verschiebe alle Elemente aus4. while i>0 and A[i]>key do A[1…j-1], die größer als key 5. A[i+1] A[i] sind eine Stelle nach rechts6. i i-17. A[i+1] key Speichere key in Lücke

Insertion Sort

3 6 7 8 14 15 18 19

1 nj

Page 82: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

82

InsertionSort(Array A) Eingabegröße n 1. for j 2 to length[A] do length[A] = n2. key A[j]3. i j-1 verschiebe alle Elemente aus4. while i>0 and A[i]>key do A[1…j-1], die größer als key 5. A[i+1] A[i] sind eine Stelle nach rechts6. i i-17. A[i+1] key Speichere key in Lücke

Insertion Sort

3 6 7 8 14 15 18 19

1 n

Sortiert

Page 83: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

83

InsertionSort

Wir haben beobachtet, dass Algorithmus InsertionSort korrekt sortiert Wie können wir zeigen, dass dies für jede Eingabe stimmt?

-> Korrektheitsbeweise

Page 84: Datenstrukturen, Algorithmen und Programmierung 2 (DAP2)

LS 2 / Informatik

84

Zusammenfassung

Es gibt schwierige algorithmische Probleme, die bzgl. Laufzeit und Speicherbedarf optimierte Algorithmen benötigen

Korrektheitsanalyse von Algorithmen ist notwendig Wir beschreiben Algorithmen in Pseudocode