diff --git a/20231207/bogosort-1.c b/20231207/bogosort-1.c new file mode 100644 index 0000000000000000000000000000000000000000..6f8f4fe71606cb33ae7b1382cb5361a4f49c9e04 --- /dev/null +++ b/20231207/bogosort-1.c @@ -0,0 +1,66 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +int comparisons = 0; + +void display (char **name, int left, int right) +{ + printf ("\e[H\e[J"); + for (int i = 0; name[i]; i++) + { + printf ("%s", name[i]); + if (i == left || i == right) + printf (" <--"); + printf ("\n"); + } + printf ("%d\n", comparisons); +} + +int compare (char **name, int left, int right) +{ + int result = strcmp (name[left], name[right]); + comparisons++; + display (name, left, right); + usleep (200000); + return result; +} + +int is_sorted (char **name) +{ + for (int i = 1; name[i]; i++) + if (compare (name, i - 1, i) > 0) + return 0; + return 1; +} + +void mix (char **name) +{ + int n = 0; + while (name[n]) + n++; + for (int i = 0; i < n; i++) + { + int j = rand () % (n - i); + char *temp = name[i]; + name[i] = name[j]; + name[j] = temp; + } +} + +void sort (char **name) +{ + while (!is_sorted (name)) + mix (name); +} + +int main (void) +{ + char *name[] = { "Otto", "Lisa", "Anna", "Heinrich", "Siegfried", "Peter", + "Dieter", "Hugo", "Berta", "Maria", "Fritz", "Box", "Hans", + "Thomas", "Ulrich", "Zacharias", NULL }; + sort (name); + display (name, -1, -1); + return 0; +} diff --git a/20231207/bogosort-2.c b/20231207/bogosort-2.c new file mode 100644 index 0000000000000000000000000000000000000000..3120ed170739dee09fe3182637c0b2369efc006d --- /dev/null +++ b/20231207/bogosort-2.c @@ -0,0 +1,64 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +int comparisons = 0; + +void display (char **name, int left, int right) +{ + printf ("\e[H\e[J"); + for (int i = 0; name[i]; i++) + { + printf ("%s", name[i]); + if (i == left || i == right) + printf (" <--"); + printf ("\n"); + } + printf ("%d\n", comparisons); +} + +int compare (char **name, int left, int right) +{ + int result = strcmp (name[left], name[right]); + comparisons++; + display (name, left, right); + usleep (200000); + return result; +} + +int is_sorted (char **name) +{ + for (int i = 1; name[i]; i++) + if (compare (name, i - 1, i) > 0) + return 0; + return 1; +} + +void mix (char **name) +{ + int n = 0; + while (name[n]) + n++; + for (int i = 0; i < n; i++) + { + int j = rand () % (n - i); + char *temp = name[i]; + name[i] = name[j]; + name[j] = temp; + } +} + +void sort (char **name) +{ + while (!is_sorted (name)) + mix (name); +} + +int main (void) +{ + char *name[] = { "Otto", "Lisa", "Anna", "Heinrich", "Siegfried", NULL }; + sort (name); + display (name, -1, -1); + return 0; +} diff --git a/20231207/bsort-1.c b/20231207/bsort-1.c new file mode 100644 index 0000000000000000000000000000000000000000..3c02adc72e6a7e702e2c7c68c838fc94cc8ef547 --- /dev/null +++ b/20231207/bsort-1.c @@ -0,0 +1,48 @@ +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +int comparisons = 0; + +void display (char **name, int left, int right) +{ + printf ("\e[H\e[J"); + for (int i = 0; name[i]; i++) + { + printf ("%s", name[i]); + if (i == left || i == right) + printf (" <--"); + printf ("\n"); + } + printf ("%d\n", comparisons); +} + +int compare (char **name, int left, int right) +{ + int result = strcmp (name[left], name[right]); + comparisons++; + display (name, left, right); + usleep (200000); + return result; +} + +void sort (char **name) +{ + for (int i = 1; name[i]; i++) + if (compare (name, i - 1, i) > 0) + { + char *temp = name[i - 1]; + name[i - 1] = name[i]; + name[i] = temp; + } +} + +int main (void) +{ + char *name[] = { "Otto", "Lisa", "Anna", "Heinrich", "Siegfried", "Peter", + "Dieter", "Hugo", "Berta", "Maria", "Fritz", "Box", "Hans", + "Thomas", "Ulrich", "Zacharias", NULL }; + sort (name); + display (name, -1, -1); + return 0; +} diff --git a/20231207/bsort-2.c b/20231207/bsort-2.c new file mode 100644 index 0000000000000000000000000000000000000000..d277be0182a40ecd0c66eaeef94d7aebac6aa542 --- /dev/null +++ b/20231207/bsort-2.c @@ -0,0 +1,55 @@ +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +int comparisons = 0; + +void display (char **name, int left, int right) +{ + printf ("\e[H\e[J"); + for (int i = 0; name[i]; i++) + { + printf ("%s", name[i]); + if (i == left || i == right) + printf (" <--"); + printf ("\n"); + } + printf ("%d\n", comparisons); +} + +int compare (char **name, int left, int right) +{ + int result = strcmp (name[left], name[right]); + comparisons++; + display (name, left, right); + usleep (200000); + return result; +} + +void sort (char **name) +{ + int sorted = 0; + while (name[sorted]) + sorted++; + while (sorted > 0) + { + for (int i = 1; i < sorted; i++) + if (compare (name, i - 1, i) > 0) + { + char *temp = name[i - 1]; + name[i - 1] = name[i]; + name[i] = temp; + } + sorted--; + } +} + +int main (void) +{ + char *name[] = { "Otto", "Lisa", "Anna", "Heinrich", "Siegfried", "Peter", + "Dieter", "Hugo", "Berta", "Maria", "Fritz", "Box", "Hans", + "Thomas", "Ulrich", "Zacharias", NULL }; + sort (name); + display (name, -1, -1); + return 0; +} diff --git a/20231207/bsort-3.c b/20231207/bsort-3.c new file mode 100644 index 0000000000000000000000000000000000000000..67d0097b913171b4523f4f04b6b6ff494046dabd --- /dev/null +++ b/20231207/bsort-3.c @@ -0,0 +1,58 @@ +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +int comparisons = 0; + +void display (char **name, int left, int right) +{ + printf ("\e[H\e[J"); + for (int i = 0; name[i]; i++) + { + printf ("%s", name[i]); + if (i == left || i == right) + printf (" <--"); + printf ("\n"); + } + printf ("%d\n", comparisons); +} + +int compare (char **name, int left, int right) +{ + int result = strcmp (name[left], name[right]); + comparisons++; + display (name, left, right); + usleep (200000); + return result; +} + +void sort (char **name) +{ + int done = 0; + int sorted = 0; + while (name[sorted]) + sorted++; + while (sorted > 0 && !done) + { + done = 1; + for (int i = 1; i < sorted; i++) + if (compare (name, i - 1, i) > 0) + { + done = 0; + char *temp = name[i - 1]; + name[i - 1] = name[i]; + name[i] = temp; + } + sorted--; + } +} + +int main (void) +{ + char *name[] = { "Otto", "Lisa", "Anna", "Heinrich", "Siegfried", "Peter", + "Dieter", "Hugo", "Berta", "Maria", "Fritz", "Box", "Hans", + "Thomas", "Ulrich", "Zacharias", NULL }; + sort (name); + display (name, -1, -1); + return 0; +} diff --git a/20231207/bsort-3a.c b/20231207/bsort-3a.c new file mode 100644 index 0000000000000000000000000000000000000000..c475276ae2e17a78afa598f0501ea116aa87ad0f --- /dev/null +++ b/20231207/bsort-3a.c @@ -0,0 +1,58 @@ +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +int comparisons = 0; + +void display (char **name, int left, int right) +{ + printf ("\e[H\e[J"); + for (int i = 0; name[i]; i++) + { + printf ("%s", name[i]); + if (i == left || i == right) + printf (" <--"); + printf ("\n"); + } + printf ("%d\n", comparisons); +} + +int compare (char **name, int left, int right) +{ + int result = strcmp (name[left], name[right]); + comparisons++; + display (name, left, right); + usleep (200000); + return result; +} + +void sort (char **name) +{ + int done = 0; + int sorted = 0; + while (name[sorted]) + sorted++; + while (sorted > 0 && !done) + { + done = 1; + for (int i = 1; i < sorted; i++) + if (compare (name, i - 1, i) > 0) + { + done = 0; + char *temp = name[i - 1]; + name[i - 1] = name[i]; + name[i] = temp; + } + sorted--; + } +} + +int main (void) +{ + char *name[] = { "Anna", "Berta", "Box", "Dieter", "Fritz", "Hans", "Heinrich", "Hugo", + "Lisa", "Maria", "Otto", "Peter", "Siegfried", "Thomas", "Ulrich", + "Zacharias", NULL }; + sort (name); + display (name, -1, -1); + return 0; +} diff --git a/20231207/bsort-3b.c b/20231207/bsort-3b.c new file mode 100644 index 0000000000000000000000000000000000000000..9795fe9a8ef59558bcf42f7bfe489f8b5f826521 --- /dev/null +++ b/20231207/bsort-3b.c @@ -0,0 +1,58 @@ +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +int comparisons = 0; + +void display (char **name, int left, int right) +{ + printf ("\e[H\e[J"); + for (int i = 0; name[i]; i++) + { + printf ("%s", name[i]); + if (i == left || i == right) + printf (" <--"); + printf ("\n"); + } + printf ("%d\n", comparisons); +} + +int compare (char **name, int left, int right) +{ + int result = strcmp (name[left], name[right]); + comparisons++; + display (name, left, right); + usleep (200000); + return result; +} + +void sort (char **name) +{ + int done = 0; + int sorted = 0; + while (name[sorted]) + sorted++; + while (sorted > 0 && !done) + { + done = 1; + for (int i = 1; i < sorted; i++) + if (compare (name, i - 1, i) > 0) + { + done = 0; + char *temp = name[i - 1]; + name[i - 1] = name[i]; + name[i] = temp; + } + sorted--; + } +} + +int main (void) +{ + char *name[] = { "Zacharias", "Ulrich", "Thomas", "Siegfried", "Peter", "Otto", "Maria", + "Lisa", "Hugo", "Heinrich", "Hans", "Fritz", "Dieter", "Box", "Berta", + "Anna", NULL }; + sort (name); + display (name, -1, -1); + return 0; +} diff --git a/20231207/bsort-4.c b/20231207/bsort-4.c new file mode 100644 index 0000000000000000000000000000000000000000..241270a46f4ffa005d252207d769c3c4059ed7e2 --- /dev/null +++ b/20231207/bsort-4.c @@ -0,0 +1,57 @@ +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +int comparisons = 0; + +void display (char **name, int left, int right) +{ + printf ("\e[H\e[J"); + for (int i = 0; name[i]; i++) + { + printf ("%s", name[i]); + if (i == left || i == right) + printf (" <--"); + printf ("\n"); + } + printf ("%d\n", comparisons); +} + +int compare (char **name, int left, int right) +{ + int result = strcmp (name[left], name[right]); + comparisons++; + display (name, left, right); + usleep (200000); + return result; +} + +void sort (char **name) +{ + int sorted = 0; + while (name[sorted]) + sorted++; + while (sorted > 0) + { + int new_sorted = 0; + for (int i = 1; i < sorted; i++) + if (compare (name, i - 1, i) > 0) + { + new_sorted = i; + char *temp = name[i - 1]; + name[i - 1] = name[i]; + name[i] = temp; + } + sorted = new_sorted; + } +} + +int main (void) +{ + char *name[] = { "Otto", "Lisa", "Anna", "Heinrich", "Siegfried", "Peter", + "Dieter", "Hugo", "Berta", "Maria", "Fritz", "Box", "Hans", + "Thomas", "Ulrich", "Zacharias", NULL }; + sort (name); + display (name, -1, -1); + return 0; +} diff --git a/20231207/hanoi-32.c b/20231207/hanoi-32.c new file mode 100644 index 0000000000000000000000000000000000000000..55e8d74db6d7284d6db225a6f8f3e08ab4efdc06 --- /dev/null +++ b/20231207/hanoi-32.c @@ -0,0 +1,58 @@ +#include <stdio.h> +#include <error.h> + +#define DISKS 32 + +int n[3], tower[3][DISKS]; + +void display (void) +{ + printf ("\n"); + for (int i = 0; i < 3; i++) + { + printf ("tower %d:", i); + for (int j = 0; j < n[i]; j++) + printf (" %d", tower[i][j]); + printf ("\n"); + } +} + +void move (int from, int to, int disks) +{ + if (disks == 1) + { + if (n[from] <= 0) + error (1, 0, "trying to move disk from empty tower"); + if (n[to] > 0 && tower[to][n[to] - 1] < tower[from][n[from] - 1]) + error (1, 0, "trying to move larger disk on smaller one"); + tower[to][n[to]] = tower[from][n[from] - 1]; + n[to]++; + n[from]--; + static int counter = 1; + if (counter++ >= 100000000) + { + display (); + counter = 1; + } + } + else + { + int help = 0 + 1 + 2 - from - to; + move (from, help, disks - 1); + move (from, to, 1); + move (help, to, disks - 1); + } +} + +int main (void) +{ + n[0] = DISKS; + for (int i = 0; i < DISKS; i++) + tower[0][i] = DISKS - i; + n[1] = 0; + n[2] = 0; + display (); + move (0, 2, DISKS); + display (); + return 0; +} diff --git a/20231207/hp-20231207.pdf b/20231207/hp-20231207.pdf index f1b861dbf29df728f937d9c68089cb0d57649ee3..19ef347b517567ceededc8726a63a5338b1b83f4 100644 Binary files a/20231207/hp-20231207.pdf and b/20231207/hp-20231207.pdf differ diff --git a/20231207/hp-20231207.tex b/20231207/hp-20231207.tex index 4bf45fea6f8b8815ed5fd7baa594135b39340e0f..45f0d25f40af750bb62e90acd4f634a5944c0651 100644 --- a/20231207/hp-20231207.tex +++ b/20231207/hp-20231207.tex @@ -248,41 +248,41 @@ } \end{lstlisting} \end{onlyenv} -% \begin{onlyenv}<6-> -% \vspace{-5.0cm} -% \hspace*{7.4cm}\begin{minipage}[t]{5cm} -% 32 Scheiben: -% \begin{lstlisting}[gobble=10,style=terminal] -% $ ¡time ./hanoi-9b¿ -% ... -% real 0m30,672s -% user 0m30,662s -% sys 0m0,008s -% \end{lstlisting} -% \pause[7] -% \begin{itemize} -% \arrowitem -% etwas über 1 Minute\\ -% für 64 Scheiben -% \end{itemize} -% \pause -% \vspace*{-0.5cm} -% \begin{picture}(0,0) -% \color{red} -% \put(0,0){\makebox(0,0)[bl]{\tikz[line width=1pt]{\draw(0,0)--(4,0.8);}}} -% \put(0,0.8){\makebox(0,0)[tl]{\tikz[line width=1pt]{\draw(0,0)--(4,-0.8);}}} -% \end{picture} -% -% Für jede zusätzliche Scheibe\\verdoppelt sich die Rechenzeit! -% % 30.672 * 2^32 / 3600 / 24 / 365.25 = 4174.43775518138261464750 -% \begin{itemize} -% \arrowitem -% $\frac{30,672\,\text{s}\,\cdot\,2^{32}}{3600\,\cdot\,24\,\cdot\,365,25} \approx 4174$ -% Jahre\\[\smallskipamount] -% für 64 Scheiben -% \end{itemize} -% \end{minipage} -% \end{onlyenv} + \begin{onlyenv}<6-> + \vspace{-5.0cm} + \hspace*{7.4cm}\begin{minipage}[t]{5cm} + 32 Scheiben: + \begin{lstlisting}[gobble=10,style=terminal] + $ ¡time ./hanoi-9b¿ + ... + real 0m30,672s + user 0m30,662s + sys 0m0,008s + \end{lstlisting} + \pause[7] + \begin{itemize} + \arrowitem + etwas über 1 Minute\\ + für 64 Scheiben + \end{itemize} + \pause + \vspace*{-0.5cm} + \begin{picture}(0,0) + \color{red} + \put(0,0){\makebox(0,0)[bl]{\tikz[line width=1pt]{\draw(0,0)--(4,0.8);}}} + \put(0,0.8){\makebox(0,0)[tl]{\tikz[line width=1pt]{\draw(0,0)--(4,-0.8);}}} + \end{picture} + + Für jede zusätzliche Scheibe\\verdoppelt sich die Rechenzeit! + % 30.672 * 2^32 / 3600 / 24 / 365.25 = 4174.43775518138261464750 + \begin{itemize} + \arrowitem + $\frac{30,672\,\text{s}\,\cdot\,2^{32}}{3600\,\cdot\,24\,\cdot\,365,25} \approx 4174$ + Jahre\\[\smallskipamount] + für 64 Scheiben + \end{itemize} + \end{minipage} + \end{onlyenv} \end{onlyenv} \end{frame} diff --git a/20231207/hp-20231207.txt b/20231207/hp-20231207.txt new file mode 100644 index 0000000000000000000000000000000000000000..0caaea428c61c64d54b0029851ee65baffce1831 --- /dev/null +++ b/20231207/hp-20231207.txt @@ -0,0 +1,20 @@ +x ^ 9 = x * x * x * x * x * x * x * x * x # 8 Multiplikationen + + = ((x^2)^2)^2 * x # 4 Multiplikationen + `----v----' + x^8 + + +9 = 1001 binär + + Bits von rechts nach links: + + x x 1 --> damit multiplizieren + + ^2 x^2 0 --> damit nicht multiplizieren + + ^2 x^4 0 --> damit nicht multiplizieren + + ^2 x^8 1 --> damit multiplizieren + +--> O(log n) statt O(n) diff --git a/20231207/names.h b/20231207/names.h new file mode 100644 index 0000000000000000000000000000000000000000..a265ff4141a2cc578d3f6f402822a2dbbd5cea90 --- /dev/null +++ b/20231207/names.h @@ -0,0 +1,50 @@ +"Michael", "Laura", +"Elias", "Julia", +"Luca", "Anna", +"Liam", "Emma", +"Alexander", "Lena", +"Noah", "Vanessa", +"Jonas", "Lea", +"Marcel", "Mila", +"Daniel", "Lisa", +"David", "Lina", +"Milan", "Sarah", +"Julian", "Alina", +"Linus", "Emilia", +"Thomas", "Nina", +"Samuel", "Elena", +"Levin", "Lara", +"Levi", "Melanie", +"Jan", "Hannah", +"Lukas", "Sandra", +"Tim", "Leonie", +"Patrick", "Sophie", +"Marvin", "Mia", +"Andreas", "Amelie", +"Leon", "Selina", +"Tobias", "Luisa", +"Simon", "Maria", +"Valentin", "Jana", +"Robin", "Johanna", +"Paul", "Marie", +"Markus", "Milena", +"Benjamin", "Melina", +"Stefan", "Michelle", +"Felix", "Emily", +"Florian", "Renesmee", +"Fabian", "Aylin", +"Emil", "Jessica", +"Aaron", "Franziska", +"Manuel", "Jasmin", +"Christian", "Fiona", +"Dominik", "Sina", +"Joshua", "Jennifer", +"Moritz", "Claudia", +"Sebastian", "Nicole", +"Peter", "Annika", +"Philipp", "Sophia", +"Max", "Katharina", +"Johannes", "Isabella", +"Finn", "Nele", +"Adrian", "Elisabeth", +"Martin", "Pia", diff --git a/20231207/qsort-1.c b/20231207/qsort-1.c new file mode 100644 index 0000000000000000000000000000000000000000..c5431b853862e62812016f4fd3a031eeb0b5c9df --- /dev/null +++ b/20231207/qsort-1.c @@ -0,0 +1,70 @@ +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +int comparisons = 0; + +void display (char **name, char *pivot, int left, int right) +{ + printf ("\e[H\e[J"); + for (int i = 0; name[i]; i++) + { + printf ("%s", name[i]); + if (name[i] == pivot) + printf (" <=="); + else if (i == left || i == right) + printf (" <--"); + printf ("\n"); + } + printf ("%d\n", comparisons); +} + +int compare (char **name, char *pivot, int left, int right) +{ + int result = strcmp (name[left], pivot); + comparisons++; + display (name, pivot, left, right); + usleep (200000); + return result; +} + +void quicksort (char **name, int left, int right) +{ + int p = (left + right) / 2; + char *pivot = name[p]; + int l = left; + int r = right; + while (l < r) + { + while (l < r && compare (name, pivot, l, r - 1) < 0) + l++; + while (l < r && compare (name, pivot, r - 1, l) > 0) + r--; + if (l < r) + { + char *temp = name[r - 1]; + name[r - 1] = name[l]; + name[l] = temp; + l++; + r--; + } + } +} + +void sort (char **name) +{ + int r = 0; + while (name[r]) + r++; + quicksort (name, 0, r); +} + +int main (void) +{ + char *name[] = { "Otto", "Lisa", "Anna", "Heinrich", "Siegfried", "Peter", + "Dieter", "Hugo", "Berta", "Maria", "Fritz", "Box", "Hans", + "Thomas", "Ulrich", "Zacharias", NULL }; + sort (name); + display (name, NULL, -1, -1); + return 0; +} diff --git a/20231207/qsort-2.c b/20231207/qsort-2.c new file mode 100644 index 0000000000000000000000000000000000000000..cfdc2ec11a92a4292c9443e01453cce818e9e73a --- /dev/null +++ b/20231207/qsort-2.c @@ -0,0 +1,72 @@ +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +int comparisons = 0; + +void display (char **name, char *pivot, int left, int right) +{ + printf ("\e[H\e[J"); + for (int i = 0; name[i]; i++) + { + printf ("%s", name[i]); + if (name[i] == pivot) + printf (" <=="); + else if (i == left || i == right) + printf (" <--"); + printf ("\n"); + } + printf ("%d\n", comparisons); +} + +int compare (char **name, char *pivot, int left, int right) +{ + int result = strcmp (name[left], pivot); + comparisons++; + display (name, pivot, left, right); + usleep (200000); + return result; +} + +void quicksort (char **name, int left, int right) +{ + int p = (left + right) / 2; + char *pivot = name[p]; + int l = left; + int r = right; + while (l < r) + { + while (l < r && compare (name, pivot, l, r - 1) < 0) + l++; + while (l < r && compare (name, pivot, r - 1, l) > 0) + r--; + if (l < r) + { + char *temp = name[r - 1]; + name[r - 1] = name[l]; + name[l] = temp; + l++; + r--; + } + } + if (l < right) + quicksort (name, l, right); +} + +void sort (char **name) +{ + int r = 0; + while (name[r]) + r++; + quicksort (name, 0, r); +} + +int main (void) +{ + char *name[] = { "Otto", "Lisa", "Anna", "Heinrich", "Siegfried", "Peter", + "Dieter", "Hugo", "Berta", "Maria", "Fritz", "Box", "Hans", + "Thomas", "Ulrich", "Zacharias", NULL }; + sort (name); + display (name, NULL, -1, -1); + return 0; +} diff --git a/20231207/qsort-3.c b/20231207/qsort-3.c new file mode 100644 index 0000000000000000000000000000000000000000..e6b7d9909473af6d8c13d3745dc6b07e8ea75699 --- /dev/null +++ b/20231207/qsort-3.c @@ -0,0 +1,74 @@ +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +int comparisons = 0; + +void display (char **name, char *pivot, int left, int right) +{ + printf ("\e[H\e[J"); + for (int i = 0; name[i]; i++) + { + printf ("%s", name[i]); + if (name[i] == pivot) + printf (" <=="); + else if (i == left || i == right) + printf (" <--"); + printf ("\n"); + } + printf ("%d\n", comparisons); +} + +int compare (char **name, char *pivot, int left, int right) +{ + int result = strcmp (name[left], pivot); + comparisons++; + display (name, pivot, left, right); + usleep (200000); + return result; +} + +void quicksort (char **name, int left, int right) +{ + int p = (left + right) / 2; + char *pivot = name[p]; + int l = left; + int r = right; + while (l < r) + { + while (l < r && compare (name, pivot, l, r - 1) < 0) + l++; + while (l < r && compare (name, pivot, r - 1, l) > 0) + r--; + if (l < r) + { + char *temp = name[r - 1]; + name[r - 1] = name[l]; + name[l] = temp; + l++; + r--; + } + } + if (r > left) + quicksort (name, left, r); + if (l < right) + quicksort (name, l, right); +} + +void sort (char **name) +{ + int r = 0; + while (name[r]) + r++; + quicksort (name, 0, r); +} + +int main (void) +{ + char *name[] = { "Otto", "Lisa", "Anna", "Heinrich", "Siegfried", "Peter", + "Dieter", "Hugo", "Berta", "Maria", "Fritz", "Box", "Hans", + "Thomas", "Ulrich", "Zacharias", NULL }; + sort (name); + display (name, NULL, -1, -1); + return 0; +} diff --git a/20231207/qsort-3a.c b/20231207/qsort-3a.c new file mode 100644 index 0000000000000000000000000000000000000000..d3e757ee78e451a57769b29438713ebe5202c2b4 --- /dev/null +++ b/20231207/qsort-3a.c @@ -0,0 +1,74 @@ +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +int comparisons = 0; + +void display (char **name, char *pivot, int left, int right) +{ + printf ("\e[H\e[J"); + for (int i = 0; name[i]; i++) + { + printf ("%s", name[i]); + if (name[i] == pivot) + printf (" <=="); + else if (i == left || i == right) + printf (" <--"); + printf ("\n"); + } + printf ("%d\n", comparisons); +} + +int compare (char **name, char *pivot, int left, int right) +{ + int result = strcmp (name[left], pivot); + comparisons++; + display (name, pivot, left, right); + usleep (200000); + return result; +} + +void quicksort (char **name, int left, int right) +{ + int p = (left + right) / 2; + char *pivot = name[p]; + int l = left; + int r = right; + while (l < r) + { + while (l < r && compare (name, pivot, l, r - 1) < 0) + l++; + while (l < r && compare (name, pivot, r - 1, l) > 0) + r--; + if (l < r) + { + char *temp = name[r - 1]; + name[r - 1] = name[l]; + name[l] = temp; + l++; + r--; + } + } + if (r > left) + quicksort (name, left, r); + if (l < right) + quicksort (name, l, right); +} + +void sort (char **name) +{ + int r = 0; + while (name[r]) + r++; + quicksort (name, 0, r); +} + +int main (void) +{ + char *name[] = { "Anna", "Berta", "Box", "Dieter", "Fritz", "Hans", "Heinrich", "Hugo", + "Lisa", "Maria", "Otto", "Peter", "Siegfried", "Thomas", "Ulrich", + "Zacharias", NULL }; + sort (name); + display (name, NULL, -1, -1); + return 0; +} diff --git a/20231207/qsort-3b.c b/20231207/qsort-3b.c new file mode 100644 index 0000000000000000000000000000000000000000..0bafef40b8500bed175ab2b53c28ea97494bfe7b --- /dev/null +++ b/20231207/qsort-3b.c @@ -0,0 +1,74 @@ +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +int comparisons = 0; + +void display (char **name, char *pivot, int left, int right) +{ + printf ("\e[H\e[J"); + for (int i = 0; name[i]; i++) + { + printf ("%s", name[i]); + if (name[i] == pivot) + printf (" <=="); + else if (i == left || i == right) + printf (" <--"); + printf ("\n"); + } + printf ("%d\n", comparisons); +} + +int compare (char **name, char *pivot, int left, int right) +{ + int result = strcmp (name[left], pivot); + comparisons++; + display (name, pivot, left, right); + usleep (200000); + return result; +} + +void quicksort (char **name, int left, int right) +{ + int p = (left + right) / 2; + char *pivot = name[p]; + int l = left; + int r = right; + while (l < r) + { + while (l < r && compare (name, pivot, l, r - 1) < 0) + l++; + while (l < r && compare (name, pivot, r - 1, l) > 0) + r--; + if (l < r) + { + char *temp = name[r - 1]; + name[r - 1] = name[l]; + name[l] = temp; + l++; + r--; + } + } + if (r > left) + quicksort (name, left, r); + if (l < right) + quicksort (name, l, right); +} + +void sort (char **name) +{ + int r = 0; + while (name[r]) + r++; + quicksort (name, 0, r); +} + +int main (void) +{ + char *name[] = { "Zacharias", "Ulrich", "Thomas", "Siegfried", "Peter", "Otto", "Maria", + "Lisa", "Hugo", "Heinrich", "Hans", "Fritz", "Dieter", "Box", "Berta", + "Anna", NULL }; + sort (name); + display (name, NULL, -1, -1); + return 0; +} diff --git a/20231207/sort-0.c b/20231207/sort-0.c new file mode 100644 index 0000000000000000000000000000000000000000..70e0e717cbfd42a27bf90f419c216fa18b0783c7 --- /dev/null +++ b/20231207/sort-0.c @@ -0,0 +1,16 @@ +#include <stdio.h> + +int find_first (char **name) +{ + return 2; +} + +int main (void) +{ + char *name[] = { "Otto", "Lisa", "Anna", "Heinrich", "Siegfried", "Peter", + "Dieter", "Hugo", "Berta", "Maria", "Fritz", "Box", "Hans", + "Thomas", "Ulrich", "Zacharias", NULL }; + int first = find_first (name); + printf ("%s\n", name[first]); + return 0; +} diff --git a/20231207/sort-1.c b/20231207/sort-1.c new file mode 100644 index 0000000000000000000000000000000000000000..3f009d45657f84e462db48a67fa22821819216ad --- /dev/null +++ b/20231207/sort-1.c @@ -0,0 +1,21 @@ +#include <stdio.h> +#include <string.h> + +int find_first (char **name) +{ + int first = 0; + for (int i = 1; name[i]; i++) + if (strcmp (name[i], name[first]) < 0) + first = i; + return first; +} + +int main (void) +{ + char *name[] = { "Otto", "Lisa", "Anna", "Heinrich", "Siegfried", "Peter", + "Dieter", "Hugo", "Berta", "Maria", "Fritz", "Box", "Hans", + "Thomas", "Ulrich", "Zacharias", NULL }; + int first = find_first (name); + printf ("%s\n", name[first]); + return 0; +} diff --git a/20231207/sort-1a.c b/20231207/sort-1a.c new file mode 100644 index 0000000000000000000000000000000000000000..e60f10393cf93897697f888c7ec0107c5313edfe --- /dev/null +++ b/20231207/sort-1a.c @@ -0,0 +1,21 @@ +#include <stdio.h> +#include <string.h> + +int find_first (char **name) +{ + int first = 0; + for (int i = 1; name[i]; i++) + if (strcmp (name[i], name[first]) < 0) + first = i; + return first; +} + +int main (void) +{ + char *name[] = { "otto", "Lisa", "Anna", "Heinrich", "Siegfried", "Peter", + "Dieter", "Hugo", "Berta", "Maria", "Fritz", "Box", "Hans", + "Thomas", "Ulrich", "Zacharias", NULL }; + int first = find_first (name); + printf ("%s\n", name[first]); + return 0; +} diff --git a/20231207/sort-2.c b/20231207/sort-2.c new file mode 100644 index 0000000000000000000000000000000000000000..638e6e2caa670ceace0a854d0827292dfcb59ca5 --- /dev/null +++ b/20231207/sort-2.c @@ -0,0 +1,46 @@ +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +int comparisons = 0; + +void display (char **name, int left, int right) +{ + printf ("\e[H\e[J"); + for (int i = 0; name[i]; i++) + { + printf ("%s", name[i]); + if (i == left || i == right) + printf (" <--"); + printf ("\n"); + } + printf ("%d\n", comparisons); +} + +int compare (char **name, int left, int right) +{ + int result = strcmp (name[left], name[right]); + comparisons++; + display (name, left, right); + usleep (200000); + return result; +} + +int find_first (char **name) +{ + int first = 0; + for (int i = 1; name[i]; i++) + if (compare (name, i, first) < 0) + first = i; + return first; +} + +int main (void) +{ + char *name[] = { "Otto", "Lisa", "Anna", "Heinrich", "Siegfried", "Peter", + "Dieter", "Hugo", "Berta", "Maria", "Fritz", "Box", "Hans", + "Thomas", "Ulrich", "Zacharias", NULL }; + int first = find_first (name); + display (name, first, -1); + return 0; +} diff --git a/20231207/sort-3.c b/20231207/sort-3.c new file mode 100644 index 0000000000000000000000000000000000000000..430a6fc0e95fcb9bc225e3572566c8d187016130 --- /dev/null +++ b/20231207/sort-3.c @@ -0,0 +1,46 @@ +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +int comparisons = 0; + +void display (char **name, int left, int right) +{ + printf ("\e[H\e[J"); + for (int i = 0; name[i]; i++) + { + printf ("%s", name[i]); + if (i == left || i == right) + printf (" <--"); + printf ("\n"); + } + printf ("%d\n", comparisons); +} + +int compare (char **name, int left, int right) +{ + int result = strcmp (name[left], name[right]); + comparisons++; + display (name, left, right); + usleep (200000); + return result; +} + +int find_first (char **name, int i0) +{ + int first = i0; + for (int i = i0 + 1; name[i]; i++) + if (compare (name, i, first) < 0) + first = i; + return first; +} + +int main (void) +{ + char *name[] = { "Otto", "Lisa", "Anna", "Heinrich", "Siegfried", "Peter", + "Dieter", "Hugo", "Berta", "Maria", "Fritz", "Box", "Hans", + "Thomas", "Ulrich", "Zacharias", NULL }; + int first = find_first (name, 0); + display (name, first, -1); + return 0; +} diff --git a/20231207/sort-4.c b/20231207/sort-4.c new file mode 100644 index 0000000000000000000000000000000000000000..19d4f416a2d30c5181ad289fe1d0dcba91ee81cf --- /dev/null +++ b/20231207/sort-4.c @@ -0,0 +1,59 @@ +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +int comparisons = 0; + +void display (char **name, int left, int right) +{ + printf ("\e[H\e[J"); + for (int i = 0; name[i]; i++) + { + printf ("%s", name[i]); + if (i == left || i == right) + printf (" <--"); + printf ("\n"); + } + printf ("%d\n", comparisons); +} + +int compare (char **name, int left, int right) +{ + int result = strcmp (name[left], name[right]); + comparisons++; + display (name, left, right); + usleep (200000); + return result; +} + +int find_first (char **name, int i0) +{ + int first = i0; + for (int i = i0 + 1; name[i]; i++) + if (compare (name, i, first) < 0) + first = i; + return first; +} + +void sort (char **name) +{ + int sorted = 0; + while (name[sorted]) + { + int first = find_first (name, sorted); + char *temp = name[sorted]; + name[sorted] = name[first]; + name[first] = temp; + sorted++; + } +} + +int main (void) +{ + char *name[] = { "Otto", "Lisa", "Anna", "Heinrich", "Siegfried", "Peter", + "Dieter", "Hugo", "Berta", "Maria", "Fritz", "Box", "Hans", + "Thomas", "Ulrich", "Zacharias", NULL }; + sort (name); + display (name, -1, -1); + return 0; +} diff --git a/20231207/sort-4a.c b/20231207/sort-4a.c new file mode 100644 index 0000000000000000000000000000000000000000..8312d2ba96ee6afe3dd1d50a440028bba6386e00 --- /dev/null +++ b/20231207/sort-4a.c @@ -0,0 +1,59 @@ +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +int comparisons = 0; + +void display (char **name, int left, int right) +{ + printf ("\e[H\e[J"); + for (int i = 0; name[i]; i++) + { + printf ("%s", name[i]); + if (i == left || i == right) + printf (" <--"); + printf ("\n"); + } + printf ("%d\n", comparisons); +} + +int compare (char **name, int left, int right) +{ + int result = strcmp (name[left], name[right]); + comparisons++; + display (name, left, right); + usleep (200000); + return result; +} + +int find_first (char **name, int i0) +{ + int first = i0; + for (int i = i0 + 1; name[i]; i++) + if (compare (name, i, first) < 0) + first = i; + return first; +} + +void sort (char **name) +{ + int sorted = 0; + while (name[sorted]) + { + int first = find_first (name, sorted); + char *temp = name[sorted]; + name[sorted] = name[first]; + name[first] = temp; + sorted++; + } +} + +int main (void) +{ + char *name[] = { "Anna", "Berta", "Box", "Dieter", "Fritz", "Hans", "Heinrich", "Hugo", + "Lisa", "Maria", "Otto", "Peter", "Siegfried", "Thomas", "Ulrich", + "Zacharias", NULL }; + sort (name); + display (name, -1, -1); + return 0; +} diff --git a/20231207/sort-5.c b/20231207/sort-5.c new file mode 100644 index 0000000000000000000000000000000000000000..f0742bc8665d0b13edf4ad4ad3fe4790c1e6b929 --- /dev/null +++ b/20231207/sort-5.c @@ -0,0 +1,60 @@ +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +int comparisons = 0; + +void display (char **name, int left, int right) +{ + printf ("\e[H\e[J"); + for (int i = 0; name[i]; i++) + { + printf ("%s", name[i]); + if (i == left || i == right) + printf (" <--"); + printf ("\n"); + } + printf ("%d\n", comparisons); +} + +int compare (char **name, int left, int right) +{ + int result = strcmp (name[left], name[right]); + comparisons++; + display (name, left, right); + usleep (200); + return result; +} + +int find_first (char **name, int i0) +{ + int first = i0; + for (int i = i0 + 1; name[i]; i++) + if (compare (name, i, first) < 0) + first = i; + return first; +} + +void sort (char **name) +{ + int sorted = 0; + while (name[sorted]) + { + int first = find_first (name, sorted); + char *temp = name[sorted]; + name[sorted] = name[first]; + name[first] = temp; + sorted++; + } +} + +int main (void) +{ + char *name[] = { + #include "names.h" + NULL + }; + sort (name); + display (name, -1, -1); + return 0; +} diff --git a/20231207/sort-6.c b/20231207/sort-6.c new file mode 100644 index 0000000000000000000000000000000000000000..94a1a16abfacda7bb728f8d3317cd00b4cf8ccde --- /dev/null +++ b/20231207/sort-6.c @@ -0,0 +1,60 @@ +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +int comparisons = 0; + +void display (char **name, int left, int right) +{ + printf ("\e[H\e[J"); + for (int i = 0; name[i]; i++) + { + printf ("%s", name[i]); + if (i == left || i == right) + printf (" <--"); + printf ("\n"); + } + printf ("%d\n", comparisons); +} + +int compare (char **name, int left, int right) +{ + int result = strcmp (name[left], name[right]); + comparisons++; +// display (name, left, right); +// usleep (200); + return result; +} + +int find_first (char **name, int i0) +{ + int first = i0; + for (int i = i0 + 1; name[i]; i++) + if (compare (name, i, first) < 0) + first = i; + return first; +} + +void sort (char **name) +{ + int sorted = 0; + while (name[sorted]) + { + int first = find_first (name, sorted); + char *temp = name[sorted]; + name[sorted] = name[first]; + name[first] = temp; + sorted++; + } +} + +int main (void) +{ + char *name[] = { + #include "names.h" + NULL + }; + sort (name); + display (name, -1, -1); + return 0; +} diff --git a/20231207/sort-7.c b/20231207/sort-7.c new file mode 100644 index 0000000000000000000000000000000000000000..46132dd247a061dda0e5ebade9c5b818a1a74fcb --- /dev/null +++ b/20231207/sort-7.c @@ -0,0 +1,53 @@ +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +int comparisons = 0; + +void display (char **name, int left, int right) +{ + printf ("\e[H\e[J"); + for (int i = 0; name[i]; i++) + { + printf ("%s", name[i]); + if (i == left || i == right) + printf (" <--"); + printf ("\n"); + } + printf ("%d\n", comparisons); +} + +int compare (char **name, int left, int right) +{ + int result = strcmp (name[left], name[right]); + comparisons++; + display (name, left, right); + usleep (200000); + return result; +} + +void sort (char **name) +{ + int sorted = 0; + while (name[sorted]) + { + int first = sorted; + for (int i = sorted + 1; name[i]; i++) + if (compare (name, i, first) < 0) + first = i; + char *temp = name[sorted]; + name[sorted] = name[first]; + name[first] = temp; + sorted++; + } +} + +int main (void) +{ + char *name[] = { "Otto", "Lisa", "Anna", "Heinrich", "Siegfried", "Peter", + "Dieter", "Hugo", "Berta", "Maria", "Fritz", "Box", "Hans", + "Thomas", "Ulrich", "Zacharias", NULL }; + sort (name); + display (name, -1, -1); + return 0; +}