diff --git a/script/bsort-1.c b/script/bsort-1.c new file mode 100644 index 0000000000000000000000000000000000000000..3c02adc72e6a7e702e2c7c68c838fc94cc8ef547 --- /dev/null +++ b/script/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/script/bsort-1a.c b/script/bsort-1a.c new file mode 100644 index 0000000000000000000000000000000000000000..348b638843002926f57cdcf1d4fbc9bd88d2a10c --- /dev/null +++ b/script/bsort-1a.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", "Zacharias", "Berta", "Maria", "Fritz", "Box", "Hans", + "Thomas", "Ulrich", NULL }; + sort (name); + display (name, -1, -1); + return 0; +} diff --git a/script/bsort-2.c b/script/bsort-2.c new file mode 100644 index 0000000000000000000000000000000000000000..d277be0182a40ecd0c66eaeef94d7aebac6aa542 --- /dev/null +++ b/script/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/script/bsort-3.c b/script/bsort-3.c new file mode 100644 index 0000000000000000000000000000000000000000..67d0097b913171b4523f4f04b6b6ff494046dabd --- /dev/null +++ b/script/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/script/bsort-4.c b/script/bsort-4.c new file mode 100644 index 0000000000000000000000000000000000000000..241270a46f4ffa005d252207d769c3c4059ed7e2 --- /dev/null +++ b/script/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/script/hp-2019ws.pdf b/script/hp-2019ws.pdf index 7e5984ff3f3cddf957034b1d6e06caa0feb86cf3..eab13eeb248cdd96d7ae143e812fffbbffde1e07 100644 Binary files a/script/hp-2019ws.pdf and b/script/hp-2019ws.pdf differ diff --git a/script/hp-2019ws.tex b/script/hp-2019ws.tex index 95c14f213ded265073eebfc23379d370acfc35cf..9f44ecd7250c6a89ec08983eb70536f20ac14948 100644 --- a/script/hp-2019ws.tex +++ b/script/hp-2019ws.tex @@ -78,7 +78,7 @@ \begin{center} {\Huge\textbf{Hardwarenahe Programmierung}\par} \vspace{2cm} - {\large Wintersemester 2018/19\\[\medskipamount] + {\large Wintersemester 2019/20\\[\medskipamount] Prof.~Dr.~rer.~nat.~Peter Gerwinski} \end{center} \vfill @@ -91,10 +91,10 @@ \strut\vfill - Stand: 2.\ Dezember 2018 + Stand: 11.\ Januar 2020 Soweit nicht anders angegeben:\\ - Text und Bilder: Copyright \copyright\ 2012, 2013, 2015, 2016, 2018\quad Peter Gerwinski\\ + Text und Bilder: Copyright \copyright\ 2012, 2013, 2015, 2016, 2018, 2020\quad Peter Gerwinski\\ Lizenz: \mylicense Sie können dieses Skript @@ -647,7 +647,8 @@ \begin{lstlisting}[style=terminal] $ ¡gcc output-4.c -o output-4¿ $ ¡./output-4¿ - Richtige Antworten wären 1$ + Richtige Antworten wären 1 + $ \end{lstlisting} Bei Verwendung der Option \lstinline[style=cmd]{-Wall} erhalten wir auch hier eine Warnung: @@ -712,6 +713,10 @@ Weitere Informationen zu den Formatspezifikationen von \lstinline{scanf()} finden Sie in der Dokumentation zu \lstinline{scanf()}. (In der Unix-Shell können Sie diese mit dem Befehl \lstinline[style=cmd]{man 3 scanf} abrufen.) + + Für das Einlesen von Strings ist \lstinline{scanf()} eher ungeeignet. + Hier empfiehlt es sich, stattdessen \lstinline{fgets()} zu benutzen + (siehe \lstinline[style=cmd]{man 3 fgets}). \end{experts} \subsection{Elementares Rechnen} @@ -1508,7 +1513,7 @@ Es ist daher besser, \lstinline{goto} nicht zu verwenden und stattdessen den Programmablauf mit Hilfe von Verzweigungen und Schleifen zu strukturieren. - (Siehe auch: \url{http://xkcd.org/292/}) + (Siehe auch: \url{http://xkcd.com/292/}) Zusammengefaßt: \begin{hint} @@ -4894,19 +4899,27 @@ \begin{itemize} \item - Minimum/Maximum ermitteln: \gitfile{hp}{script}{sort-0.c}, - \gitfile{hp}{script}{sort-1.c} und \gitfile{hp}{script}{sort-2.c} - \item - Selectionsort: \gitfile{hp}{script}{sort-3.c}, \gitfile{hp}{script}{sort-4.c} - \item - Bubblesort: \gitfile{hp}{script}{sort-5.c} - \item - Bubblesort mit Abbruch in äußerer Schleife: \gitfile{hp}{script}{sort-6.c} - \item - Bubblesort mit Abbruch in innerer Schleife: \gitfile{hp}{script}{sort-7.c}, - \gitfile{hp}{script}{sort-7a.c}, \gitfile{hp}{script}{sort-7b.c} - \item - Vergleich mit Quicksort: \gitfile{hp}{script}{sort-8.c}, \gitfile{hp}{script}{sort-8a.c}, \gitfile{hp}{script}{sort-8b.c} + Minimum/Maximum ermitteln: + \gitfile{hp}{script}{sort-0.c} (mit "`Schummeln"'), + \gitfile{hp}{script}{sort-1.c} (lineare Suche), + \gitfile{hp}{script}{sort-2.c} (mit Visualisierung) + \item + Selectionsort: + \gitfile{hp}{script}{sort-3.c} (bei Minimumsuche Anfang des Arrays überspringen), + \gitfile{hp}{script}{sort-4.c} (Selectionsort), + \gitfile{hp}{script}{sort-5.c} (100 Namen), + \gitfile{hp}{script}{sort-6.c} (100 Namen, ohne Visualisierung) + \item + Bubblesort: + \gitfile{hp}{script}{sort-7.c} (Selectionsort, Minimumsuche mit in der Hauptschleife), + \gitfile{hp}{script}{bsort-1.c} (Minimumsuche durch Vergleich benachbarter Strings), + \gitfile{hp}{script}{bsort-2.c} (Abbruch in äußerer Schleife, sobald sortiert), + \gitfile{hp}{script}{bsort-3.c} (Abbruch auch in innerer Schleife, sobald sortiert) + \item + Quicksort: + \gitfile{hp}{script}{qsort-1.c} (Array in 2 Hälften vorsortieren), + \gitfile{hp}{script}{qsort-2.c} (rekursiver Aufruf für linke Hälfte), + \gitfile{hp}{script}{qsort-3.c} (rekursiver Aufruf für beide Hälften) \end{itemize} Bei "`zufällig"' sortierten Ausgangsdaten arbeitet Quicksort schneller als Bubblesort. diff --git a/script/qsort-1.c b/script/qsort-1.c new file mode 100644 index 0000000000000000000000000000000000000000..c5431b853862e62812016f4fd3a031eeb0b5c9df --- /dev/null +++ b/script/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/script/qsort-2.c b/script/qsort-2.c new file mode 100644 index 0000000000000000000000000000000000000000..cfdc2ec11a92a4292c9443e01453cce818e9e73a --- /dev/null +++ b/script/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/script/qsort-3.c b/script/qsort-3.c new file mode 100644 index 0000000000000000000000000000000000000000..e6b7d9909473af6d8c13d3745dc6b07e8ea75699 --- /dev/null +++ b/script/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/script/sort-0.c b/script/sort-0.c new file mode 100644 index 0000000000000000000000000000000000000000..70e0e717cbfd42a27bf90f419c216fa18b0783c7 --- /dev/null +++ b/script/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/script/sort-1.c b/script/sort-1.c new file mode 100644 index 0000000000000000000000000000000000000000..3f009d45657f84e462db48a67fa22821819216ad --- /dev/null +++ b/script/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/script/sort-2.c b/script/sort-2.c new file mode 100644 index 0000000000000000000000000000000000000000..638e6e2caa670ceace0a854d0827292dfcb59ca5 --- /dev/null +++ b/script/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/script/sort-3.c b/script/sort-3.c new file mode 100644 index 0000000000000000000000000000000000000000..430a6fc0e95fcb9bc225e3572566c8d187016130 --- /dev/null +++ b/script/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/script/sort-4.c b/script/sort-4.c new file mode 100644 index 0000000000000000000000000000000000000000..19d4f416a2d30c5181ad289fe1d0dcba91ee81cf --- /dev/null +++ b/script/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/script/sort-5.c b/script/sort-5.c new file mode 100644 index 0000000000000000000000000000000000000000..f0742bc8665d0b13edf4ad4ad3fe4790c1e6b929 --- /dev/null +++ b/script/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/script/sort-6.c b/script/sort-6.c new file mode 100644 index 0000000000000000000000000000000000000000..94a1a16abfacda7bb728f8d3317cd00b4cf8ccde --- /dev/null +++ b/script/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/script/sort-7.c b/script/sort-7.c new file mode 100644 index 0000000000000000000000000000000000000000..46132dd247a061dda0e5ebade9c5b818a1a74fcb --- /dev/null +++ b/script/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; +}