118
Selection-Sort Insertion-Sort Bubble-Sort

Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Embed Size (px)

Citation preview

Page 1: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

• Selection-Sort

• Insertion-Sort

• Bubble-Sort

Page 2: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Selection-Sort

Page 3: 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.

Page 4: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

23 6 1 7 4

Selection-Sort

j = 0

Page 5: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

23 6 1 7 4

Selection-Sort

min = 0

j = 0

Page 6: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

23 6 1 7 4

Selection-Sort

i = 1

min = 0

j = 0

Page 7: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

23 6 1 7 4

Selection-Sort

i = 1

min = 1

j = 0

Page 8: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

23 6 1 7 4

Selection-Sort

i = 2

min = 1

j = 0

Page 9: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

23 6 1 7 4

Selection-Sort

i = 3

min = 1

j = 0

Page 10: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

23 6 1 7 4

Selection-Sort

i = 3

min = 3

j = 0

Page 11: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

23 6 1 7 4

Selection-Sort

i = 4

min = 3

j = 0

Page 12: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

23 6 1 7 4

Selection-Sort

i = 5

min = 3

j = 0

Page 13: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

23 6 1 7 4

Selection-Sort

i = 5

tausche (a[j], a[min])

min = 3

j = 0

Page 14: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

21 6 3 7 4

Selection-Sort

sortiert

j = 0

Page 15: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

21 6 3 7 4

Selection-Sort

sortiert

j = 1

Page 16: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

21 6 3 7 4

Selection-Sort

sortiert

min = 1

j = 1

Page 17: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

21 6 3 7 4

Selection-Sort

sortiert

min = 1

i = 2j = 1

Page 18: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

21 6 3 7 4

Selection-Sort

sortiert

min = 1

i = 3j = 1

Page 19: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

21 6 3 7 4

Selection-Sort

sortiert

min = 1

i = 4j = 1

Page 20: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

21 6 3 7 4

Selection-Sort

sortiert

min = 1

i = 5j = 1

Page 21: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

21 6 3 7 4

Selection-Sort

sortiert

min = 1

i = 5

tausche (a[j], a[min])

j = 1

Page 22: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

21 6 3 7 4

Selection-Sort

sortiert

j = 1

Page 23: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

21 6 3 7 4

Selection-Sort

sortiert

j = 2

Page 24: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

21 6 3 7 4

Selection-Sort

sortiert

min = 2

j = 2

Page 25: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

21 6 3 7 4

Selection-Sort

sortiert

min = 2

i = 3j = 2

Page 26: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

21 6 3 7 4

Selection-Sort

sortiert

min = 3

i = 3j = 2

Page 27: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

21 6 3 7 4

Selection-Sort

sortiert

min = 3

i = 4j = 2

Page 28: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

21 6 3 7 4

Selection-Sort

sortiert

min = 3

i = 5j = 2

Page 29: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

21 6 3 7 4

Selection-Sort

sortiert

min = 3

i = 5j = 2

tausche (a[j], a[min])

Page 30: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

21 3 6 7 4

Selection-Sort

sortiert

j = 2

Page 31: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

21 3 6 7 4

Selection-Sort

sortiert

j = 3

Page 32: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

21 3 6 7 4

Selection-Sort

sortiert

j = 3

min = 3

Page 33: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

21 3 6 7 4

Selection-Sort

sortiert

j = 3

min = 3

i = 4

Page 34: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

21 3 6 7 4

Selection-Sort

sortiert

j = 3

min = 3

i = 5

Page 35: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

21 3 6 7 4

Selection-Sort

sortiert

j = 3

min = 5

i = 5

Page 36: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

21 3 6 7 4

Selection-Sort

sortiert

j = 3

min = 5

i = 5

tausche (a[j], a[min])

Page 37: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

21 3 4 7 6

Selection-Sort

sortiert

j = 3

Page 38: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

21 3 4 7 6

Selection-Sort

