• Keine Ergebnisse gefunden

VL-06: Sortieren I (Datenstrukturen und Algorithmen, SS 2017) Janosch Fuchs

N/A
N/A
Protected

Academic year: 2022

Aktie "VL-06: Sortieren I (Datenstrukturen und Algorithmen, SS 2017) Janosch Fuchs"

Copied!
127
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

VL-06: Sortieren I

(Datenstrukturen und Algorithmen, SS 2017) Janosch Fuchs

SS 2017, RWTH

(2)

Organisatorisches

• Vorlesung: Gerhard Woeginger (Zimmer 4024 im E1) Sprechstunde: Mittwoch 11:15–12:00

• Übungen: Tim Hartmann, David Korzeniewski, Björn Tauer Email: dsal-i1@algo.rwth-aachen.de

• Webseite:http://algo.rwth-aachen.de/Lehre/SS17/DSA.php

DSAL/SS 2017 VL-06: Sortieren I 2/34

(3)

Sortieren I

• Einführung

• Sortieren durch Einfügen (InsertionSort)

• Divide-and-Conquer und MergeSort

• Geht es noch effizienter?

(4)

Einführung ins Sortieren

DSAL/SS 2017 VL-06: Sortieren I 4/34

(5)

Sortieren

Donald Knuth (The Art of Computer Programming, Volume 3) Computer manufacturers of the 1960s estimated that more than 25 percent of the running time on their computers was spent on sorting, when all their customers were taken into account.

In fact, there were many installations in which the task of sorting was responsible for more than half of the computing time.

Sortieren ist wichtig

Sortieren hat viele Anwendungen.

Geniale Algorithmen Grundlegende Ideen

(6)

Sortieren: Anwendungen

Beispiel (Suchen)

Schnellere Suche=häufigste Anwendung des Sortierens.

Binäre Suche findet ein Element in O(logn).

Beispiel (Closest pair)

Gegeben seiennZahlen. Finde Zahlenpaar mit geringstem Abstand. Nach dem Sortieren liegen die beiden Zahlen nebeneinander. Der Aufwand ist dann nochO(n).

Beispiel (Eigenschaften von Datenobjekten)

Sind alle nElemente einzigartig oder gibt es Duplikate? Welches Element kommt am häufigsten vor?

Was ist dask-t grösste Element einer Menge?

DSAL/SS 2017 VL-06: Sortieren I 6/34

(7)

Sortieren: Anwendungen

Beispiel (Suchen)

Schnellere Suche=häufigste Anwendung des Sortierens.

Binäre Suche findet ein Element in O(logn).

Beispiel (Closest pair)

Gegeben seiennZahlen. Finde Zahlenpaar mit geringstem Abstand.

Nach dem Sortieren liegen die beiden Zahlen nebeneinander.

Der Aufwand ist dann nochO(n).

Beispiel (Eigenschaften von Datenobjekten)

Sind alle nElemente einzigartig oder gibt es Duplikate? Welches Element kommt am häufigsten vor?

Was ist dask-t grösste Element einer Menge?

(8)

Sortieren: Anwendungen

Beispiel (Suchen)

Schnellere Suche=häufigste Anwendung des Sortierens.

Binäre Suche findet ein Element in O(logn).

Beispiel (Closest pair)

Gegeben seiennZahlen. Finde Zahlenpaar mit geringstem Abstand.

Nach dem Sortieren liegen die beiden Zahlen nebeneinander.

Der Aufwand ist dann nochO(n).

Beispiel (Eigenschaften von Datenobjekten)

Sind alle nElemente einzigartig oder gibt es Duplikate?

Welches Element kommt am häufigsten vor?

Was ist dask-t grösste Element einer Menge?

DSAL/SS 2017 VL-06: Sortieren I 6/34

(9)

Sortieren ist nicht trivial!

TimSort in der Wikipedia

Timsort is a hybrid stable sorting algorithm, derived from merge sort and insertion sort, designed to perform well on many kinds of real-world data.

It uses techniques from Peter McIlroy’s “Optimistic Sorting and

Information Theoretic Complexity” It was implemented by Tim Peters in 2002 for use in the Python programming language. It is used in Java (OpenJDK + Oracle) and Android.

(10)

Sortieren ist nicht trivial!

OpenJDK’s java.utils.Collection.sort() is broken:

The good, the bad and the worst case?

Stijn de Gouw1,2, Jurriaan Rot3,1, Frank S. de Boer1,3, Richard Bubel4, and Reiner H¨ahnle4

1CWI, Amsterdam, The Netherlands

2SDL, Amsterdam, The Netherlands

3Leiden University, The Netherlands

4 Technische Universit¨at Darmstadt, Germany

Abstract.We investigate the correctness of TimSort, which is the main sorting algorithm provided by the Java standard library. The goal is functional verification with mechanical proofs. During our verification attempt we discovered a bug which causes the implementation to crash.

We characterize the conditions under which the bug occurs, and from this we derive a bug-free version that does not compromise the performance.

We formally specify the new version and mechanically verify the absence of this bug with KeY, a state-of-the-art verification tool for Java.

1 Introduction

Some of the arguments often invoked against the usage of formal software veri- fication include the following: it is expensive, it is not worthwhile (compared to its cost), it is less e↵ective than bug finding (e.g., by testing, static analysis, or model checking), it does not work for “real” software. In this article we evaluate these arguments in terms of a case study in formal verification.

The goal of this paper is functional verification of sorting algorithms written in Java with mechanical proofs. Because of the complexity of the code under verification, it is essential to break down the problem into subtasks of manage- able size. This is achieved withcontract-based deductive verification[3], where the functionality and the side e↵ects of each method are precisely specified with expressive first-order contracts. In addition, each class is equipped with an in- variant that has to be re-established by each method upon termination. These formal specifications are expressed in the Java Modeling Language (JML) [9].

We use the state-of-art Java verification tool KeY [4], a semi-automatic, in- teractive theorem prover, which covers nearly full sequential Java. KeY typically finds more than 99% of the proof steps automatically (see Sect. 6), while the re- maining ones are interactively done by a human expert. This is facilitated by the use in KeY of symbolic execution plus invariant reasoning as its proof paradigm.

That results in a close correspondence between proof nodes and symbolic pro- gram states which brings the experience of program verification somewhat close to that of debugging.

?Partly funded by the EU project FP7-610582Envisageand the NWO project 612.063.920CoRE.

DSAL/SS 2017 VL-06: Sortieren I 8/34

(11)

Sortieren: Notation

Permutation

EinePermutation einer MengeA={a1, . . . ,an}ist eine bijektive Abbildungπ:AA.

Totale Ordnung

SeiA={a1, . . . ,an}eine Menge. Die binäre Relation≤aufAist eine

totale Ordnungwenn für alleai,aj,akAgilt:

1 Antisymmetrie:aiaj undajai impliziertai =aj.

2 Transitivität:aiaj und ajak impliziertaiak.

3 Totalität:aiaj oderajai. Beispiel

Die lexikographische Ordnung von Zeichenketten und die numerische Ordnung von Zahlen sind totale Ordnungen.

(12)

Sortieren: Notation

Permutation

EinePermutation einer MengeA={a1, . . . ,an}ist eine bijektive Abbildungπ:AA.

