Konkreter Beispieldurchlauf von mergeSort auf einem Array der Groesse 16 a = {1,3,2,67,23,12,8,13,24,7,6,50,22,9,30,40} mergeSort(a,0,15) mid = 7 mergeSort(a,0,7) a= {1,2,3,8,12,13,23,67,24,7,6,50,22,9,30,40} mergeSort(a,8,15) a=... /* wenn NR zuende */ merge(a,0,8,15) a=... /* wenn NR zuende */ NR1 mergeSort(a,0,7) a = {1,3,2,67,23,12,8,13,24,7,6,50,22,9,30,40} mid = 3 mergeSort(a,0,3) a = {1,2,3,67,23,12,8,13,24,7,6,50,22,9,30,40} mergeSort(a,4,7) a = {1,2,3,67,8,12,13,23,24,7,6,50,22,9,30,40} merge(a,0,3,7) /* b = {1,2,3,8,12,13,23,67} */ a = {1,2,3,8,12,13,23,67,24,7,6,50,22,9,30,40} NR2 mergeSort(a,8,15) a= {1,2,3,8,12,13,23,67,24,7,6,50,22,9,30,40} mid = 11 ... Fleissaufgabe ... ... NR1.1 mergeSort(a,0,3) a = {1,3,2,67,23,12,8,13,24,7,6,50,22,9,30,40} mid = 1 mergeSort(a,0,1) /* Ergebnisse bei NR 1.1.1 ablesen */ mergeSort(a,2,3) /* Ergebnisse bei NR 1.1.2 ablesen */ a = {1,3,2,67,23,12,8,13,24,7,6,50,22,9,30,40} merge(a,0,1,3) /* b={1,2,3,67 */ a = {1,2,3,67,23,12,8,13,24,7,6,50,22,9,30,40} NR1.2 mergeSort(a,4,7) a = {1,2,3,67,23,12,8,13,24,7,6,50,22,9,30,40} mid = 5 mergeSort(a,4,5) a = {1,2,3,67,12,23,8,13,24,7,6,50,22,9,30,40} mergeSort(a,6,7) a = {1,2,3,67,12,23,8,13,24,7,6,50,22,9,30,40} merge(a,4,5,7) /* b= {8,12,13,23 a = {1,2,3,67,8,12,13,23,24,7,6,50,22,9,30,40} NR1.1.1 mergeSort(a,0,1) a = {1,3,2,67,23,12,8,13,24,7,6,50,22,9,30,40} mid = 0 mergeSort(a,0,0) /* Effekt ist "return", d.h. a bleibt unveraendert */ mergeSort(a,1,1) /* " " " " " " */ a = {1,3,2,67,23,12,8,13,24,7,6,50,22,9,30,40} merge(a,0,0,1) /* b={1,3} */ a = {1,3,2,67,23,12,8,13,24,7,6,50,22,9,30,40} NR1.1.2 mergeSort(a,2,3) a = {1,3,2,67,23,12,8,13,24,7,6,50,22,9,30,40} mid = 2 mergeSort(a,2,2) /* Effekt ist "return", d.h. a bleibt unveraendert */ mergeSort(a,3,3) /* " " " " " " */ a = {1,3,2,67,23,12,8,13,24,7,6,50,22,9,30,40} merge(a,2,2,3) /* b={2,67} */ a = {1,3,2,67,23,12,8,13,24,7,6,50,22,9,30,40} Rekursive Methode zur Erzeugung von Plaenen fuer die "Tuerme von Hanoi" public static String plan(int anzScheiben, int von, int nach) { if (von==nach) return ""; if (anzScheiben==0) return ""; else { int hilf = 6 - von - nach; /* der dritte Stapel */ String planA = plan(anzScheiben-1,von,hilf); String planB = "(" + von + "->" + nach + ")"; String planC = plan(anzScheiben-1,hilf,nach); return planA + planB + planC; } } Illustration der Notwendigkeit der wildcards. public class Bankkonto implements Comparable { ..... public int compareTo(Bankkonto other) { return getKontoNr() - othergetKontoNr(); } } public class Sparkonto extends Bankkonto { zinsen } Bankkonto [] alleKonten = ... Sparkonto [] alleSparkonten = ... mergeSort(alleKonten,...) mergeSort(alleSparkonten,...) Bankkonto extends Comparable, d.h. T=Bankkonto erfuellt T extends Comparable, Sparkonto extends Comparable, d.h. T=Sparkonto erfuellt wenn auch nicht dies: T extends Comparable, so doch immerhin das: T extends Comparable