sortiert

j = 4

Page 39: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

21 3 4 7 6

Selection-Sort

sortiert

j = 4

min = 4

Page 40: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

21 3 4 7 6

Selection-Sort

sortiert

j = 4

min = 4

i = 5

Page 41: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

21 3 4 7 6

Selection-Sort

sortiert

j = 4

min = 5

i = 5

Page 42: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

21 3 4 7 6

Selection-Sort

sortiert

j = 4

min = 5

i = 5

tausche (a[j], a[min])

Page 43: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

21 3 4 6 7

Selection-Sort

sortiert

j = 4

Page 44: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

21 3 4 6 7

Selection-Sort

sortiert

j = 4

Page 45: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

a[i] < a[min] solange i < n

min = i

i = j+1

i++

tausche a[j] mit a[min]

ja nein

solange j < n-1

j = 0

j++

min = j

Selection-Sort Nassi-Shneiderman-Diagramm

Page 46: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

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); zeigeArray(a); } } // end of main

Selection-Sort Java-Code (Teil1)

Page 47: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

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

Selection-Sort Java-Code (Teil2)

Page 48: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

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;}

Page 49: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

• Selection-Sort

• Insertion-Sort

• Bubble-Sort

Page 50: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Insertion-Sort

Page 51: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Beschreibung Insertion-Sort

1. Betrachte das nullte Element als sortiert.2. Wähle das nächste Element und füge es an die richtige Stelle

innerhalb des sortierten Teilbereichs ein.3. Wiederhole den 2. Schritt solange, bis der unsortierte Teilbereich leer

ist.

Page 52: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Insertion-Sort

12 34 56

Page 53: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Insertion-Sort

134 56

2e =

Page 54: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Insertion-Sort

134 56

2

Page 55: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Insertion-Sort

134 562

Page 56: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Insertion-Sort

1

3

4 562

e =

Page 57: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Insertion-Sort

1

3

4 562

Page 58: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Insertion-Sort

13 4 562

Page 59: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Insertion-Sort

13 4 5

6

2

e =

Page 60: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Insertion-Sort

13 4 562

Page 61: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Insertion-Sort

1

3 4 562

e =

Page 62: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Insertion-Sort

1

3 4 562

Page 63: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Insertion-Sort

1

3 4 562

Page 64: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Insertion-Sort

1

3 4 562

Page 65: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Insertion-Sort

1

3 4 562

Page 66: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Insertion-Sort

1 3 4 562

Page 67: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Insertion-Sort

1 3 4

5

62

e =

Page 68: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Insertion-Sort

1 3 4

5

62

Page 69: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Insertion-Sort

1 3 4 5 62

Page 70: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

i = j

solange i > 0 und a[i-1] > e

i--

solange j < n

j = 1

e = a[j]

a[i] = a[i-1]

a[i] = ej++

Page 71: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

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; zeigeArray(a); } } 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.

Page 72: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

• Selection-Sort

• Insertion-Sort

• Bubble-Sort

Page 73: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

Page 74: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Beschreibung Bubble-Sort1. Wähle die nullte Position als Ausgangsposition.2. Vertausche das Element an der Anfangspostion mit dem

Nachfolger, wenn der Nachfolger kleiner ist.3. Erhöhe die Ausgangsposition um den Wert 1.4. Wiederhole die Schritte 2 und 3, bis auch der letzte Wert der

Liste verglichen wurde.5. Wiederhole die Schritte 2 bis 4 (n-1) mal.

Page 75: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

12 34 56

1. Durchgang

Page 76: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

14 32 56

1. Durchgang

Page 77: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

14 32 56

1. Durchgang

Page 78: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

13 42 56

1. Durchgang

Page 79: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

13 42 56

1. Durchgang

Page 80: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

13 42 56

1. Durchgang

Page 81: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

63 42 51

1. Durchgang

Page 82: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

63 42 51

1. Durchgang