Totale Ordnung

SeiA={a1, . . . ,an}eine Menge. Die binäre Relation≤aufAist eine

totale Ordnungwenn für alleai,aj,akAgilt:

1 Antisymmetrie:aiaj undajai impliziertai =aj.

2 Transitivität:aiaj und ajak impliziertaiak.

3 Totalität: aiaj oderajai.

Beispiel

Die lexikographische Ordnung von Zeichenketten und die numerische Ordnung von Zahlen sind totale Ordnungen.

DSAL/SS 2017 VL-06: Sortieren I 9/34

(13)

Sortieren: Notation

Permutation

EinePermutation einer MengeA={a1, . . . ,an}ist eine bijektive Abbildungπ:AA.

Totale Ordnung

SeiA={a1, . . . ,an}eine Menge. Die binäre Relation≤aufAist eine

totale Ordnungwenn für alleai,aj,akAgilt:

1 Antisymmetrie:aiaj undajai impliziertai =aj.

2 Transitivität:aiaj und ajak impliziertaiak.

3 Totalität: aiaj oderajai. Beispiel

Die lexikographische Ordnung von Zeichenketten und die numerische Ordnung von Zahlen sind totale Ordnungen.

(14)

Sortieren: Spezifikation

Das Sortier-Problem

Eingabe: 1 Ein ArrayEmitnEinträgen.

2 Die Einträge gehören zu einer MengeAmit totaler Ordnung≤.

Ausgabe: Ein ArrayFmitn Einträgen, so dass

1 F[1],. . .,F[n]einePermutationvonE[1],. . .,E[n] ist

2 Für alle 0<i <ngilt:F[i]F[i+1].

Annahme in dieser Vorlesung

Elementaroperation = Vergleich von Schlüsseln

DSAL/SS 2017 VL-06: Sortieren I 10/34

(15)

Sortieren: Algorithmen

Beispiel (Einige Sortieralgorithmen)

Insertionsort, Bubblesort, Shellsort, Mergesort, Heapsort, Quicksort, Countingsort, Bucketsort, Radixsort, Cocktailsort, Bogosort, etc.

Stabilität

Ein Sortieralgorithmus iststabil, wenn er die Reihenfolge der Elemente, deren Sortierschlüssel gleich sind, aufrecht erhält.

Zum Beispiel:

Eine Liste von Personendateien ist alphabetisch nach dem Familiennamen sortiert. Diese Liste wird nun nach dem Geburtsdatum neu sortiert wird. Dann bleiben unter einemstabilenSortierverfahren alle Personen mit gleichem Geburtsdatum weiterhin alphabetisch nach Familiennamen sortiert.

(16)

Sortieren: Algorithmen

Beispiel (Einige Sortieralgorithmen)

Insertionsort, Bubblesort, Shellsort, Mergesort, Heapsort, Quicksort, Countingsort, Bucketsort, Radixsort, Cocktailsort, Bogosort, etc.

Stabilität

Ein Sortieralgorithmus iststabil, wenn er die Reihenfolge der Elemente, deren Sortierschlüssel gleich sind, aufrecht erhält.

Zum Beispiel:

Eine Liste von Personendateien ist alphabetisch nach dem Familiennamen sortiert. Diese Liste wird nun nach dem Geburtsdatum neu sortiert wird.

Dann bleiben unter einemstabilenSortierverfahren alle Personen mit gleichem Geburtsdatum weiterhin alphabetisch nach Familiennamen sortiert.

DSAL/SS 2017 VL-06: Sortieren I 11/34

(17)

Dutch National Flag Problem (1)

(18)

Dutch National Flag Problem (2)

Edsger W. Dijkstra (1930–2002):

• Berühmter Informatiker

• Vater der Algorithmik

Beispiel (Das niederländische Flaggen-Problem[Dijkstra, 1972])

Eingabe: 1 Ein ArrayEmitnEinträgen, wobei für alle 0<in

E[i] == rot,E[i] == blauoderE[i] == weiss

2 Ordnung:rot<weiss<blau

Ausgabe: Ein sortiertes Array mit den Einträgen ausE. Erwünschte Worst-Case Zeitkomplexität: Θ(n).

Erwünschte Worst-Case Speicherkomplexität: Θ(1).

DSAL/SS 2017 VL-06: Sortieren I 13/34

(19)

Dutch National Flag Problem (3)

Grundidee

Wir zerlegen das ArrayEin vierRegionen:

(1)0<ir, (2)r<i<u, (3)ui<b, und(4)bin

mit Hilfsvariablenr,uundb, so dass die folgende Invariante gilt:

1 E[1],. . ., E[r]ist“rote”Region:E[i] == rotfür 0<ir

2 E[r+1],. . .,E[u-1]ist“weisse”Region:E[i] == weissfür r<i<u

3 E[u],. . ., E[b-1]ist unbekannte Region:E[i] == rotoder

E[i] == weissoderE[i] == blaufür ui<b

4 E[b],. . ., E[n]ist“blaue”Region:E[i] == blaufür bin

Arrayelemente können mit derswap-Operation vertauscht werden.

(20)

Dutch National Flag Problem (4)

1 v o i d D u t c h N a t i o n a l F l a g (C o l o r E [] , int n ) {

2 int r = 0 , b = n + 1; // r o t e und b l a u e R e g i o n e n l e e r 3 int u = 1; // w e i s s e R e g i o n l e e r ; u n b e k a n n t e == E 4 w h i l e ( u < b ) {

5 if ( E [ u ] == rot ) { 6 s w a p ( E [ r + 1] , E [ u ]) ;

7 r = r + 1; // v e r g r o e s s e r e r o t e R e g i o n 8 u = u + 1; // v e r k l e i n e r e u n b e k a n n t e R e g i o n

9 }

10 if ( E [ u ] == w e i s s ) {

11 u = u + 1;

12 }

13 if ( E [ u ] == b l a u ) { 14 s w a p ( E [ b - 1] , E [ u ]) ;

15 b = b - 1; // v e r g r o e s s e r e b l a u e R e g i o n

16 }

17 }

18 }

r u b

DSAL/SS 2017 VL-06: Sortieren I 15/34

(21)

Dutch National Flag Problem (4)

1 v o i d D u t c h N a t i o n a l F l a g (C o l o r E [] , int n ) {

2 int r = 0 , b = n + 1; // r o t e und b l a u e R e g i o n e n l e e r 3 int u = 1; // w e i s s e R e g i o n l e e r ; u n b e k a n n t e == E 4 w h i l e ( u < b ) {

5 if ( E [ u ] == rot ) { 6 s w a p ( E [ r + 1] , E [ u ]) ;

7 r = r + 1; // v e r g r o e s s e r e r o t e R e g i o n 8 u = u + 1; // v e r k l e i n e r e u n b e k a n n t e R e g i o n

9 }

10 if ( E [ u ] == w e i s s ) {

11 u = u + 1;

12 }

13 if ( E [ u ] == b l a u ) { 14 s w a p ( E [ b - 1] , E [ u ]) ;

15 b = b - 1; // v e r g r o e s s e r e b l a u e R e g i o n

16 }

17 }

18 }

r u b

(22)

Dutch National Flag Problem (4)

