Selection-Sort Insertion-Sort Bubble-Sort
Selection-Sort
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.
Selection-Sort j = 0 3 2 6 1 7 4
Selection-Sort j = 0 3 2 6 1 7 4 min = 0
Selection-Sort j = 0 i = 1 3 2 6 1 7 4 min = 0
Selection-Sort j = 0 i = 1 3 2 6 1 7 4 min = 1
Selection-Sort j = 0 i = 2 3 2 6 1 7 4 min = 1
Selection-Sort j = 0 i = 3 3 2 6 1 7 4 min = 1
Selection-Sort j = 0 i = 3 3 2 6 1 7 4 min = 3
Selection-Sort j = 0 i = 4 3 2 6 1 7 4 min = 3
Selection-Sort j = 0 i = 5 3 2 6 1 7 4 min = 3
Selection-Sort j = 0 i = 5 3 2 6 1 7 4 min = 3 tausche (a[j], a[min])
Selection-Sort j = 0 1 2 6 3 7 4 sortiert
Selection-Sort j = 1 1 2 6 3 7 4 sortiert
Selection-Sort j = 1 1 2 6 3 7 4 sortiert min = 1
Selection-Sort j = 1 i = 2 1 2 6 3 7 4 sortiert min = 1
Selection-Sort j = 1 i = 3 1 2 6 3 7 4 sortiert min = 1
Selection-Sort j = 1 i = 4 1 2 6 3 7 4 sortiert min = 1
Selection-Sort j = 1 i = 5 1 2 6 3 7 4 sortiert min = 1
1 2 6 3 7 4 tausche (a[j], a[min]) Selection-Sort j = 1 i = 5 sortiert
Selection-Sort j = 1 1 2 6 3 7 4 sortiert
Selection-Sort j = 2 1 2 6 3 7 4 sortiert
Selection-Sort j = 2 1 2 6 3 7 4 sortiert min = 2
Selection-Sort j = 2 i = 3 1 2 6 3 7 4 sortiert min = 2
Selection-Sort j = 2 i = 3 1 2 6 3 7 4 sortiert min = 3
Selection-Sort j = 2 i = 4 1 2 6 3 7 4 sortiert min = 3
Selection-Sort j = 2 i = 5 1 2 6 3 7 4 sortiert min = 3
1 2 6 3 7 4 tausche (a[j], a[min]) Selection-Sort j = 2 i = 5 sortiert
Selection-Sort j = 2 1 2 3 6 7 4 sortiert
Selection-Sort j = 3 1 2 3 6 7 4 sortiert
Selection-Sort j = 3 1 2 3 6 7 4 sortiert min = 3
Selection-Sort j = 3 i = 4 1 2 3 6 7 4 sortiert min = 3
Selection-Sort j = 3 i = 5 1 2 3 6 7 4 sortiert min = 3
Selection-Sort j = 3 i = 5 1 2 3 6 7 4 sortiert min = 5
1 2 3 6 7 4 tausche (a[j], a[min]) Selection-Sort j = 3 i = 5 sortiert
Selection-Sort j = 3 1 2 3 4 7 6 sortiert
Selection-Sort j = 4 1 2 3 4 7 6 sortiert
Selection-Sort j = 4 1 2 3 4 7 6 sortiert min = 4
Selection-Sort j = 4 i = 5 1 2 3 4 7 6 sortiert min = 4
Selection-Sort j = 4 i = 5 1 2 3 4 7 6 sortiert min = 5
1 2 3 4 7 6 tausche (a[j], a[min]) Selection-Sort j = 4 i = 5 sortiert
Selection-Sort j = 4 1 2 3 4 6 7 sortiert
Selection-Sort j = 4 1 2 3 4 6 7 sortiert
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++
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
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
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;
Selection-Sort Insertion-Sort Bubble-Sort
Insertion-Sort
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.
Insertion-Sort 4 2 3 6 1 5
Insertion-Sort 4 3 6 1 5 e = 2
Insertion-Sort 4 3 6 1 5 2
Insertion-Sort 2 4 3 6 1 5
Insertion-Sort 2 4 6 1 5 e = 3
Insertion-Sort 2 4 6 1 5 3
Insertion-Sort 2 3 4 6 1 5
Insertion-Sort 2 3 4 1 5 e = 6
Insertion-Sort 2 3 4 6 1 5
Insertion-Sort 2 3 4 6 5 e = 1
Insertion-Sort 2 3 4 6 5 1
Insertion-Sort 2 3 4 6 5 1
Insertion-Sort 2 3 4 6 5 1
Insertion-Sort 2 3 4 6 5 1
Insertion-Sort 1 2 3 4 6 5
Insertion-Sort 1 2 3 4 6 e = 5
Insertion-Sort 1 2 3 4 6 5
Insertion-Sort 1 2 3 4 5 6
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++
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.
Selection-Sort Insertion-Sort Bubble-Sort
Bubble-Sort
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.
Bubble-Sort 1. Durchgang 4 2 3 6 1 5
Bubble-Sort 1. Durchgang 2 4 3 6 1 5
Bubble-Sort 1. Durchgang 2 4 3 6 1 5
Bubble-Sort 1. Durchgang 2 3 4 6 1 5
Bubble-Sort 1. Durchgang 2 3 4 6 1 5
Bubble-Sort 1. Durchgang 2 3 4 6 1 5
Bubble-Sort 1. Durchgang 2 3 4 1 6 5
Bubble-Sort 1. Durchgang 2 3 4 1 6 5
Bubble-Sort 1. Durchgang 2 3 4 1 5 6
Bubble-Sort 2. Durchgang 2 3 4 1 5 6
Bubble-Sort 2. Durchgang 2 3 4 1 5 6
Bubble-Sort 2. Durchgang 2 3 4 1 5 6
Bubble-Sort 2. Durchgang 2 3 1 4 5 6
Bubble-Sort 2. Durchgang 2 3 1 4 5 6
Bubble-Sort 2. Durchgang 2 3 1 4 5 6
Bubble-Sort 3. Durchgang 2 3 1 4 5 6
Bubble-Sort 3. Durchgang 2 3 1 4 5 6
Bubble-Sort 3. Durchgang 2 1 3 4 5 6
Bubble-Sort 3. Durchgang 2 1 3 4 5 6
Bubble-Sort 3. Durchgang 2 1 3 4 5 6
Bubble-Sort 3. Durchgang 2 1 3 4 5 6
Bubble-Sort 4. Durchgang 2 1 3 4 5 6
Bubble-Sort 4. Durchgang 1 2 3 4 5 6
Bubble-Sort 4. Durchgang 1 2 3 4 5 6
Bubble-Sort 4. Durchgang 1 2 3 4 5 6
Bubble-Sort 4. Durchgang 1 2 3 4 5 6
Bubble-Sort 4. Durchgang 1 2 3 4 5 6
Bubble-Sort 5. Durchgang 1 2 3 4 5 6
Bubble-Sort 5. Durchgang 1 2 3 4 5 6
Bubble-Sort 5. Durchgang 1 2 3 4 5 6
Bubble-Sort 5. Durchgang 1 2 3 4 5 6
Bubble-Sort 5. Durchgang 1 2 3 4 5 6
Selection-Sort Insertion-Sort Bubble-Sort
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++
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
Stabilität 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.
Handelt es sich bei Selection-Sort um ein stabiles Sortierverfahren? Anton 3 Bernd 3 Chris 1 Nach Namen sortierte Liste. werden vertauscht
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.
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.
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:
Aufwandsabschätzung Bubble-Sort Betrachtet werden soll die Anzahl A aller Vergleiche im optimierten Fall. n-i Vergleiche pro Durchgang n-1 Durchgänge
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.
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.