Page 83: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

53 42 61

1. Durchgang

Page 84: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

53 42 61

2. Durchgang

Page 85: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

53 42 61

2. Durchgang

Page 86: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

53 42 61

2. Durchgang

Page 87: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

53 12 64

2. Durchgang

Page 88: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

53 12 64

2. Durchgang

Page 89: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

53 12 64

2. Durchgang

Page 90: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

53 12 64

3. Durchgang

Page 91: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

53 12 64

3. Durchgang

Page 92: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

51 32 64

3. Durchgang

Page 93: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

51 32 64

3. Durchgang

Page 94: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

51 32 64

3. Durchgang

Page 95: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

51 32 64

3. Durchgang

Page 96: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

51 32 64

4. Durchgang

Page 97: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

52 31 64

4. Durchgang

Page 98: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

52 31 64

4. Durchgang

Page 99: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

52 31 64

4. Durchgang

Page 100: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

52 31 64

4. Durchgang

Page 101: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

52 31 64

4. Durchgang

Page 102: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

52 31 64

5. Durchgang

Page 103: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

52 31 64

5. Durchgang

Page 104: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

52 31 64

5. Durchgang

Page 105: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

52 31 64

5. Durchgang

Page 106: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Bubble-Sort

52 31 64

5. Durchgang

Page 107: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

• Selection-Sort

• Insertion-Sort

• Bubble-Sort

Page 108: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

i=0

solange i < n-1

ja

solange j < n-1

j=1

a[i] > a[i+1]

tausche a[i] mit a[i+1]

nein

i++

j++

Page 109: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

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); } } } zeigeArray(a); } // 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

Page 110: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Stabilität

Anton3

Bernd3

Chris1

Chris1

Anton3

Bernd3

Page 111: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Anton3

Bernd3

Chris1

Handelt es sich bei Selection-Sort um ein stabiles Sortierverfahren?

Nach Namen sortierte Liste.

Page 112: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Anton3

Bernd3

Chris1

Handelt es sich bei Selection-Sort um ein stabiles Sortierverfahren?

Nach Namen sortierte Liste.

werden vertauscht

Page 113: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Anton3

Bernd3

Chris1

Handelt es sich bei Selection-Sort um ein stabiles Sortierverfahren?

Nach Namen sortierte Liste.

werden vertauscht

Chris1

Bernd3

Anton3

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.

Page 114: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Optimierungsmöglichkeiten von Bubble-Sort

nicht optimiert:n-1 Vergleiche pro Durchgangn-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.

Page 115: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Aufwandsabschätzung Bubble-Sort

Betrachtet werden soll die Anzahl A aller Vergleiche im nicht optimierten Fall.

n-1 Vergleiche pro Durchgangn-1 Durchgänge

2A n 1 n 1 n 2n 1

2O n

Der Vergleichsaufwand bei Bubble-Sort liegt im

in der Größenordnung:

Page 116: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

Aufwandsabschätzung Bubble-Sort

Betrachtet werden soll die Anzahl A aller Vergleiche im optimierten Fall.

n-i Vergleiche pro Durchgangn-1 Durchgänge

(n 1) (n 2) ... 1 ?

1 ... (2) (n 1) ?

(5) (4) 3 2 1 ?

(1) (2) 3 4 5 ?

(6) (6) 6 6 6 30 n ... n n n n 1

2n n 1 n nA

2 2

Page 117: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

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.Vertauschungsaufwand liegt in der Größenordnung

bester Fall: Bei keinem Vergleich wird getauscht.Vertauschungsaufwand liegt in der Größenordnung 0.

2O n

2O n

Page 118: Selection-Sort Insertion-Sort Bubble-Sort. Selection-Sort

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.Vertauschungsaufwand liegt in der Größenordnung

bester Fall: Bei keinem Vergleich wird getauscht.Vertauschungsaufwand liegt in der Größenordnung 0.

2O n

2O n