1 v o i d D u t c h N a t i o n a l F l a g (C o l o r E [] , int n ) {

2 int r = 0 , b = n + 1; // r o t e und b l a u e R e g i o n e n l e e r 3 int u = 1; // w e i s s e R e g i o n l e e r ; u n b e k a n n t e == E 4 w h i l e ( u < b ) {

5 if ( E [ u ] == rot ) { 6 s w a p ( E [ r + 1] , E [ u ]) ;

7 r = r + 1; // v e r g r o e s s e r e r o t e R e g i o n 8 u = u + 1; // v e r k l e i n e r e u n b e k a n n t e R e g i o n

9 }

10 if ( E [ u ] == w e i s s ) {

11 u = u + 1;

12 }

13 if ( E [ u ] == b l a u ) { 14 s w a p ( E [ b - 1] , E [ u ]) ;

15 b = b - 1; // v e r g r o e s s e r e b l a u e R e g i o n

16 }

17 }

18 }

r u b

DSAL/SS 2017 VL-06: Sortieren I 15/34

(23)

Dutch National Flag Problem (4)

1 v o i d D u t c h N a t i o n a l F l a g (C o l o r E [] , int n ) {

2 int r = 0 , b = n + 1; // r o t e und b l a u e R e g i o n e n l e e r 3 int u = 1; // w e i s s e R e g i o n l e e r ; u n b e k a n n t e == E 4 w h i l e ( u < b ) {

5 if ( E [ u ] == rot ) { 6 s w a p ( E [ r + 1] , E [ u ]) ;

7 r = r + 1; // v e r g r o e s s e r e r o t e R e g i o n 8 u = u + 1; // v e r k l e i n e r e u n b e k a n n t e R e g i o n

9 }

10 if ( E [ u ] == w e i s s ) {

11 u = u + 1;

12 }

13 if ( E [ u ] == b l a u ) { 14 s w a p ( E [ b - 1] , E [ u ]) ;

15 b = b - 1; // v e r g r o e s s e r e b l a u e R e g i o n

16 }

17 }

18 }

r u b

(24)

Dutch National Flag Problem (4)

1 v o i d D u t c h N a t i o n a l F l a g (C o l o r E [] , int n ) {

2 int r = 0 , b = n + 1; // r o t e und b l a u e R e g i o n e n l e e r 3 int u = 1; // w e i s s e R e g i o n l e e r ; u n b e k a n n t e == E 4 w h i l e ( u < b ) {

5 if ( E [ u ] == rot ) { 6 s w a p ( E [ r + 1] , E [ u ]) ;

7 r = r + 1; // v e r g r o e s s e r e r o t e R e g i o n 8 u = u + 1; // v e r k l e i n e r e u n b e k a n n t e R e g i o n

9 }

10 if ( E [ u ] == w e i s s ) {

11 u = u + 1;

12 }

13 if ( E [ u ] == b l a u ) { 14 s w a p ( E [ b - 1] , E [ u ]) ;

15 b = b - 1; // v e r g r o e s s e r e b l a u e R e g i o n

16 }

17 }

18 }

r u b

DSAL/SS 2017 VL-06: Sortieren I 15/34

(25)

Dutch National Flag Problem (4)

1 v o i d D u t c h N a t i o n a l F l a g (C o l o r E [] , int n ) {

2 int r = 0 , b = n + 1; // r o t e und b l a u e R e g i o n e n l e e r 3 int u = 1; // w e i s s e R e g i o n l e e r ; u n b e k a n n t e == E 4 w h i l e ( u < b ) {

5 if ( E [ u ] == rot ) { 6 s w a p ( E [ r + 1] , E [ u ]) ;

7 r = r + 1; // v e r g r o e s s e r e r o t e R e g i o n 8 u = u + 1; // v e r k l e i n e r e u n b e k a n n t e R e g i o n

9 }

10 if ( E [ u ] == w e i s s ) {

11 u = u + 1;

12 }

13 if ( E [ u ] == b l a u ) { 14 s w a p ( E [ b - 1] , E [ u ]) ;

15 b = b - 1; // v e r g r o e s s e r e b l a u e R e g i o n

16 }

17 }

18 }

r u b

(26)

Dutch National Flag Problem (4)

1 v o i d D u t c h N a t i o n a l F l a g (C o l o r E [] , int n ) {

2 int r = 0 , b = n + 1; // r o t e und b l a u e R e g i o n e n l e e r 3 int u = 1; // w e i s s e R e g i o n l e e r ; u n b e k a n n t e == E 4 w h i l e ( u < b ) {

5 if ( E [ u ] == rot ) { 6 s w a p ( E [ r + 1] , E [ u ]) ;

7 r = r + 1; // v e r g r o e s s e r e r o t e R e g i o n 8 u = u + 1; // v e r k l e i n e r e u n b e k a n n t e R e g i o n

9 }

10 if ( E [ u ] == w e i s s ) {

11 u = u + 1;

12 }

13 if ( E [ u ] == b l a u ) { 14 s w a p ( E [ b - 1] , E [ u ]) ;

15 b = b - 1; // v e r g r o e s s e r e b l a u e R e g i o n

16 }

17 }

18 }

r u b

DSAL/SS 2017 VL-06: Sortieren I 15/34

(27)

Dutch National Flag Problem (4)

1 v o i d D u t c h N a t i o n a l F l a g (C o l o r E [] , int n ) {

2 int r = 0 , b = n + 1; // r o t e und b l a u e R e g i o n e n l e e r 3 int u = 1; // w e i s s e R e g i o n l e e r ; u n b e k a n n t e == E 4 w h i l e ( u < b ) {

5 if ( E [ u ] == rot ) { 6 s w a p ( E [ r + 1] , E [ u ]) ;

7 r = r + 1; // v e r g r o e s s e r e r o t e R e g i o n 8 u = u + 1; // v e r k l e i n e r e u n b e k a n n t e R e g i o n

9 }

10 if ( E [ u ] == w e i s s ) {

11 u = u + 1;

12 }

13 if ( E [ u ] == b l a u ) { 14 s w a p ( E [ b - 1] , E [ u ]) ;

15 b = b - 1; // v e r g r o e s s e r e b l a u e R e g i o n

16 }

17 }

18 }

r u b

(28)

Dutch National Flag Problem (4)

1 v o i d D u t c h N a t i o n a l F l a g (C o l o r E [] , int n ) {

2 int r = 0 , b = n + 1; // r o t e und b l a u e R e g i o n e n l e e r 3 int u = 1; // w e i s s e R e g i o n l e e r ; u n b e k a n n t e == E 4 w h i l e ( u < b ) {

5 if ( E [ u ] == rot ) { 6 s w a p ( E [ r + 1] , E [ u ]) ;

7 r = r + 1; // v e r g r o e s s e r e r o t e R e g i o n 8 u = u + 1; // v e r k l e i n e r e u n b e k a n n t e R e g i o n

9 }

10 if ( E [ u ] == w e i s s ) {

11 u = u + 1;

12 }

13 if ( E [ u ] == b l a u ) { 14 s w a p ( E [ b - 1] , E [ u ]) ;

15 b = b - 1; // v e r g r o e s s e r e b l a u e R e g i o n

16 }

17 }

18 }

r u b

