-
SS 2016 DuA - Kapitel 9 1
9. Sortieren in linearer Zeit
Es gibt Algorithmen, die die Ω(nlog(n)) untere Schrankefür
Vergleichssortierer schlagen.
Diese Algorithmen benutzen neben Vergleichen
undKontrolloperationen noch andere Operationen wie
z.B.arithmetische Operationen auf den zu sortierendenZahlen.
Allerdings benötigen diese Algorithmen gewisseEinschränkungen,
um eine Laufzeit von o(n log n) zuerzielen.
Zu diesen Algorithmen gehören Counting-Sort,Bucket-Sort und
Radix-Sort.
-
SS 2016 DuA - Kapitel 9 2
Sortieren durch Abzählen (1)Annahme: Es gibt ein dem Algorithmus
Counting-Sort bekannter Parameter k, so dass für die Eingabefolge (
)na,,a 1 gilt: ni k ai ≤≤≤≤ 1alle für0 . Algorithmusidee:
1. Für alle ki,i ≤≤0 bestimme Anzahl iC der iaa jj ≤ mit .
2. Kopiere iaa jj = mit in Felder [ ] [ ]ii CB,,CB 11 +− eines
Arrays B mit length[B]=n. Dabei gilt 01 =−C .
Es gilt: 1−− ii CC ist die Anzahl der iaa jj = mit .
Annahme: Es gibt ein dem Algorithmus Counting-Sort bekannter
Parameter k, so dass für die Eingabefolge
(
)
n
a
,
,
a
K
1
gilt:
n
i
k
a
i
£
£
£
£
1
alle
für
0
.
Algorithmusidee:
1. Für alle
k
i
,
i
£
£
0
bestimme Anzahl
i
C
der
i
a
a
j
j
£
mit
.
2. Kopiere
i
a
a
j
j
=
mit
in Felder
[
]
[
]
i
i
C
B
,
,
C
B
K
1
1
+
-
eines Arrays B mit length[B]=n. Dabei gilt
0
1
=
-
C
.
Es gilt:
1
-
-
i
i
C
C
ist die Anzahl der
i
a
a
j
j
=
mit
.
_1175420433.unknown
_1175420964.unknown
_1175421013.unknown
_1175421095.unknown
_1175420973.unknown
_1175420713.unknown
_1175420887.unknown
_1175420070.unknown
_1175420413.unknown
_1175419901.unknown
-
SS 2016 DuA - Kapitel 9 3
Sortieren durch Abzählen (2)
1a 2a 3a 4a … … 1−na naA
… … …. …
10C 11 +−iC iC kC
iaa jj = mit 0 mit =jj aa
B
-
SS 2016 DuA - Kapitel 9 4
Counting-Sort
Counting-Sort(A,B,k)
1. for i←0 to k do2. C[i]←03. for j←1 to length(A) do4.
C[A[j]]←C[A[j]]+15. C[i] enthält Anzahl der Elemente in A mit Wert
i6. for i←1 to k do7. C[i]←C[i]+C[i-1]8. C[i] enthält Anzahl der
Elemente in A mit Wert ≤i9. for j←length(A) downto 1 do10.
B[C[A[j]]]←A[j]11. C[A[j]]←C[A[j]]-1
-
SS 2016 DuA - Kapitel 9 5
Illustration für Counting-Sort (1)
A 21
5
2
3
3
0
4
2
5
3
6
0
7
3
8
C(nach Zeilen 3-4)
0
1
0
2
0
3
0
4
0
5
0
0
C(nach Zeilen 6-7)
-
SS 2016 DuA - Kapitel 9 6
Illustration für Counting-Sort (1)
A 21
5
2
3
3
0
4
2
5
3
6
0
7
3
8
C(nach Zeilen 3-4)
0
1
0
2
0
3
0
4
0
5
0
0
C(nach Zeilen 6-7)
-
SS 2016 DuA - Kapitel 9 7
Illustration für Counting-Sort (1)
A 21
5
2
3
3
0
4
2
5
3
6
0
7
3
8
C(nach Zeilen 3-4)
0
1
1
2
0
3
0
4
0
5
0
0
C(nach Zeilen 6-7)
-
SS 2016 DuA - Kapitel 9 8
Illustration für Counting-Sort (1)
A 21
5
2
3
3
0
4
2
5
3
6
0
7
3
8
C(nach Zeilen 3-4)
0
1
1
2
0
3
0
4
0
5
0
0
C(nach Zeilen 6-7)
-
SS 2016 DuA - Kapitel 9 9
Illustration für Counting-Sort (1)
A 21
5
2
3
3
0
4
2
5
3
6
0
7
3
8
C(nach Zeilen 3-4)
0
1
1
2
0
3
0
4
1
5
0
0
C(nach Zeilen 6-7)
-
SS 2016 DuA - Kapitel 9 10
Illustration für Counting-Sort (1)
A 21
5
2
3
3
0
4
2
5
3
6
0
7
3
8
C(nach Zeilen 3-4)
0
1
1
2
0
3
0
4
1
5
0
0
C(nach Zeilen 6-7)
-
SS 2016 DuA - Kapitel 9 11
Illustration für Counting-Sort (1)
A 21
5
2
3
3
0
4
2
5
3
6
0
7
3
8
C(nach Zeilen 3-4)
0
1
1
2
1
3
0
4
1
5
0
0
C(nach Zeilen 6-7)
-
SS 2016 DuA - Kapitel 9 12
Illustration für Counting-Sort (1)
A 21
5
2
3
3
0
4
2
5
3
6
0
7
3
8
C(nach Zeilen 3-4)
0
1
1
2
1
3
0
4
1
5
0
0
C(nach Zeilen 6-7)
-
SS 2016 DuA - Kapitel 9 13
Illustration für Counting-Sort (1)
A 21
5
2
3
3
0
4
2
5
3
6
0
7
3
8
C(nach Zeilen 3-4)
0
1
1
2
1
3
0
4
1
5
1
0
C(nach Zeilen 6-7)
-
SS 2016 DuA - Kapitel 9 14
Illustration für Counting-Sort (1)
A 21
5
2
3
3
0
4
2
5
3
6
0
7
3
8
C(nach Zeilen 3-4)
0
1
1
2
1
3
0
4
1
5
1
0
C(nach Zeilen 6-7)
-
SS 2016 DuA - Kapitel 9 15
Illustration für Counting-Sort (1)
A 21
5
2
3
3
0
4
2
5
3
6
0
7
3
8
C(nach Zeilen 3-4)
0
1
2
2
1
3
0
4
1
5
1
0
C(nach Zeilen 6-7)
-
SS 2016 DuA - Kapitel 9 16
Illustration für Counting-Sort (1)
A 21
5
2
3
3
0
4
2
5
3
6
0
7
3
8
C(nach Zeilen 3-4)
0
1
2
2
1
3
0
4
1
5
1
0
C(nach Zeilen 6-7)
-
SS 2016 DuA - Kapitel 9 17
Illustration für Counting-Sort (1)
A 21
5
2
3
3
0
4
2
5
3
6
0
7
3
8
C(nach Zeilen 3-4)
0
1
2
2
2
3
0
4
1
5
1
0
C(nach Zeilen 6-7)
-
SS 2016 DuA - Kapitel 9 18
Illustration für Counting-Sort (1)
A 21
5
2
3
3
0
4
2
5
3
6
0
7
3
8
C(nach Zeilen 3-4)
0
1
2
2
2
3
0
4
1
5
1
0
C(nach Zeilen 6-7)
-
SS 2016 DuA - Kapitel 9 19
Illustration für Counting-Sort (1)
A 21
5
2
3
3
0
4
2
5
3
6
0
7
3
8
C(nach Zeilen 3-4)
0
1
2
2
2
3
0
4
1
5
2
0
C(nach Zeilen 6-7)
-
SS 2016 DuA - Kapitel 9 20
Illustration für Counting-Sort (1)
A 21
5
2
3
3
0
4
2
5
3
6
0
7
3
8
C(nach Zeilen 3-4)
0
1
2
2
2
3
0
4
1
5
2
0
C(nach Zeilen 6-7)
-
SS 2016 DuA - Kapitel 9 21
Illustration für Counting-Sort (1)
A 21
5
2
3
3
0
4
2
5
3
6
0
7
3
8
C(nach Zeilen 3-4)
0
1
2
2
3
3
0
4
1
5
2
0
C(nach Zeilen 6-7)
-
SS 2016 DuA - Kapitel 9 22
Illustration für Counting-Sort (1)
A 21
5
2
3
3
0
4
2
5
3
6
0
7
3
8
C(nach Zeilen 3-4)
0
1
2
2
3
3
0
4
1
5
2
0
0
1
2
2
3
3
0
4
1
5
2
0
C(nach Zeilen 6-7)
-
SS 2016 DuA - Kapitel 9 23
Illustration für Counting-Sort (1)
A 21
5
2
3
3
0
4
2
5
3
6
0
7
3
8
C(nach Zeilen 3-4)
0
1
2
2
3
3
0
4
1
5
2
0
2
1
2
2
3
3
0
4
1
5
2
0
C(nach Zeilen 6-7)
-
SS 2016 DuA - Kapitel 9 24
Illustration für Counting-Sort (1)
A 21
5
2
3
3
0
4
2
5
3
6
0
7
3
8
C(nach Zeilen 3-4)
0
1
2
2
3
3
0
4
1
5
2
0
2
1
4
2
3
3
0
4
1
5
2
0
C(nach Zeilen 6-7)
-
SS 2016 DuA - Kapitel 9 25
Illustration für Counting-Sort (1)
A 21
5
2
3
3
0
4
2
5
3
6
0
7
3
8
C(nach Zeilen 3-4)
0
1
2
2
3
3
0
4
1
5
2
0
2
1
4
2
7
3
0
4
1
5
2
0
C(nach Zeilen 6-7)
-
SS 2016 DuA - Kapitel 9 26
Illustration für Counting-Sort (1)
A 21
5
2
3
3
0
4
2
5
3
6
0
7
3
8
C(nach Zeilen 3-4)
0
1
2
2
3
3
0
4
1
5
2
0
2
1
4
2
7
3
7
4
1
5
2
0
C(nach Zeilen 6-7)
-
SS 2016 DuA - Kapitel 9 27
Illustration für Counting-Sort (1)
A 21
5
2
3
3
0
4
2
5
3
6
0
7
3
8
C(nach Zeilen 3-4)
0
1
2
2
3
3
0
4
1
5
2
0
2
1
4
2
7
3
7
4
8
5
2
0
C(nach Zeilen 6-7)
-
SS 2016 DuA - Kapitel 9 28
Illustration für Counting-Sort (1)
A 21
5
2
3
3
0
4
2
5
3
6
0
7
3
8
C(nach Zeilen 3-4)
0
1
2
2
3
3
0
4
1
5
2
0
2
1
4
2
7
3
7
4
8
5
2
0
C(nach Zeilen 6-7)
-
SS 2016 DuA - Kapitel 9 29
Illustration für Counting-Sort (2)
A 21
52
33
04
25
36
07
38
C 21
42
73
74
85
20
B1 2 3 4 5 6 7 8
C 21
42
73
74
85
20
-
SS 2016 DuA - Kapitel 9 30
Illustration für Counting-Sort (2)
A 21
52
33
04
25
36
07
38
C 21
42
73
74
85
20
B1 2 3 4 5 6 7 8
C 21
42
73
74
85
20
-
SS 2016 DuA - Kapitel 9 31
Illustration für Counting-Sort (2)
A 21
52
33
04
25
36
07
38
C 21
42
73
74
85
20
B1 2 3 4 5 6
37 8
C 21
42
63
74
85
20
-
SS 2016 DuA - Kapitel 9 32
Illustration für Counting-Sort (2)
A 21
52
33
04
25
36
07
38
C 21
42
63
74
85
20
B1 2 3 4 5 6
37 8
C 21
42
63
74
85
20
-
SS 2016 DuA - Kapitel 9 33
Illustration für Counting-Sort (2)
A 21
52
33
04
25
36
07
38
C 21
42
63
74
85
20
B1 2 3 4 5 6
37 8
C 21
42
63
74
85
10
0
-
SS 2016 DuA - Kapitel 9 34
Illustration für Counting-Sort (2)
A 21
52
33
04
25
36
07
38
B1 2 3 4 5 6
37 8
C 21
42
63
74
85
10
0
-
SS 2016 DuA - Kapitel 9 35
Illustration für Counting-Sort (2)
A 21
52
33
04
25
36
07
38
B1 2 3 4 5 6
37 8
C 21
42
53
74
85
10
0 3
-
SS 2016 DuA - Kapitel 9 36
Illustration für Counting-Sort (2)
A 21
52
33
04
25
36
07
38
B1 2 3 4 5 6
37 8
C 21
42
53
74
85
10
0 3
-
SS 2016 DuA - Kapitel 9 37
Illustration für Counting-Sort (2)
A 21
52
33
04
25
36
07
38
B1 2 3 4 5 6
37 8
C 21
32
53
74
85
10
0 2 3
-
SS 2016 DuA - Kapitel 9 38
Illustration für Counting-Sort (2)
A 21
52
33
04
25
36
07
38
B1 2 3 4 5 6
37 8
C 21
32
53
74
85
10
0 2 3
-
SS 2016 DuA - Kapitel 9 39
Illustration für Counting-Sort (2)
A 21
52
33
04
25
36
07
38
B1 2 3 4 5 6
37 8
C 21
32
53
74
85
00
0 2 30
-
SS 2016 DuA - Kapitel 9 40
Illustration für Counting-Sort (2)
A 21
52
33
04
25
36
07
38
B1 2 3 4 5 6
37 8
C 21
32
53
74
85
00
0 2 30
-
SS 2016 DuA - Kapitel 9 41
Illustration für Counting-Sort (2)
A 21
52
33
04
25
36
07
38
B1 2 3 4 5 6
37 8
C 21
32
43
74
85
00
0 2 30 3
-
SS 2016 DuA - Kapitel 9 42
Illustration für Counting-Sort (2)
A 21
52
33
04
25
36
07
38
B1 2 3 4 5 6
37 8
C 21
32
43
74
85
00
0 2 30 3
-
SS 2016 DuA - Kapitel 9 43
Illustration für Counting-Sort (2)
A 21
52
33
04
25
36
07
38
B1 2 3 4 5 6
37 8
C 21
32
43
74
75
00
0 2 30 3 5
-
SS 2016 DuA - Kapitel 9 44
Illustration für Counting-Sort (2)
A 21
52
33
04
25
36
07
38
B1 2 3 4 5 6
37 8
C 21
32
43
74
75
00
0 2 30 3 5
-
SS 2016 DuA - Kapitel 9 45
Illustration für Counting-Sort (2)
A 21
52
33
04
25
36
07
38
B1 2 3 4 5 6
37 8
C 21
22
43
74
75
00
0 2 30 3 52
-
SS 2016 DuA - Kapitel 9 46
Illustration für Counting-Sort (2)
A 21
52
33
04
25
36
07
38
B1 2 3 4 5 6
37 8
C 21
22
43
74
75
00
0 2 30 3 52
-
SS 2016 DuA - Kapitel 9 47
Laufzeit von Counting-Sort
Zeilen 1-2, 6-7: jeweils O(k)
Zeilen 3-4, 9-11: jeweils O(n)
Counting-Sort(A,B,k)
1. for i←0 to k do2. C[i]←03. for j←1 to length(A) do4.
C[A[j]]←C[A[j]]+15. C[i] enthält Anzahl der Elemente in A mit Wert
i6. for i←1 to k do7. C[i]←C[i]+C[i-1]8. C[i] enthält Anzahl der
Elemente in A mit Wert ≤i9. for j←length(A) downto 1 do10.
B[C[A[j]]]←A[j]11. C[A[j]]←C[A[j]]-1
-
SS 2016 DuA - Kapitel 9 48
Laufzeit von Counting-SortSatz 9.1: Counting-Sort besitzt
Laufzeit O(n+k).
Korollar 9.2: Gilt k= O(n), so besitzt Counting-SortLaufzeit
O(n).
-
SS 2016 DuA - Kapitel 9 49
Korrektheit von Counting-SortCounting-Sort(A,B,k)
1. for i←0 to k do2. C[i]←0
3. for j←1 to length(A) do4. C[A[j]]←C[A[j]]+15. C[i] enthält
Anzahl der Elemente in A mit Wert i
6. for i←1 to k do7. C[i]←C[i]+C[i-1]8. C[i] enthält Anzahl der
Elemente in A mit Wert ≤i
9. for j←length(A) downto 1 do10. B[C[A[j]]]←A[j]11.
C[A[j]]←C[A[j]]-1
I1(j): ∀i∈{0,…,length(A)}: C[i]= #i in A[1,…,j-1]
I2(j): ∀j∈{0,…,i-1}: C[j]= #≤j in A und ∀j∈{i,…,k}: C[j]= #j in
A
I3(j): ∀i∈{j+1,…,length(A)}: Pos. von A[i] in B ist gleich |{
1≤l≤length(A) | A[l]
-
SS 2016 DuA - Kapitel 9 50
Korrektheit von Counting-SortCounting-Sort(A,B,k)
1. for i←0 to k do2. C[i]←03. for j←1 to length(A) do4.
C[A[j]]←C[A[j]]+15. C[i] enthält Anzahl der Elemente in A mit Wert
i6. for i←1 to k do7. C[i]←C[i]+C[i-1]8. C[i] enthält Anzahl der
Elemente in A mit Wert ≤i9. for j←length(A) downto 1 do10.
B[C[A[j]]]←A[j]11. C[A[j]]←C[A[j]]-1
Am Ende gilt: ∀i∈{1,…,length(A)}: Pos. von A[i] in B ist
gleich|{ 1≤l≤length(A) | A[l]
-
SS 2016 DuA - Kapitel 9 51
Korrektheit von Counting-Sort
A 21
52
33
04
25
36
07
38
B1 2 3 4 5 6
37 8
C 21
22
43
74
75
00
0 2 30 3 52
Am Ende gilt: ∀i∈{1,…,length(A)}: Pos. von A[i] in B ist
gleich|{ 1≤l≤length(A) | A[l]
-
SS 2016 DuA - Kapitel 9 52
Radixsort
Ideen: • verwende k-adische Darstellung der Schlüssel• Sortiere
Ziffer für Ziffer gemäß Counting-Sort• Behalte bei Betrachtung der
i-ten Ziffer die
Reihenfolge der Elemente mit gleicher i-terZiffer bei (s.
Countingsort)
Annahme: für alle Zahlen z: 0≤z
-
SS 2016 DuA - Kapitel 9 53
Radixsort
Radixsort(A,k)
1. for i←0 to d-1 do2. Counting-Sort(A,B,i,k-1) 3. sortiere
gemäß der Ai[j]-Werte, wobei Ai[j]
die i-te Ziffer in A[j] angibt
Laufzeit: O(d(n+k))k=n und d konstant: Laufzeit O(n).
-
SS 2016 DuA - Kapitel 9 54
Radixsort
Counting-Sort(A,B,i,k)
1. for i←0 to k do2. C[i]←03. for j←0 to length(A) do4.
C[Ai[j]]←C[Ai[j]]+15. C[i] enthält Anzahl der Elemente in A mit
Wert i6. for i←1 to k do7. C[i]←C[i]+C[i-1]8. C[i] enthält Anzahl
der Elemente in A mit Wert ≤i9. for j←length(A) downto 1 do10.
B[C[Ai[j]]]←A[j]11. C[Ai[j]]←C[Ai[j]]-1
-
SS 2016 DuA - Kapitel 9 55
Radixsort
Beispiel:
Nach Counting-Sort(A,B,0,9):
12 203 3 74 24 17 112
12 112 3203 74 24 17
-
SS 2016 DuA - Kapitel 9 56
Radixsort
Beispiel:
Nach Counting-Sort(A,B,1,9):
12 112 203 3 74 24 17
203 03 11212 17 24 74
-
SS 2016 DuA - Kapitel 9 57
Radixsort
Beispiel:
Nach Counting-Sort(A,B,1,9):
203 3 12 112 17 24 74
003 012 024017 074 112 203
Zauberei???
-
SS 2016 DuA - Kapitel 9 58
Radixsort
Korrektheit:• Für jedes Paar a
-
SS 2016 DuA - Kapitel 9 59
Changelog
09.05.16: Folien 4, 47, 49 (neu), 50 (neu), 51 (neu), 52
10.05.16: Folien 49, 50, 51, 58
9. Sortieren in linearer Zeit Sortieren durch Abzählen (1)
Sortieren durch Abzählen (2)Counting-SortIllustration für
Counting-Sort (1)Illustration für Counting-Sort (1)Illustration für
Counting-Sort (1)Illustration für Counting-Sort (1)Illustration für
Counting-Sort (1)Illustration für Counting-Sort (1)Illustration für
Counting-Sort (1)Illustration für Counting-Sort (1)Illustration für
Counting-Sort (1)Illustration für Counting-Sort (1)Illustration für
Counting-Sort (1)Illustration für Counting-Sort (1)Illustration für
Counting-Sort (1)Illustration für Counting-Sort (1)Illustration für
Counting-Sort (1)Illustration für Counting-Sort (1)Illustration für
Counting-Sort (1)Illustration für Counting-Sort (1)Illustration für
Counting-Sort (1)Illustration für Counting-Sort (1)Illustration für
Counting-Sort (1)Illustration für Counting-Sort (1)Illustration für
Counting-Sort (1)Illustration für Counting-Sort (1)Illustration für
Counting-Sort (2)Illustration für Counting-Sort (2)Illustration für
Counting-Sort (2)Illustration für Counting-Sort (2)Illustration für
Counting-Sort (2)Illustration für Counting-Sort (2)Illustration für
Counting-Sort (2)Illustration für Counting-Sort (2)Illustration für
Counting-Sort (2)Illustration für Counting-Sort (2)Illustration für
Counting-Sort (2)Illustration für Counting-Sort (2)Illustration für
Counting-Sort (2)Illustration für Counting-Sort (2)Illustration für
Counting-Sort (2)Illustration für Counting-Sort (2)Illustration für
Counting-Sort (2)Illustration für Counting-Sort (2)Laufzeit von
Counting-SortLaufzeit von Counting-SortKorrektheit von
Counting-SortKorrektheit von Counting-SortKorrektheit von
Counting-SortRadixsortRadixsortRadixsortRadixsortRadixsortRadixsortRadixsortChangelog