Präsentation herunterladen
Die Präsentation wird geladen. Bitte warten
Veröffentlicht von:Kriemhild Neidhart Geändert vor über 9 Jahren
1
Selection-Sort Insertion-Sort Bubble-Sort
2
Selection-Sort
3
Selection-Sort-Beschreibung
Suche das kleinste Element aus der unsortierten Liste und tausche es mit dem Element an der nullten Position. Nun besteht die Liste aus einem sortierten Teil an der nullten Position und einem unsortierten Teil an den anderen Positionen. Suche nun stets das kleinste Element aus der unsortierten Teilliste und tausche es mit dem nullten Element der unsortierten Teilliste. Die sortierte Teilliste ist damit um ein Element gewachsen. Wiederhole diesen Vorgang (n-1) mal.
4
Selection-Sort j = 0 3 2 6 1 7 4
5
Selection-Sort j = 0 3 2 6 1 7 4 min = 0
6
Selection-Sort j = 0 i = 1 3 2 6 1 7 4 min = 0
7
Selection-Sort j = 0 i = 1 3 2 6 1 7 4 min = 1
8
Selection-Sort j = 0 i = 2 3 2 6 1 7 4 min = 1
9
Selection-Sort j = 0 i = 3 3 2 6 1 7 4 min = 1
10
Selection-Sort j = 0 i = 3 3 2 6 1 7 4 min = 3
11
Selection-Sort j = 0 i = 4 3 2 6 1 7 4 min = 3
12
Selection-Sort j = 0 i = 5 3 2 6 1 7 4 min = 3
13
Selection-Sort j = 0 i = 5 3 2 6 1 7 4 min = 3 tausche (a[j], a[min])
14
Selection-Sort j = 0 1 2 6 3 7 4 sortiert
15
Selection-Sort j = 1 1 2 6 3 7 4 sortiert
16
Selection-Sort j = 1 1 2 6 3 7 4 sortiert min = 1
17
Selection-Sort j = 1 i = 2 1 2 6 3 7 4 sortiert min = 1
18
Selection-Sort j = 1 i = 3 1 2 6 3 7 4 sortiert min = 1
19
Selection-Sort j = 1 i = 4 1 2 6 3 7 4 sortiert min = 1
20
Selection-Sort j = 1 i = 5 1 2 6 3 7 4 sortiert min = 1
21
1 2 6 3 7 4 tausche (a[j], a[min]) Selection-Sort j = 1 i = 5 sortiert
22
Selection-Sort j = 1 1 2 6 3 7 4 sortiert
23
Selection-Sort j = 2 1 2 6 3 7 4 sortiert
24
Selection-Sort j = 2 1 2 6 3 7 4 sortiert min = 2
25
Selection-Sort j = 2 i = 3 1 2 6 3 7 4 sortiert min = 2
26
Selection-Sort j = 2 i = 3 1 2 6 3 7 4 sortiert min = 3
27
Selection-Sort j = 2 i = 4 1 2 6 3 7 4 sortiert min = 3
28
Selection-Sort j = 2 i = 5 1 2 6 3 7 4 sortiert min = 3
29
1 2 6 3 7 4 tausche (a[j], a[min]) Selection-Sort j = 2 i = 5 sortiert
30
Selection-Sort j = 2 1 2 3 6 7 4 sortiert
31
Selection-Sort j = 3 1 2 3 6 7 4 sortiert
32
Selection-Sort j = 3 1 2 3 6 7 4 sortiert min = 3
33
Selection-Sort j = 3 i = 4 1 2 3 6 7 4 sortiert min = 3
34
Selection-Sort j = 3 i = 5 1 2 3 6 7 4 sortiert min = 3
35
Selection-Sort j = 3 i = 5 1 2 3 6 7 4 sortiert min = 5
36
1 2 3 6 7 4 tausche (a[j], a[min]) Selection-Sort j = 3 i = 5 sortiert
37
Selection-Sort j = 3 1 2 3 4 7 6 sortiert
38
Selection-Sort j = 4 1 2 3 4 7 6 sortiert
39
Selection-Sort j = 4 1 2 3 4 7 6 sortiert min = 4
40
Selection-Sort j = 4 i = 5 1 2 3 4 7 6 sortiert min = 4
41
Selection-Sort j = 4 i = 5 1 2 3 4 7 6 sortiert min = 5
42
1 2 3 4 7 6 tausche (a[j], a[min]) Selection-Sort j = 4 i = 5 sortiert
43
Selection-Sort j = 4 1 2 3 4 6 7 sortiert
44
Selection-Sort j = 4 1 2 3 4 6 7 sortiert
45
Selection-Sort Nassi-Shneiderman-Diagramm
j = 0 solange j < n-1 min = j i = j+1 solange i < n a[i] < a[min] ja nein min = i i++ tausche a[j] mit a[min] j++
46
Selection-Sort Java-Code (Teil1)
public class SelectionSortVersion1 { public static void main(String[] args) { int[] a = {3,2,6,1,7,4}; zeigeArray(a); int min = 0; for (int j=0; j<a.length-1; j++) { min = j; for (int i=j+1; i<a.length; i++ ) { if (a[i]<a[min]) { min = i; } tausche(a,j,min); } // end of main
47
Selection-Sort Java-Code (Teil2)
public static void zeigeArray(int[] x) { for (int i=0;i<x.length ;i++ ) { System.out.print(x[i]+" "); } System.out.println(); public static void tausche(int[] x, int pos1, int pos2) { int dummy = x[pos1]; x[pos1] = x[pos2]; x[pos2] = dummy; } // end of class
48
public static void zeigeArray(int[] x) {
for (int i=0;i<x.length ;i++ ) { System.out.print(x[i]+" "); } System.out.println(); public static void tausche(int[] x, int pos1, int pos2) { int dummy = x[pos1]; x[pos1] = x[pos2]; x[pos2] = dummy;
49
Selection-Sort Insertion-Sort Bubble-Sort
50
Insertion-Sort
51
Beschreibung Insertion-Sort
Betrachte das nullte Element als sortiert. Wähle das nächste Element und füge es an die richtige Stelle innerhalb des sortierten Teilbereichs ein. Wiederhole den 2. Schritt solange, bis der unsortierte Teilbereich leer ist.
52
Insertion-Sort 4 2 3 6 1 5
53
Insertion-Sort 4 3 6 1 5 e = 2
54
Insertion-Sort 4 3 6 1 5 2
55
Insertion-Sort 2 4 3 6 1 5
56
Insertion-Sort 2 4 6 1 5 e = 3
57
Insertion-Sort 2 4 6 1 5 3
58
Insertion-Sort 2 3 4 6 1 5
59
Insertion-Sort 2 3 4 1 5 e = 6
60
Insertion-Sort 2 3 4 6 1 5
61
Insertion-Sort 2 3 4 6 5 e = 1
62
Insertion-Sort 2 3 4 6 5 1
63
Insertion-Sort 2 3 4 6 5 1
64
Insertion-Sort 2 3 4 6 5 1
65
Insertion-Sort 2 3 4 6 5 1
66
Insertion-Sort 1 2 3 4 6 5
67
Insertion-Sort 1 2 3 4 6 e = 5
68
Insertion-Sort 1 2 3 4 6 5
69
Insertion-Sort 1 2 3 4 5 6
70
solange i > 0 und a[i-1] > e
j = 1 solange j < n e = a[j] i = j solange i > 0 und a[i-1] > e a[i] = a[i-1] i-- a[i] = e j++
71
public class InsertionSortVersion1 {
public static void main(String[] args) { int[] a = {4,2,3,6,1,5}; zeigeArray(a); int e = 0; for (int j=1; j<a.length; j++) { e = a[j]; int i = j; while ((i>0) && (a[i-1]>e)) { a[i] = a[i-1]; j--; } a[i] = e; public static void zeigeArray(int[] x) { for (int i=0;i<x.length ;i++ ) { System.out.print(x[i]+" "); System.out.println(); Achtung! Bei && wird Bedingung2 nicht überprüft, wenn Bedingung1 "false" ist. Dies ist hier sinnvoll, da es kein a[-1] gibt.
72
Selection-Sort Insertion-Sort Bubble-Sort
73
Bubble-Sort
74
Beschreibung Bubble-Sort
Wähle die nullte Position als Ausgangsposition. Vertausche das Element an der Anfangspostion mit dem Nachfolger, wenn der Nachfolger kleiner ist. Erhöhe die Ausgangsposition um den Wert 1. Wiederhole die Schritte 2 und 3, bis auch der letzte Wert der Liste verglichen wurde. Wiederhole die Schritte 2 bis 4 (n-1) mal.
75
Bubble-Sort 1. Durchgang 4 2 3 6 1 5
76
Bubble-Sort 1. Durchgang 2 4 3 6 1 5
77
Bubble-Sort 1. Durchgang 2 4 3 6 1 5
78
Bubble-Sort 1. Durchgang 2 3 4 6 1 5
79
Bubble-Sort 1. Durchgang 2 3 4 6 1 5
80
Bubble-Sort 1. Durchgang 2 3 4 6 1 5
81
Bubble-Sort 1. Durchgang 2 3 4 1 6 5
82
Bubble-Sort 1. Durchgang 2 3 4 1 6 5
83
Bubble-Sort 1. Durchgang 2 3 4 1 5 6
84
Bubble-Sort 2. Durchgang 2 3 4 1 5 6
85
Bubble-Sort 2. Durchgang 2 3 4 1 5 6
86
Bubble-Sort 2. Durchgang 2 3 4 1 5 6
87
Bubble-Sort 2. Durchgang 2 3 1 4 5 6
88
Bubble-Sort 2. Durchgang 2 3 1 4 5 6
89
Bubble-Sort 2. Durchgang 2 3 1 4 5 6
90
Bubble-Sort 3. Durchgang 2 3 1 4 5 6
91
Bubble-Sort 3. Durchgang 2 3 1 4 5 6
92
Bubble-Sort 3. Durchgang 2 1 3 4 5 6
93
Bubble-Sort 3. Durchgang 2 1 3 4 5 6
94
Bubble-Sort 3. Durchgang 2 1 3 4 5 6
95
Bubble-Sort 3. Durchgang 2 1 3 4 5 6
96
Bubble-Sort 4. Durchgang 2 1 3 4 5 6
97
Bubble-Sort 4. Durchgang 1 2 3 4 5 6
98
Bubble-Sort 4. Durchgang 1 2 3 4 5 6
99
Bubble-Sort 4. Durchgang 1 2 3 4 5 6
100
Bubble-Sort 4. Durchgang 1 2 3 4 5 6
101
Bubble-Sort 4. Durchgang 1 2 3 4 5 6
102
Bubble-Sort 5. Durchgang 1 2 3 4 5 6
103
Bubble-Sort 5. Durchgang 1 2 3 4 5 6
104
Bubble-Sort 5. Durchgang 1 2 3 4 5 6
105
Bubble-Sort 5. Durchgang 1 2 3 4 5 6
106
Bubble-Sort 5. Durchgang 1 2 3 4 5 6
107
Selection-Sort Insertion-Sort Bubble-Sort
108
j=1 solange j < n-1 i=0 solange i < n-1 a[i] > a[i+1] ja nein
tausche a[i] mit a[i+1] i++ j++
109
public class BubbleSortVersion1 {
public static void main(String[] args) { int[] a = {4,2,3,6,1,5}; zeigeArray(a); for (int j=1;j<a.length-1;j++) { for (int i=0;i<a.length-1;i++ ) { if (a[i]>a[i+1]) { tausche(a,i,i+1); } } // end of main public static void zeigeArray(int[] x) { for (int i=0;i<x.length ;i++ ) { System.out.print(x[i]+" "); } // end of for System.out.println(); public static void tausche(int[] x, int pos1, int pos2) { int dummy = x[pos1]; x[pos1] = x[pos2]; x[pos2] = dummy; } // end of class
110
Stabilität Anton 3 Bernd 3 Chris 1 Chris 1 Bernd 3 Anton 3
111
Handelt es sich bei Selection-Sort um ein stabiles Sortierverfahren?
Anton 3 Bernd 3 Chris 1 Nach Namen sortierte Liste.
112
Handelt es sich bei Selection-Sort um ein stabiles Sortierverfahren?
Anton 3 Bernd 3 Chris 1 Nach Namen sortierte Liste. werden vertauscht
113
Anton 3 Bernd 3 Chris 1 Chris 1 Bernd 3 Anton 3
Handelt es sich bei Selection-Sort um ein stabiles Sortierverfahren? Anton 3 Bernd 3 Chris 1 Nach Namen sortierte Liste. werden vertauscht Chris 1 Bernd 3 Anton 3 Nach Zahlen sortierte Liste. Trotz gleicher Zahl wurde die Reihenfolge von Anton und Bernd geändert. Daraus folgt, dass es sich bei Selection-Sort um kein stabiles Sortierverfahren handelt.
114
Optimierungsmöglichkeiten von Bubble-Sort
nicht optimiert: n-1 Vergleiche pro Durchgang n-1 Durchgänge 1. Optimierungsmöglichkeit bei i Durchgängen: n-i Vergleiche pro Durchgang 2. Optimierungsmöglichkeit: Abbruch, wenn innerhalb eines Durchgangs keine Vertauschung stattgefunden hat.
115
Aufwandsabschätzung Bubble-Sort
Betrachtet werden soll die Anzahl A aller Vergleiche im nicht optimierten Fall. n-1 Vergleiche pro Durchgang n-1 Durchgänge Der Vergleichsaufwand bei Bubble-Sort liegt im in der Größenordnung:
116
Aufwandsabschätzung Bubble-Sort
Betrachtet werden soll die Anzahl A aller Vergleiche im optimierten Fall. n-i Vergleiche pro Durchgang n-1 Durchgänge
117
Aufwandsabschätzung bezüglich der Vertauschungen bei Bubble-Sort (optimierte Variante 1)
schlechtester Fall: Bei jedem Vergleich wird auch getauscht. Vertauschungsaufwand liegt in der Größenordnung durchschnittlicher Fall: Bei ungefähr jedem zweiten Vergleich wird getauscht. bester Fall: Bei keinem Vergleich wird getauscht. Vertauschungsaufwand liegt in der Größenordnung 0.
118
Aufwandsabschätzung bezüglich der Vertauschungen bei Bubble-Sort (optimierte Variante 2 (mit Abbruchbedingung)) schlechtester Fall: Bei jedem Vergleich wird auch getauscht. Vertauschungsaufwand liegt in der Größenordnung durchschnittlicher Fall: Bei ungefähr jedem zweiten Vergleich wird getauscht. bester Fall: Bei keinem Vergleich wird getauscht. Vertauschungsaufwand liegt in der Größenordnung 0.
Ähnliche Präsentationen
© 2024 SlidePlayer.org Inc.
All rights reserved.