DSAL/SS 2017 VL-06: Sortieren I 15/34

(29)

Dutch National Flag Problem (4)

1 v o i d D u t c h N a t i o n a l F l a g (C o l o r E [] , int n ) {

2 int r = 0 , b = n + 1; // r o t e und b l a u e R e g i o n e n l e e r 3 int u = 1; // w e i s s e R e g i o n l e e r ; u n b e k a n n t e == E 4 w h i l e ( u < b ) {

5 if ( E [ u ] == rot ) { 6 s w a p ( E [ r + 1] , E [ u ]) ;

7 r = r + 1; // v e r g r o e s s e r e r o t e R e g i o n 8 u = u + 1; // v e r k l e i n e r e u n b e k a n n t e R e g i o n

9 }

10 if ( E [ u ] == w e i s s ) {

11 u = u + 1;

12 }

13 if ( E [ u ] == b l a u ) { 14 s w a p ( E [ b - 1] , E [ u ]) ;

15 b = b - 1; // v e r g r o e s s e r e b l a u e R e g i o n

16 }

17 }

18 }

r u b

(30)

Dutch National Flag Problem (4)

1 v o i d D u t c h N a t i o n a l F l a g (C o l o r E [] , int n ) {

2 int r = 0 , b = n + 1; // r o t e und b l a u e R e g i o n e n l e e r 3 int u = 1; // w e i s s e R e g i o n l e e r ; u n b e k a n n t e == E 4 w h i l e ( u < b ) {

5 if ( E [ u ] == rot ) { 6 s w a p ( E [ r + 1] , E [ u ]) ;

7 r = r + 1; // v e r g r o e s s e r e r o t e R e g i o n 8 u = u + 1; // v e r k l e i n e r e u n b e k a n n t e R e g i o n

9 }

10 if ( E [ u ] == w e i s s ) {

11 u = u + 1;

12 }

13 if ( E [ u ] == b l a u ) { 14 s w a p ( E [ b - 1] , E [ u ]) ;

15 b = b - 1; // v e r g r o e s s e r e b l a u e R e g i o n

16 }

17 }

18 }

r u b

DSAL/SS 2017 VL-06: Sortieren I 15/34

(31)

Dutch National Flag Problem (4)

1 v o i d D u t c h N a t i o n a l F l a g (C o l o r E [] , int n ) {

2 int r = 0 , b = n + 1; // r o t e und b l a u e R e g i o n e n l e e r 3 int u = 1; // w e i s s e R e g i o n l e e r ; u n b e k a n n t e == E 4 w h i l e ( u < b ) {

5 if ( E [ u ] == rot ) { 6 s w a p ( E [ r + 1] , E [ u ]) ;

7 r = r + 1; // v e r g r o e s s e r e r o t e R e g i o n 8 u = u + 1; // v e r k l e i n e r e u n b e k a n n t e R e g i o n

9 }

10 if ( E [ u ] == w e i s s ) {

11 u = u + 1;

12 }

13 if ( E [ u ] == b l a u ) { 14 s w a p ( E [ b - 1] , E [ u ]) ;

15 b = b - 1; // v e r g r o e s s e r e b l a u e R e g i o n

16 }

17 }

18 }

r b

u

(32)

Dutch National Flag Problem (5)

Frage

Ist DNF-Algorithmus einstabilesSortierverfahren?

Antwort:Nein Speicherkomplexität

Die Worst-Case Speicherkomplexität vom DNF-Algorithmus istΘ(1), dar,uundb die einzigen extra Variablen sind. DNF istin-place: Algorithmus arbeitet ohne zusätzlichen Speicherplatz.

Zeitkomplexität

(Elementaroperation = Vergleich der FormE[i] == ....) Die Worst-Case Zeitkomplexität istΘ(n):

1 in jedem Durchlauf werden konstant viele Vergleiche durchgeführt

2 die Anzahl der Durchläufe ist Θ(n), da in jedem Durchlauf die Grösseb-udes unbekannten Gebiets um mindestens eins und um höchstens drei verkleinert wird.

DSAL/SS 2017 VL-06: Sortieren I 16/34

(33)

Dutch National Flag Problem (5)

Frage

Ist DNF-Algorithmus einstabilesSortierverfahren? Antwort:Nein

Speicherkomplexität

Die Worst-Case Speicherkomplexität vom DNF-Algorithmus istΘ(1), dar,uundb die einzigen extra Variablen sind. DNF istin-place: Algorithmus arbeitet ohne zusätzlichen Speicherplatz.

Zeitkomplexität

(Elementaroperation = Vergleich der FormE[i] == ....) Die Worst-Case Zeitkomplexität istΘ(n):

1 in jedem Durchlauf werden konstant viele Vergleiche durchgeführt

2 die Anzahl der Durchläufe ist Θ(n), da in jedem Durchlauf die Grösseb-udes unbekannten Gebiets um mindestens eins und um höchstens drei verkleinert wird.

(34)

Dutch National Flag Problem (5)

Frage

Ist DNF-Algorithmus einstabilesSortierverfahren? Antwort:Nein Speicherkomplexität

Die Worst-Case Speicherkomplexität vom DNF-Algorithmus istΘ(1), dar,uundb die einzigen extra Variablen sind. DNF istin-place:

Algorithmus arbeitet ohne zusätzlichen Speicherplatz.

Zeitkomplexität

(Elementaroperation = Vergleich der FormE[i] == ....) Die Worst-Case Zeitkomplexität istΘ(n):

1 in jedem Durchlauf werden konstant viele Vergleiche durchgeführt

2 die Anzahl der Durchläufe ist Θ(n), da in jedem Durchlauf die Grösseb-udes unbekannten Gebiets um mindestens eins und um höchstens drei verkleinert wird.

DSAL/SS 2017 VL-06: Sortieren I 16/34

(35)

Dutch National Flag Problem (5)

Frage

Ist DNF-Algorithmus einstabilesSortierverfahren? Antwort:Nein Speicherkomplexität

Die Worst-Case Speicherkomplexität vom DNF-Algorithmus istΘ(1), dar,uundb die einzigen extra Variablen sind. DNF istin-place:

Algorithmus arbeitet ohne zusätzlichen Speicherplatz.

Zeitkomplexität

(Elementaroperation = Vergleich der FormE[i] == ....) Die Worst-Case Zeitkomplexität istΘ(n):

1 in jedem Durchlauf werden konstant viele Vergleiche durchgeführt

2 die Anzahl der Durchläufe ist Θ(n), da in jedem Durchlauf die Grösseb-udes unbekannten Gebiets um mindestens eins und um höchstens drei verkleinert wird.

(36)

Sortieren durch Einfügen (InsertionSort)

DSAL/SS 2017 VL-06: Sortieren I 17/34

(37)

InsertionSort: Idee

0 12 17 17 19 8 25 3 6 69 26 4 2 13 34 41

als Nächstes einzusortieren

bereits sortiert noch unsortiert

Durchlaufe das (unsortierte) Array von links nach rechts

Gehe zum ersten noch nicht behandelten Element (gelb)

Füge es im sortierten Teil (grün) durch elementweise Vergleiche ein

(38)

InsertionSort: Idee

0 12 17 17 19 8 25 3 6 69 26 4 2 13 34 41

