10.07.2015 Aufrufe

Informatik II (37-836) Sortieren Insertion Sort C++ Code

Informatik II (37-836) Sortieren Insertion Sort C++ Code

Informatik II (37-836) Sortieren Insertion Sort C++ Code

MEHR ANZEIGEN
WENIGER ANZEIGEN

Sie wollen auch ein ePaper? Erhöhen Sie die Reichweite Ihrer Titel.

YUMPU macht aus Druck-PDFs automatisch weboptimierte ePaper, die Google liebt.

Shell <strong>Sort</strong>Leistung von Shellsort1000000Time for <strong>Sort</strong>ing on a log-log ScaleB<strong>C++</strong> <strong>Code</strong>1 void Shell<strong>Sort</strong>(int A[], int N) {2 int i,j,Tmp,Gap;34 for(Gap=N/2; Gap>0;Gap= Gap==2?1:(int)(Gap/2.2)){5 for(i=Gap; i=Gap && Tmp


Analyse (worst case)T(1) = 1T(n) = T(n-1) + n + T(1)=N 2Analyse (best, average case):T(n) = 2*T(n/2) + nLösung mit Ansatz: N Log NNLogN = 2*(N/2*Log(N/2))+N= (N*Log(N/2))+N= (N*(Log(N)-Log(2))+N= (N*(Log(N)-1)+N= (N*Log(N)-N+N= N*Log(N) ok!Lösung mit Rezept (Mastertheorem)siehe Folien Vorlesung 3-5!<strong>37</strong>-<strong>836</strong> <strong>Informatik</strong> <strong>II</strong> 16.6.99 - 9 © StrickerMerge <strong>Sort</strong><strong>37</strong>-<strong>836</strong> <strong>Informatik</strong> <strong>II</strong> 16.6.99 - 10 © StrickerMerge <strong>Sort</strong>Merge <strong>Sort</strong>• Extra Speicherplatz notwendig.• Ideal für externes <strong><strong>Sort</strong>ieren</strong> ab Bänder.• Anzahl Operationen: optimal?Triviale GrenzeUntere Grenze:N Operationen• Man muss jedes Element ansehen!aber....reicht “ein paar mal Ansehen” oderreichts nicht?Genauere Definitionen erforderlich!Anzahl Operationen pro Phase:Anzahl MergephasenNLog[N]<strong>37</strong>-<strong>836</strong> <strong>Informatik</strong> <strong>II</strong> 16.6.99 - 11 © Stricker<strong>37</strong>-<strong>836</strong> <strong>Informatik</strong> <strong>II</strong> 16.6.99 - 12 © Stricker


Untere Grenze:Basisoperation / Modell:• Vergleich und Austausch zwischen zweiElementenIdee:• <strong><strong>Sort</strong>ieren</strong> = Permutieren -1Welche Permutation sortieren wir?Anzahl Permutationen: N!Entscheidungsbaum hat HöheLog[N!]=NLogN-1.44NRouting = <strong>Sort</strong>ingFrage: wieviele Schaltpunkte brauchtes in einem Telefonnetz mit N Teilnehmerzur Vermittlung von N Anrufen.• Ebenfalls eine Permutation• Bei N gleichzeitigen Anrufen<strong><strong>Sort</strong>ieren</strong> nach gerufener Nummer!=a2:a3a1:a3>>≤ a1:a3 ≤ a2:a3≤>≤> <strong>37</strong>-<strong>836</strong> <strong>Informatik</strong> <strong>II</strong> 16.6.99 - 13 © Stricker<strong>37</strong>-<strong>836</strong> <strong>Informatik</strong> <strong>II</strong> 16.6.99 - 14 © Stricker<strong><strong>Sort</strong>ieren</strong> ohne VergleichenCounting <strong>Sort</strong>1 2 3 4 5 6 7 8A: 3 6 4 1 3 4 1 41 2 3 4 5 6 1 2 3 4 5 6 plusC: 2 0 2 3 0 1 -> C’:2 2 4 7 7 8 scan1 2 3 4 5 6 7 8A: 3 6 4 1 3 4 1 41 2 3 4 5 6 1 2 3 4 5 6C’:2 2 4 7 7 8 -> 2 2 4 6 7 81 2 3 4 5 6 7 8B: . . . . . . 4 .1 2 3 4 5 6 1 2 3 4 5 6C’:2 2 4 6 7 8 -> 1 2 4 6 7 81 2 3 4 5 6 7 8B: . 1 . . . . 4 .1 2 3 4 5 6 1 2 3 4 5 6C’:1 2 4 6 7 8 -> 1 2 4 5 7 81 2 3 4 5 6 7 8B: . 1 . . . 4 4 ....<strong>37</strong>-<strong>836</strong> <strong>Informatik</strong> <strong>II</strong> 16.6.99 - 15 © StrickerCounting <strong>Sort</strong>1 void Count<strong>Sort</strong>(short A[], int N) {2 int C[65536];3 for (i=0; i


Input:Stacks Application(xyz[abc]uvw})345[567inputStacks Applicationaccept (no error)reject (error)output -8Regeln: zahl -> pushoperator -> pop pop op push -> pop result<strong>37</strong>-<strong>836</strong> <strong>Informatik</strong> <strong>II</strong> 16.6.99 - 17 © Stricker<strong>37</strong>-<strong>836</strong> <strong>Informatik</strong> <strong>II</strong> 16.6.99 - 18 © Strickerinput:outputpostfix: 1 2 3 3 ^ ^ - 4 5 6 * + 7 * -<strong>37</strong>-<strong>836</strong> <strong>Informatik</strong> <strong>II</strong> 16.6.99 - 19 © Stricker

Hurra! Ihre Datei wurde hochgeladen und ist bereit für die Veröffentlichung.

Erfolgreich gespeichert!

Leider ist etwas schief gelaufen!