3. Inkrementelle Algorithmen - uni-paderborn.deSS 2017 1 3. Inkrementelle Algorithmen Definition...
Transcript of 3. Inkrementelle Algorithmen - uni-paderborn.deSS 2017 1 3. Inkrementelle Algorithmen Definition...
SS 2017 1
3. Inkrementelle Algorithmen
Definition 3.1: Bei einem inkrementellen Algorithmus wird sukzessive die Teillösung für die ersten i Objekte aus der bereits bekannten Teillösung für die ersten i-1 Objekte berechnet, i=1,…,n.
Beispiel Min-Search:
Objekte sind Einträge des Eingabearrays.
Teilproblem bestehend aus ersten i Objektenbedeutet Minimum im Teilarray A[1..i] bestimmen.
DuA - Kapitel 3
SS 2017 2
Sortieren und inkrementelle Algorithmen
48,51,59)(26,31,41, :Ausgabe26,51,48)(31,41,59, :Eingabe
berechnet. )b,,b,(b Umordnung sortierte
)a,,a,(a Folge jeder zu das Verfahren, :orithmusSortieralg
.bbb dass so ge,Eingabefol der )b,,b,(b Umordnung :Sortieren beim Ausgabe
).a,,a,(a Zahlen n von Folge :Sortieren beim Eingabe
n21
n21
n21
n21
n21
≤≤≤
DuA - Kapitel 3
SS 2017 3
Insertion-Sort
keyiAii
iAiAkeyiAi
ji.jj
jAkey)A(lengthj
)A(
←+−←←+
>>−←
−←
←
]1[ 8 1 7
][]1[ 6 ][ and 0 5
1 4 ein 1] A[1.Folge sortierte die in ] A[Füge 3
][ 2 2 1
Sort-Insertion
dowhile
dotofor
.
Idee: Sukzessive wird eine Sortierung der Teilarrays A[1..i], 1≤i ≤length(A) berechnet.
DuA - Kapitel 3
4
Insertion SortInsertionSort(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: 8 15 3 14 7 6 18 19SS 2017 DuA - Kapitel 3
5
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. 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: 8 15 3 14 7 6 18 19SS 2017 DuA - Kapitel 3
6
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. 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: 8 15 3 14 7 6 18 191 n
SS 2017 DuA - Kapitel 3
7
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. 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: 8 15 3 14 7 6 18 19j1 n
SS 2017 DuA - Kapitel 3
8
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. 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: 8 15 3 14 7 6 18 191 nj
keySS 2017 DuA - Kapitel 3
9
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. 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: 8 15 3 14 7 6 18 19i nj
keySS 2017 DuA - Kapitel 3
10
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. 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: 8 15 3 14 7 6 18 19i nj
keySS 2017 DuA - Kapitel 3
11
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. 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: 8 15 3 14 7 6 18 19i nj
keySS 2017 DuA - Kapitel 3
12
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. 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: 8 15 3 14 7 6 18 191 nj
SS 2017 DuA - Kapitel 3
13
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. 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: 8 15 3 14 7 6 18 191 nj
keySS 2017 DuA - Kapitel 3
14
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. 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: 8 15 3 14 7 6 18 191 nj
key=3
i
SS 2017 DuA - Kapitel 3
15
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. 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: 8 15 3 14 7 6 18 191 nji
key=3SS 2017 DuA - Kapitel 3
16
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. 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: 8 15 15 14 7 6 18 191 nji
key=3SS 2017 DuA - Kapitel 3
17
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. 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: 8 15 15 14 7 6 18 19i nj
key=3SS 2017 DuA - Kapitel 3
18
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. 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: 8 15 15 14 7 6 18 19i nj
key=3SS 2017 DuA - Kapitel 3
19
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. 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: 8 8 15 14 7 6 18 19i nj
key=3SS 2017 DuA - Kapitel 3
20
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. 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: 8 8 15 14 7 6 18 191 nj
key=3
i
SS 2017 DuA - Kapitel 3
21
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. 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: 8 8 15 14 7 6 18 191 nj
key=3
i
SS 2017 DuA - Kapitel 3
22
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. 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: 3 8 15 14 7 6 18 191 nj
key=3SS 2017 DuA - Kapitel 3
23
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. 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: 3 8 15 14 7 6 18 191 nj
SortiertSS 2017 DuA - Kapitel 3
24
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. 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: 3 8 15 14 7 6 18 191 nj
Sortiert keySS 2017 DuA - Kapitel 3
25
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. 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: 3 8 15 14 7 6 18 191 nj
Sortiert key=14SS 2017 DuA - Kapitel 3
26
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. key ← A[j]3. i ← j-1 verschiebe alle 4. while i>0 and A[i]>key do A[1,..,j-1], die größer als 5. A[i+1] ← A[i] key sind eine Stelle 6. i ← i-1 nach rechts7. A[i+1] ← key
Beispiel: 3 8 15 14 7 6 18 191 nj
Sortiert key=14
j-1
SS 2017 DuA - Kapitel 3
27
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. key ← A[j]3. i ← j-1 verschiebe alle 4. while i>0 and A[i]>key do A[1,..,j-1], die größer als 5. A[i+1] ← A[i] key sind eine Stelle 6. i ← i-1 nach rechts7. A[i+1] ← key
Beispiel: 3 8 15 14 7 6 18 191 nj
Sortiert key=14
j-1
SS 2017 DuA - Kapitel 3
28
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. key ← A[j]3. i ← j-1 verschiebe alle 4. while i>0 and A[i]>key do A[1,..,j-1], die größer als 5. A[i+1] ← A[i] key sind eine Stelle 6. i ← i-1 nach rechts7. A[i+1] ← key
Beispiel: 3 8 15 15 7 6 18 191 nj
Sortiert key=14
j-1i
SS 2017 DuA - Kapitel 3
29
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. key ← A[j]3. i ← j-1 verschiebe alle 4. while i>0 and A[i]>key do A[1,..,j-1], die größer als5. A[i+1] ← A[i] key sind eine Stelle 6. i ← i-1 nach rechts7. A[i+1] ← key
Beispiel: 3 8 15 15 7 6 18 191 nj
key=14
i+1i
SS 2017 DuA - Kapitel 3
30
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. key ← A[j]3. i ← j-1 verschiebe alle 4. while i>0 and A[i]>key do A[1,..,j-1], die größer als5. A[i+1] ← A[i] key sind eine Stelle 6. i ← i-1 nach rechts7. A[i+1] ← key Speichere key in „Lücke“
Beispiel: 3 8 15 15 7 6 18 191 nj
key=14
i+1i
SS 2017 DuA - Kapitel 3
31
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. key ← A[j]3. i ← j-1 verschiebe alle 4. while i>0 and A[i]>key do A[1,..,j-1], die größer als5. A[i+1] ← A[i] key sind eine Stelle 6. i ← i-1 nach rechts7. A[i+1] ← key Speichere key in „Lücke“
Beispiel: 3 8 14 15 7 6 18 191 nj
key=14
i+1i
SS 2017 DuA - Kapitel 3
32
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. key ← A[j]3. i ← j-1 verschiebe alle 4. while i>0 and A[i]>key do A[1,..,j-1], die größer als5. A[i+1] ← A[i] key sind eine Stelle 6. i ← i-1 nach rechts7. A[i+1] ← key Speichere key in „Lücke“
Beispiel: 3 8 14 15 7 6 18 191 nj
SortiertSS 2017 DuA - Kapitel 3
33
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. key ← A[j]3. i ← j-1 verschiebe alle 4. while i>0 and A[i]>key do A[1,..,j-1], die größer als5. A[i+1] ← A[i] key sind eine Stelle 6. i ← i-1 nach rechts7. A[i+1] ← key Speichere key in „Lücke“
Beispiel: 3 8 14 15 7 6 18 191 nj
Sortiert key=7SS 2017 DuA - Kapitel 3
34
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. key ← A[j]3. i ← j-1 verschiebe alle 4. while i>0 and A[i]>key do A[1,..,j-1], die größer als5. A[i+1] ← A[i] key sind eine Stelle 6. i ← i-1 nach rechts7. A[i+1] ← key Speichere key in „Lücke“
Beispiel: 3 8 14 15 7 6 18 191 nj
Sortiert key=7SS 2017 DuA - Kapitel 3
35
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. key ← A[j]3. i ← j-1 verschiebe alle 4. while i>0 and A[i]>key do A[1,..,j-1], die größer als5. A[i+1] ← A[i] key sind eine Stelle 6. i ← i-1 nach rechts7. A[i+1] ← key Speichere key in „Lücke“
Beispiel: 3 8 8 14 15 6 18 191 nj
key=7SS 2017 DuA - Kapitel 3
36
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. key ← A[j]3. i ← j-1 verschiebe alle 4. while i>0 and A[i]>key do A[1,..,j-1], die größer als5. A[i+1] ← A[i] key sind eine Stelle 6. i ← i-1 nach rechts7. A[i+1] ← key Speichere key in „Lücke“
Beispiel: 3 8 8 14 15 6 18 191 nj
key=7SS 2017 DuA - Kapitel 3
37
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. key ← A[j]3. i ← j-1 verschiebe alle 4. while i>0 and A[i]>key do A[1,..,j-1], die größer als5. A[i+1] ← A[i] key sind eine Stelle 6. i ← i-1 nach rechts7. A[i+1] ← key Speichere key in „Lücke“
Beispiel: 3 7 8 14 15 6 18 191 nj
key=7SS 2017 DuA - Kapitel 3
38
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. key ← A[j]3. i ← j-1 verschiebe alle 4. while i>0 and A[i]>key do A[1,..,j-1], die größer als5. A[i+1] ← A[i] key sind eine Stelle 6. i ← i-1 nach rechts7. A[i+1] ← key Speichere key in „Lücke“
Beispiel: 3 7 8 14 15 6 18 191 nj
SortiertSS 2017 DuA - Kapitel 3
39
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. key ← A[j]3. i ← j-1 verschiebe alle 4. while i>0 and A[i]>key do A[1,..,j-1], die größer als5. A[i+1] ← A[i] key sind eine Stelle 6. i ← i-1 nach rechts7. A[i+1] ← key Speichere key in „Lücke“
Beispiel: 3 7 8 14 15 6 18 191 n
key=6
j
SortiertSS 2017 DuA - Kapitel 3
40
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. key ← A[j]3. i ← j-1 verschiebe alle 4. while i>0 and A[i]>key do A[1,..,j-1], die größer als5. A[i+1] ← A[i] key sind eine Stelle 6. i ← i-1 nach rechts7. A[i+1] ← key Speichere key in „Lücke“
Beispiel: 3 7 8 14 15 6 18 191 nj
key=6SS 2017 DuA - Kapitel 3
41
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. key ← A[j]3. i ← j-1 verschiebe alle 4. while i>0 and A[i]>key do A[1,..,j-1], die größer als5. A[i+1] ← A[i] key sind eine Stelle 6. i ← i-1 nach rechts7. A[i+1] ← key Speichere key in „Lücke“
Beispiel: 3 7 7 8 14 15 18 191 nj
key=6SS 2017 DuA - Kapitel 3
42
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. key ← A[j]3. i ← j-1 verschiebe alle 4. while i>0 and A[i]>key do A[1,..,j-1], die größer als5. A[i+1] ← A[i] key sind eine Stelle 6. i ← i-1 nach rechts7. A[i+1] ← key Speichere key in „Lücke“
Beispiel: 3 7 7 8 14 15 18 191 nj
key=6SS 2017 DuA - Kapitel 3
43
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. key ← A[j]3. i ← j-1 verschiebe alle 4. while i>0 and A[i]>key do A[1,..,j-1], die größer als5. A[i+1] ← A[i] key sind eine Stelle 6. i ← i-1 nach rechts7. A[i+1] ← key Speichere key in „Lücke“
Beispiel: 3 6 7 8 14 15 18 191 nj
key=6SS 2017 DuA - Kapitel 3
44
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. key ← A[j]3. i ← j-1 verschiebe alle 4. while i>0 and A[i]>key do A[1,..,j-1], die größer als5. A[i+1] ← A[i] key sind eine Stelle 6. i ← i-1 nach rechts7. A[i+1] ← key Speichere key in „Lücke“
Beispiel: 3 6 7 8 14 15 18 191 nj
SortiertSS 2017 DuA - Kapitel 3
45
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. key ← A[j]3. i ← j-1 verschiebe alle 4. while i>0 and A[i]>key do A[1,..,j-1], die größer als5. A[i+1] ← A[i] key sind eine Stelle 6. i ← i-1 nach rechts7. A[i+1] ← key Speichere key in „Lücke“
Beispiel: 3 6 7 8 14 15 18 191 nj
SS 2017 DuA - Kapitel 3
46
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. key ← A[j]3. i ← j-1 verschiebe alle 4. while i>0 and A[i]>key do A[1,..,j-1], die größer als5. A[i+1] ← A[i] key sind eine Stelle 6. i ← i-1 nach rechts7. A[i+1] ← key Speichere key in „Lücke“
Beispiel: 3 6 7 8 14 15 18 191 n
SortiertSS 2017 DuA - Kapitel 3
47
Insertion SortInsertionSort(Array A) Eingabegröße n 1. for j ← 2 to length(A) do (length(A)=n)2. key ← A[j]3. i ← j-1 verschiebe alle 4. while i>0 and A[i]>key do A[1,..,j-1], die größer als5. A[i+1] ← A[i] key sind eine Stelle 6. i ← i-1 nach rechts7. A[i+1] ← key Speichere key in „Lücke“
Beispiel: 3 6 7 8 14 15 18 191 n
SortiertSS 2017 DuA - Kapitel 3
SS 2017 48
Invariante bei Insertion-Sort
• A0[i] : anfänglicher i-ter Wert von Array A• A[i] : aktueller i-ter Wert von Array A
Invariante I(j): A[1..j-1] enthält die Zahlen in A0[1..j-1] in sortierter Reihenfolge.
Initialisierung: anfangs gilt offensichtlich I(2) (nur das erste Element ist sortiert) und damit auch I(j) für j=2.
Erhaltung: wenn am Anfang der for-Schleife I(j) gilt, dann gilt am Ende der for-Schleife I(j+1).
Terminierung: Am Ende gilt I(length(A)+1) d.h.A[1. . length(A)] enthält die sortierten Zahlen in A0[1..length(A)], woraus die Korrektheit folgt.
DuA - Kapitel 3
SS 2017 49
Invariante bei Insertion-Sort
Satz 3.2• Insertion-Sort sortiert eine Folge von n Zahlen aufsteigend.Beweis:• Wir zeigen, dass die Schleifeninvariante I(j) alle
Anforderungen erfüllt.• Die Initialisierungs- und Terminierungsanforderungen der
Schleifeninvariante I(j) gelten offensichtlich (sofern die Erhaltung gilt).
• Es bleibt, die Erhaltung der Schleifeninvariante zu beweisen.
DuA - Kapitel 3
SS 2017 50
Invariante bei Insertion-Sort
Erhaltung (j→j+1):• Angenommen I(j) gelte am Anfang der Schleife für ein j• Insertionsort merkt sich A[j] in Variable key• Sei 1≤k≤j-1 der kleinste Index mit A[k]>key oder
k=j, falls ein solcher nicht existiert• Der Algorithmus verschiebt A[k,..,j-1] nach A[k+1,..,j]• Dann wird A[k] auf den Wert key gesetzt• Danach gilt:
(1) A[1]≤A[2]≤…≤A[k-1] wegen Annahme I(j) oben (2) A[k-1] ≤ A[k] ≤ A[k+1] nach Ablauf der Schleife(3) A[k+1] ≤ A[k+2] ≤…≤ A[j] wegen Annahme I(j) oben
• Aus (1)-(3) folgt I(j+1) und damit die Erhaltung.
Formale Analyse (nächste Seite):
DuA - Kapitel 3
SS 2017 51
Invariante bei Insertion-SortInsertion-Sort(A)1 I(2)
for j←2 to length(A) do I(j)
2 key←A[j] I(j) ⋀ key=A0[j]
3 i←j-1 ( I(j,i): A[1..i,i+2..j] enthält sortierte Zahlen in A0[1..j-1] ) ⋀ key=A0[j]
4 while i>0 and A[i]>key do I(j,i) ⋀ key=A0[j] ⋀ key<A[i] ⋀ i>0
5 A[i+1]←A[i] I(j,i-1) ⋀ key=A0[j] ⋀ key<A[i+1] ⋀ i>0
6 i←i-1 I(j,i) ⋀ key=A0[j] ⋀ key<A[i+2] ⋀ i≥0
Fall (a): i=0 ⇒ I(j,0) ⋀ key=A0[j] ⋀ key<A[2] Fall (b): A[i]≤key ⇒ I(j,i) ⋀ key=A0[j] ⋀ A[i]≤key<A[i+2]
7 A[i+1]←key I(j+1)
I(length[A]+1)⇒ A[1..length[A]] enthält die sortierten Zahlen von A0[1..length[A]]
Initialisierung
d.h. Algo korrekt
Erhaltung
Terminierung
DuA - Kapitel 3
j=2
SS 2017 DuA - Kapitel 3 52
InsertionSort(Array A) Laufzeit1. for j ← 2 to length(A) do Σj=2
n T(I)2. key ← A[j] O(1)3. i ← j-1 O(1)4. while i>0 and A[i]>key do ≤ Σi=1
j-1 T(I)5. A[i+1] ← A[i] O(1)6. i ← i-1 O(1)7. A[i+1] ← key O(1)
Laufzeit: O(Σj=2n ((Σi=1
j-1 1) +1)) = O(n2)
Insertion-Sort – Analyse (1)
verwendeΦ(i)=i
SS 2017 DuA - Kapitel 3 53
Satz 3.3: Insertion-Sort hat eine worst-caseLaufzeit von Θ(n2).
Beweis:• O(n2): schon gezeigt• Ω(n2): worst-case Folge ist absteigend sortierte
Folge, da in diesem Fall die while-Schleife j-1-mal für jedes j durchlaufen wird.
Insertion-Sort – Analyse (2)