als Nächstes einzusortieren

bereits sortiert noch unsortiert

Durchlaufe das (unsortierte) Array von links nach rechts Gehe zum ersten noch nicht behandelten Element (gelb)

Füge es im sortierten Teil (grün) durch elementweise Vergleiche ein

DSAL/SS 2017 VL-06: Sortieren I 18/34

(39)

InsertionSort: Idee

0 12 17 17 19 8 25 3 6 69 26 4 2 13 34 41

als Nächstes einzusortieren

bereits sortiert noch unsortiert

Durchlaufe das (unsortierte) Array von links nach rechts Gehe zum ersten noch nicht behandelten Element (gelb)

Füge es im sortierten Teil (grün) durch elementweise Vergleiche ein

(40)

InsertionSort: Animation und Algorithmus

41 26 17 25 19 17 8 3 6 69 12 4 2 13 34 0

1 v o i d i n s e r t i o n S o r t ( int E []) { 2 int i , j ;

3 for ( i = 1; i < E . l e n g t h ; i ++) { 4 int v = E [ i ]; // s p e i c h e r e E [ i ]

5 for ( j = i ; j > 0 && E [ j -1] > v ; j - -) {

6 E [ j ] = E [ j - 1 ] ; // s c h i e b e E l e m e n t j -1 n a c h r e c h t s

7 }

8 E [ j ] = v ; // f u e g e E [ i ] an der r i c h t i g e n S t e l l e ein

9 }

10 }

Insertionsort ist in-place: kein zusätzlicher Speicherplatz Insertionsort ist stabil: die Reihenfolge der gleichwertigen Arrayelemente bleibt unverändert

DSAL/SS 2017 VL-06: Sortieren I 19/34

(41)

InsertionSort: Animation und Algorithmus

26 41 17 25 19 17 8 3 6 69 12 4 2 13 34 0

1 v o i d i n s e r t i o n S o r t ( int E []) { 2 int i , j ;

3 for ( i = 1; i < E . l e n g t h ; i ++) { 4 int v = E [ i ]; // s p e i c h e r e E [ i ]

5 for ( j = i ; j > 0 && E [ j -1] > v ; j - -) {

6 E [ j ] = E [ j - 1 ] ; // s c h i e b e E l e m e n t j -1 n a c h r e c h t s

7 }

8 E [ j ] = v ; // f u e g e E [ i ] an der r i c h t i g e n S t e l l e ein

9 }

10 }

Insertionsort ist in-place: kein zusätzlicher Speicherplatz Insertionsort ist stabil: die Reihenfolge der gleichwertigen Arrayelemente bleibt unverändert

(42)

InsertionSort: Animation und Algorithmus

17 26 41 25 19 17 8 3 6 69 12 4 2 13 34 0

1 v o i d i n s e r t i o n S o r t ( int E []) { 2 int i , j ;

3 for ( i = 1; i < E . l e n g t h ; i ++) { 4 int v = E [ i ]; // s p e i c h e r e E [ i ]

5 for ( j = i ; j > 0 && E [ j -1] > v ; j - -) {

6 E [ j ] = E [ j - 1 ] ; // s c h i e b e E l e m e n t j -1 n a c h r e c h t s

7 }

8 E [ j ] = v ; // f u e g e E [ i ] an der r i c h t i g e n S t e l l e ein

9 }

10 }

Insertionsort ist in-place: kein zusätzlicher Speicherplatz Insertionsort ist stabil: die Reihenfolge der gleichwertigen Arrayelemente bleibt unverändert

DSAL/SS 2017 VL-06: Sortieren I 19/34

(43)

InsertionSort: Animation und Algorithmus

17 25 26 41 19 17 8 3 6 69 12 4 2 13 34 0

1 v o i d i n s e r t i o n S o r t ( int E []) { 2 int i , j ;

3 for ( i = 1; i < E . l e n g t h ; i ++) { 4 int v = E [ i ]; // s p e i c h e r e E [ i ]

5 for ( j = i ; j > 0 && E [ j -1] > v ; j - -) {

6 E [ j ] = E [ j - 1 ] ; // s c h i e b e E l e m e n t j -1 n a c h r e c h t s

7 }

8 E [ j ] = v ; // f u e g e E [ i ] an der r i c h t i g e n S t e l l e ein

9 }

10 }

Insertionsort ist in-place: kein zusätzlicher Speicherplatz Insertionsort ist stabil: die Reihenfolge der gleichwertigen Arrayelemente bleibt unverändert

(44)

InsertionSort: Animation und Algorithmus

17 19 25 26 41 17 8 3 6 69 12 4 2 13 34 0

1 v o i d i n s e r t i o n S o r t ( int E []) { 2 int i , j ;

3 for ( i = 1; i < E . l e n g t h ; i ++) { 4 int v = E [ i ]; // s p e i c h e r e E [ i ]

5 for ( j = i ; j > 0 && E [ j -1] > v ; j - -) {

6 E [ j ] = E [ j - 1 ] ; // s c h i e b e E l e m e n t j -1 n a c h r e c h t s

7 }

8 E [ j ] = v ; // f u e g e E [ i ] an der r i c h t i g e n S t e l l e ein

9 }

10 }

Insertionsort ist in-place: kein zusätzlicher Speicherplatz Insertionsort ist stabil: die Reihenfolge der gleichwertigen Arrayelemente bleibt unverändert

DSAL/SS 2017 VL-06: Sortieren I 19/34

(45)

InsertionSort: Animation und Algorithmus

17 17 19 25 26 41 8 3 6 69 12 4 2 13 34 0

1 v o i d i n s e r t i o n S o r t ( int E []) { 2 int i , j ;

3 for ( i = 1; i < E . l e n g t h ; i ++) { 4 int v = E [ i ]; // s p e i c h e r e E [ i ]

5 for ( j = i ; j > 0 && E [ j -1] > v ; j - -) {

6 E [ j ] = E [ j - 1 ] ; // s c h i e b e E l e m e n t j -1 n a c h r e c h t s

7 }

8 E [ j ] = v ; // f u e g e E [ i ] an der r i c h t i g e n S t e l l e ein

9 }

10 }

Insertionsort ist in-place: kein zusätzlicher Speicherplatz Insertionsort ist stabil: die Reihenfolge der gleichwertigen Arrayelemente bleibt unverändert

(46)

InsertionSort: Animation und Algorithmus

8 17 17 19 25 26 41 3 6 69 12 4 2 13 34 0

1 v o i d i n s e r t i o n S o r t ( int E []) { 2 int i , j ;

3 for ( i = 1; i < E . l e n g t h ; i ++) { 4 int v = E [ i ]; // s p e i c h e r e E [ i ]

5 for ( j = i ; j > 0 && E [ j -1] > v ; j - -) {

6 E [ j ] = E [ j - 1 ] ; // s c h i e b e E l e m e n t j -1 n a c h r e c h t s

7 }

8 E [ j ] = v ; // f u e g e E [ i ] an der r i c h t i g e n S t e l l e ein

9 }

10 }

Insertionsort ist in-place: kein zusätzlicher Speicherplatz Insertionsort ist stabil: die Reihenfolge der gleichwertigen Arrayelemente bleibt unverändert

DSAL/SS 2017 VL-06: Sortieren I 19/34

(47)

InsertionSort: Animation und Algorithmus

3 8 17 17 19 25 26 41 6 69 12 4 2 13 34 0

1 v o i d i n s e r t i o n S o r t ( int E []) { 2 int i , j ;

3 for ( i = 1; i < E . l e n g t h ; i ++) { 4 int v = E [ i ]; // s p e i c h e r e E [ i ]

5 for ( j = i ; j > 0 && E [ j -1] > v ; j - -) {

6 E [ j ] = E [ j - 1 ] ; // s c h i e b e E l e m e n t j -1 n a c h r e c h t s

7 }

8 E [ j ] = v ; // f u e g e E [ i ] an der r i c h t i g e n S t e l l e ein

9 }

10 }

Insertionsort ist in-place: kein zusätzlicher Speicherplatz Insertionsort ist stabil: die Reihenfolge der gleichwertigen Arrayelemente bleibt unverändert

(48)

InsertionSort: Animation und Algorithmus

3 6 8 17 17 19 25 26 41 69 12 4 2 13 34 0

1 v o i d i n s e r t i o n S o r t ( int E []) { 2 int i , j ;

3 for ( i = 1; i < E . l e n g t h ; i ++) { 4 int v = E [ i ]; // s p e i c h e r e E [ i ]

5 for ( j = i ; j > 0 && E [ j -1] > v ; j - -) {

6 E [ j ] = E [ j - 1 ] ; // s c h i e b e E l e m e n t j -1 n a c h r e c h t s

7 }

8 E [ j ] = v ; // f u e g e E [ i ] an der r i c h t i g e n S t e l l e ein

9 }

10 }

Insertionsort ist in-place: kein zusätzlicher Speicherplatz Insertionsort ist stabil: die Reihenfolge der gleichwertigen Arrayelemente bleibt unverändert

DSAL/SS 2017 VL-06: Sortieren I 19/34

(49)

InsertionSort: Animation und Algorithmus

3 6 8 17 17 19 25 26 41 69 12 4 2 13 34 0

1 v o i d i n s e r t i o n S o r t ( int E []) { 2 int i , j ;

3 for ( i = 1; i < E . l e n g t h ; i ++) { 4 int v = E [ i ]; // s p e i c h e r e E [ i ]

5 for ( j = i ; j > 0 && E [ j -1] > v ; j - -) {

6 E [ j ] = E [ j - 1 ] ; // s c h i e b e E l e m e n t j -1 n a c h r e c h t s

7 }

8 E [ j ] = v ; // f u e g e E [ i ] an der r i c h t i g e n S t e l l e ein

9 }

10 }

Insertionsort ist in-place: kein zusätzlicher Speicherplatz Insertionsort ist stabil: die Reihenfolge der gleichwertigen Arrayelemente bleibt unverändert

(50)

InsertionSort: Animation und Algorithmus

3 6 8 12 17 17 19 25 26 41 69 4 2 13 34 0

1 v o i d i n s e r t i o n S o r t ( int E []) { 2 int i , j ;

3 for ( i = 1; i < E . l e n g t h ; i ++) { 4 int v = E [ i ]; // s p e i c h e r e E [ i ]

5 for ( j = i ; j > 0 && E [ j -1] > v ; j - -) {

6 E [ j ] = E [ j - 1 ] ; // s c h i e b e E l e m e n t j -1 n a c h r e c h t s

7 }

8 E [ j ] = v ; // f u e g e E [ i ] an der r i c h t i g e n S t e l l e ein

9 }

10 }

Insertionsort ist in-place: kein zusätzlicher Speicherplatz Insertionsort ist stabil: die Reihenfolge der gleichwertigen Arrayelemente bleibt unverändert

DSAL/SS 2017 VL-06: Sortieren I 19/34

(51)

InsertionSort: Animation und Algorithmus

3 4 6 8 12 17 17 19 25 26 41 69 2 13 34 0

1 v o i d i n s e r t i o n S o r t ( int E []) { 2 int i , j ;

3 for ( i = 1; i < E . l e n g t h ; i ++) { 4 int v = E [ i ]; // s p e i c h e r e E [ i ]

5 for ( j = i ; j > 0 && E [ j -1] > v ; j - -) {

6 E [ j ] = E [ j - 1 ] ; // s c h i e b e E l e m e n t j -1 n a c h r e c h t s

7 }

8 E [ j ] = v ; // f u e g e E [ i ] an der r i c h t i g e n S t e l l e ein

9 }

10 }

Insertionsort ist in-place: kein zusätzlicher Speicherplatz Insertionsort ist stabil: die Reihenfolge der gleichwertigen Arrayelemente bleibt unverändert

(52)

InsertionSort: Animation und Algorithmus

2 3 4 6 8 12 17 17 19 25 26 41 69 13 34 0

1 v o i d i n s e r t i o n S o r t ( int E []) { 2 int i , j ;

3 for ( i = 1; i < E . l e n g t h ; i ++) { 4 int v = E [ i ]; // s p e i c h e r e E [ i ]

5 for ( j = i ; j > 0 && E [ j -1] > v ; j - -) {

6 E [ j ] = E [ j - 1 ] ; // s c h i e b e E l e m e n t j -1 n a c h r e c h t s

7 }

8 E [ j ] = v ; // f u e g e E [ i ] an der r i c h t i g e n S t e l l e ein

9 }

10 }

Insertionsort ist in-place: kein zusätzlicher Speicherplatz Insertionsort ist stabil: die Reihenfolge der gleichwertigen Arrayelemente bleibt unverändert

DSAL/SS 2017 VL-06: Sortieren I 19/34

(53)

InsertionSort: Animation und Algorithmus

2 3 4 6 8 12 13 17 17 19 25 26 41 69 34 0

1 v o i d i n s e r t i o n S o r t ( int E []) { 2 int i , j ;

3 for ( i = 1; i < E . l e n g t h ; i ++) { 4 int v = E [ i ]; // s p e i c h e r e E [ i ]

5 for ( j = i ; j > 0 && E [ j -1] > v ; j - -) {

6 E [ j ] = E [ j - 1 ] ; // s c h i e b e E l e m e n t j -1 n a c h r e c h t s

7 }

8 E [ j ] = v ; // f u e g e E [ i ] an der r i c h t i g e n S t e l l e ein

9 }

10 }

Insertionsort ist in-place: kein zusätzlicher Speicherplatz Insertionsort ist stabil: die Reihenfolge der gleichwertigen Arrayelemente bleibt unverändert

(54)

InsertionSort: Animation und Algorithmus

2 3 4 6 8 12 13 17 17 19 25 26 34 41 69 0

1 v o i d i n s e r t i o n S o r t ( int E []) { 2 int i , j ;

3 for ( i = 1; i < E . l e n g t h ; i ++) { 4 int v = E [ i ]; // s p e i c h e r e E [ i ]

5 for ( j = i ; j > 0 && E [ j -1] > v ; j - -) {

6 E [ j ] = E [ j - 1 ] ; // s c h i e b e E l e m e n t j -1 n a c h r e c h t s

7 }

8 E [ j ] = v ; // f u e g e E [ i ] an der r i c h t i g e n S t e l l e ein

9 }

10 }

Insertionsort ist in-place: kein zusätzlicher Speicherplatz Insertionsort ist stabil: die Reihenfolge der gleichwertigen Arrayelemente bleibt unverändert

DSAL/SS 2017 VL-06: Sortieren I 19/34

(55)

InsertionSort: Animation und Algorithmus

0 2 3 4 6 8 12 13 17 17 19 25 26 34 41 69

1 v o i d i n s e r t i o n S o r t ( int E []) { 2 int i , j ;

3 for ( i = 1; i < E . l e n g t h ; i ++) { 4 int v = E [ i ]; // s p e i c h e r e E [ i ]

5 for ( j = i ; j > 0 && E [ j -1] > v ; j - -) {

6 E [ j ] = E [ j - 1 ] ; // s c h i e b e E l e m e n t j -1 n a c h r e c h t s

7 }

8 E [ j ] = v ; // f u e g e E [ i ] an der r i c h t i g e n S t e l l e ein

9 }

10 }

Insertionsort ist in-place: kein zusätzlicher Speicherplatz Insertionsort ist stabil: die Reihenfolge der gleichwertigen Arrayelemente bleibt unverändert

(56)

InsertionSort: Animation und Algorithmus

1 v o i d i n s e r t i o n S o r t ( int E []) { 2 int i , j ;

3 for ( i = 1; i < E . l e n g t h ; i ++) { 4 int v = E [ i ]; // s p e i c h e r e E [ i ]

5 for ( j = i ; j > 0 && E [ j -1] > v ; j - -) {

6 E [ j ] = E [ j - 1 ] ; // s c h i e b e E l e m e n t j -1 n a c h r e c h t s

7 }

8 E [ j ] = v ; // f u e g e E [ i ] an der r i c h t i g e n S t e l l e ein

9 }

10 }

Insertionsort ist in-place: kein zusätzlicher Speicherplatz Insertionsort ist stabil: die Reihenfolge der gleichwertigen Arrayelemente bleibt unverändert

DSAL/SS 2017 VL-06: Sortieren I 19/34

(57)

InsertionSort: Best- und Worst-Case-Analyse

Best-Case

Im Best-Case ist das Array bereits sortiert.

Pro Element ist dann nur ein Vergleich nötig:

ErgoB(n) =n−1∈Θ(n)

Worst-Case

Im Worst-Case wird das einzusortierende Element immer ganz vorne eingefügt

Es mussmit allen vorhergehendenElementen verglichen werden Das tritt etwa auf, wenn das Array umgekehrt vorsortiert war Zum Einsortieren desi-ten Elements sind dann iVergleiche nötig. ErgoW(n) =

n−1

X

i=1

i =1

2n·(n−1)∈Θ(n2)

(58)

InsertionSort: Best- und Worst-Case-Analyse

Best-Case

Im Best-Case ist das Array bereits sortiert.

Pro Element ist dann nur ein Vergleich nötig:

ErgoB(n) =n−1∈Θ(n) Worst-Case

Im Worst-Case wird das einzusortierende Element immer ganz vorne eingefügt

Es mussmit allen vorhergehendenElementen verglichen werden Das tritt etwa auf, wenn das Array umgekehrt vorsortiert war Zum Einsortieren desi-ten Elements sind dann iVergleiche nötig.

ErgoW(n) =

n−1

X

i=1

i =1

2n·(n−1)∈Θ(n2)

DSAL/SS 2017 VL-06: Sortieren I 20/34

(59)

InsertionSort: Average-Case-Analyse (1)

Unsere Annahmen für Average-Case-Analyse

Alle Permutationen der Elemente treten mit gleicher Häufigkeit auf.

Die zu sortierenden Elemente sind alle verschieden.

Dann gilt:

A(n) =

n−1

X

i=1

(erwartete Anzahl von Vergleichen, umE[i]einzusortieren)

Wir bestimmen zunächst die erwartete Anzahl von Vergleichen, um den richtigen Platz fürE[i]zu finden

(60)

Insertionsort – Average-Case-Analyse (2)

i

X

j=0

Pr (

i-tes Element wird an Positionj eingefügt

)

· Anzahl Vergleiche, umE[i]

an Positionj einzufügen

!

(Comment:E[i]wird an beliebiger Positionjmit gleicher W’lichkeit eingefügt)

=

i

X

j=0

1

i+1 · Anzahl Vergleiche, umE[i] an Positionj einzufügen

!

(Comment: Anzahl Vergleiche, um an Position 0 einzufügen isti, sonsti−j+1.)

= 1

i+1 ·i+ 1 i+1 ·

i

X

j=1

(i−j+1)

= i

i+1+ 1 i+1·

i

X

j=1

j = i

i+1+ 1

i+1·i·(i+1)

2 = i

2 +1− 1 i+1.

DSAL/SS 2017 VL-06: Sortieren I 22/34

(61)

Insertionsort – Average-Case-Analyse (2)

i

X

j=0

Pr (

i-tes Element wird an Positionj eingefügt

)

· Anzahl Vergleiche, umE[i]

an Positionj einzufügen

!

(Comment:E[i]wird an beliebiger Positionjmit gleicher W’lichkeit eingefügt)

=

i

X

j=0

1

i+1 · Anzahl Vergleiche, umE[i]

an Positionj einzufügen

!

(Comment: Anzahl Vergleiche, um an Position 0 einzufügen isti, sonsti−j+1.)

= 1

i+1 ·i+ 1 i+1 ·

i

X

j=1

(i−j+1)

= i

i+1+ 1 i+1·

i

X

j=1

j = i

i+1+ 1

i+1·i·(i+1)

2 = i

2 +1− 1 i+1.

(62)

Insertionsort – Average-Case-Analyse (2)

i

X

j=0

Pr (

i-tes Element wird an Positionj eingefügt

)

· Anzahl Vergleiche, umE[i]

an Positionj einzufügen

!

(Comment:E[i]wird an beliebiger Positionjmit gleicher W’lichkeit eingefügt)

=

i

X

j=0

1

i+1 · Anzahl Vergleiche, umE[i]

an Positionj einzufügen

!

(Comment: Anzahl Vergleiche, um an Position 0 einzufügen isti, sonsti−j+1.)

= 1

i+1 ·i+ 1 i+1 ·

i

X

j=1

(i−j+1)

= i

i+1+ 1 i+1·

i

X

j=1

j = i

i+1+ 1

i+1·i·(i+1)

2 = i

2 +1− 1 i+1.

DSAL/SS 2017 VL-06: Sortieren I 22/34

(63)

Insertionsort – Average-Case-Analyse (2)

i

X

j=0

Pr (

i-tes Element wird an Positionj eingefügt

)

· Anzahl Vergleiche, umE[i]

an Positionj einzufügen

!

(Comment:E[i]wird an beliebiger Positionjmit gleicher W’lichkeit eingefügt)

=

i

X

j=0

1

i+1 · Anzahl Vergleiche, umE[i]

an Positionj einzufügen

!

(Comment: Anzahl Vergleiche, um an Position 0 einzufügen isti, sonsti−j+1.)

= 1

i+1 ·i+ 1 i+1 ·

i

X

j=1

(i−j+1)

= i

i+1+ 1 i+1·

i

X

j=1

j = i

i+1+ 1

i+1·i·(i+1)

2 = i

2 +1− 1 i+1.

(64)

Insertionsort – Average-Case-Analyse (3)

Harmonische Reihe: Pn

i=1(1/i)≈lnn

A(n) =

n−1

X

i=1

i

2 +1− 1 i+1

= n·(n−1)

4 + (n−1)−

n

X

i=2

1 i

= n·(n−1)

4 +n

n

X

i=1

1 i

n·(n−1)

4 +n−lnn ∈Θ(n2)

Insertionsort ist im Worst-Case und im Average-Casequadratisch.

DSAL/SS 2017 VL-06: Sortieren I 23/34

(65)

Insertionsort – Average-Case-Analyse (3)

Harmonische Reihe: Pn

i=1(1/i)≈lnn

A(n) =

n−1

X

i=1

i

2 +1− 1 i+1

= n·(n−1)

4 + (n−1)−

n

X

i=2

1 i

= n·(n−1)

4 +n

n

X

i=1

1 i

n·(n−1)

4 +n−lnn ∈Θ(n2)

Insertionsort ist im Worst-Case und im Average-Casequadratisch.

(66)

Insertionsort – Average-Case-Analyse (3)

Harmonische Reihe: Pn

i=1(1/i)≈lnn

A(n) =

n−1

X

i=1

i

2 +1− 1 i+1

= n·(n−1)

4 + (n−1)−

n

X

i=2

1 i

= n·(n−1)

4 +n

n

X

i=1

1 i

n·(n−1)

4 +n−lnn ∈Θ(n2)

Insertionsort ist im Worst-Case und im Average-Casequadratisch.

DSAL/SS 2017 VL-06: Sortieren I 23/34

(67)

Insertionsort – Average-Case-Analyse (3)

Harmonische Reihe: Pn

i=1(1/i)≈lnn

A(n) =

n−1

X

i=1

i

2 +1− 1 i+1

= n·(n−1)

4 + (n−1)−

n

X

i=2

1 i

= n·(n−1)

4 +n

n

X

i=1

1 i

n·(n−1)

4 +n−lnn ∈Θ(n2)

Insertionsort ist im Worst-Case und im Average-Casequadratisch.

(68)

Divide-and-Conquer und MergeSort

DSAL/SS 2017 VL-06: Sortieren I 24/34

(69)

Divide-and-Conquer

Teile-und-Beherrsche

Teile-und-BeherrscheAlgorithmen (divide-and-conquer) teilen das Problem in mehrere Teilprobleme auf, die dem Ausgangsproblem ähneln, jedoch von kleinerer Grösse sind.

Sie lösen die Teilproblemerekursivund kombinieren diese Lösungen dann, um die Lösung des eigentlichen Problems zu erstellen.

Das Paradigma von Teile-und-Beherrsche umfasst 3 Schritte auf jeder Rekursionsebene:

Teile das Problem in eine Anzahl von Teilproblemen auf. Beherrsche die Teilprobleme durch rekursives Lösen. Hinreichend

kleine Teilprobleme werden direkt gelöst. Verbinde die Lösungen der Teilprobleme zur Lösung des

Ausgangsproblems.

(70)

Divide-and-Conquer

Teile-und-Beherrsche

Teile-und-BeherrscheAlgorithmen (divide-and-conquer) teilen das Problem in mehrere Teilprobleme auf, die dem Ausgangsproblem ähneln, jedoch von kleinerer Grösse sind.

Sie lösen die Teilproblemerekursivund kombinieren diese Lösungen dann, um die Lösung des eigentlichen Problems zu erstellen.

Das Paradigma von Teile-und-Beherrsche umfasst 3 Schritte auf jeder Rekursionsebene:

Teile das Problem in eine Anzahl von Teilproblemen auf.

Beherrsche die Teilprobleme durch rekursives Lösen. Hinreichend kleine Teilprobleme werden direkt gelöst.

Verbinde die Lösungen der Teilprobleme zur Lösung des Ausgangsproblems.

DSAL/SS 2017 VL-06: Sortieren I 25/34

(71)

Divide-and-Conquer

Teile-und-Beherrsche

Teile-und-BeherrscheAlgorithmen (divide-and-conquer) teilen das Problem in mehrere Teilprobleme auf, die dem Ausgangsproblem ähneln, jedoch von kleinerer Grösse sind.

Sie lösen die Teilproblemerekursivund kombinieren diese Lösungen dann, um die Lösung des eigentlichen Problems zu erstellen.

Das Paradigma von Teile-und-Beherrsche umfasst 3 Schritte auf jeder Rekursionsebene:

Teile das Problem in eine Anzahl von Teilproblemen auf.

Beherrsche die Teilprobleme durch rekursives Lösen. Hinreichend kleine Teilprobleme werden direkt gelöst.

Verbinde die Lösungen der Teilprobleme zur Lösung des Ausgangsproblems.

(72)

Divide-and-Conquer

Teile-und-Beherrsche

Teile-und-BeherrscheAlgorithmen (divide-and-conquer) teilen das Problem in mehrere Teilprobleme auf, die dem Ausgangsproblem ähneln, jedoch von kleinerer Grösse sind.

Sie lösen die Teilproblemerekursivund kombinieren diese Lösungen dann, um die Lösung des eigentlichen Problems zu erstellen.

Das Paradigma von Teile-und-Beherrsche umfasst 3 Schritte auf jeder Rekursionsebene:

Teile das Problem in eine Anzahl von Teilproblemen auf.

Beherrsche die Teilprobleme durch rekursives Lösen. Hinreichend kleine Teilprobleme werden direkt gelöst.

Verbinde die Lösungen der Teilprobleme zur Lösung des Ausgangsproblems.

DSAL/SS 2017 VL-06: Sortieren I 25/34

Referenzen

ÄHNLICHE DOKUMENTE

Element getMin(PriorityQueue pq) gibt das Element mit dem kleinsten Schlüssel zurück; benötigt nicht-leere pq. void delMin(PriorityQueue pq) entfernt das Element mit dem

Man kann einen neuen Knoten mit Schlüssel k in den BST t einfügen, ohne die BST-Eigenschaft zu zerstören:.. Suche einen geeigneten,

5 Für jeden Knoten x gilt: alle Pfade, die im Knoten x starten und in einem externen Knoten enden, enthalten die selbe Anzahl an schwarzen Knoten.. So ein Baum heisst

Daher: Falls Schlüssel auch gelöscht werden sollen, so wird häufiger Verkettung zur Kollisionsauflösung verwendet.... Löschen bei offener

und versuchen der Reihe nach die Kanten in den Baum einzufügen Falls Kante zwei Knoten in zwei verschiedenen. Zusammenhangskomponenten verbindet:

Lambert Kurt John Ivo Harry Greg Frank Edward Dirk Cor Bernhard Adrian. DSAL/SS 2017 VL-18:

Die Ford-Fulkerson Methode erweitert den Fluss in G sukzessive um augmentierende Pfade im Residualen Netzwerk G f bis es keine solche Pfade mehr gibt. Wir werden

Wenn eine Wahl getroffen wurde, kann sie nicht mehr rückgängig gemacht werden.. Greedy Algorithmen finden nicht immer die