diff --git a/20210107/bsort-1.c b/20210107/bsort-1.c new file mode 100644 index 0000000000000000000000000000000000000000..3c02adc72e6a7e702e2c7c68c838fc94cc8ef547 --- /dev/null +++ b/20210107/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/20210107/bsort-1a.c b/20210107/bsort-1a.c new file mode 100644 index 0000000000000000000000000000000000000000..348b638843002926f57cdcf1d4fbc9bd88d2a10c --- /dev/null +++ b/20210107/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/20210107/bsort-2.c b/20210107/bsort-2.c new file mode 100644 index 0000000000000000000000000000000000000000..d277be0182a40ecd0c66eaeef94d7aebac6aa542 --- /dev/null +++ b/20210107/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/20210107/bsort-3.c b/20210107/bsort-3.c new file mode 100644 index 0000000000000000000000000000000000000000..67d0097b913171b4523f4f04b6b6ff494046dabd --- /dev/null +++ b/20210107/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/20210107/bsort-4.c b/20210107/bsort-4.c new file mode 100644 index 0000000000000000000000000000000000000000..241270a46f4ffa005d252207d769c3c4059ed7e2 --- /dev/null +++ b/20210107/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/20210107/hanoi-0.c b/20210107/hanoi-0.c new file mode 100644 index 0000000000000000000000000000000000000000..83b5f081e33e699f2d5eaa1f858320530e1b2820 --- /dev/null +++ b/20210107/hanoi-0.c @@ -0,0 +1,26 @@ +#include <stdio.h> + +#define DISKS 4 + +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"); + } +} + +int main (void) +{ + n[0] = 0; + n[1] = 0; + n[2] = 0; + display (); + return 0; +} diff --git a/20210107/hanoi-1.c b/20210107/hanoi-1.c new file mode 100644 index 0000000000000000000000000000000000000000..6d005f0b12afa6086afb65e1794fb5c977b474e2 --- /dev/null +++ b/20210107/hanoi-1.c @@ -0,0 +1,30 @@ +#include <stdio.h> + +#define DISKS 4 + +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"); + } +} + +int main (void) +{ + n[0] = 4; + tower[0][0] = 4; + tower[0][1] = 3; + tower[0][2] = 2; + tower[0][3] = 1; + n[1] = 0; + n[2] = 0; + display (); + return 0; +} diff --git a/20210107/hanoi-2.c b/20210107/hanoi-2.c new file mode 100644 index 0000000000000000000000000000000000000000..c928c1130539da08d0882249bdffc877d72289b2 --- /dev/null +++ b/20210107/hanoi-2.c @@ -0,0 +1,28 @@ +#include <stdio.h> + +#define DISKS 4 + +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"); + } +} + +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 (); + return 0; +} diff --git a/20210107/hanoi-3.c b/20210107/hanoi-3.c new file mode 100644 index 0000000000000000000000000000000000000000..4b59c046962c1f79eca315882d93faf6b6c298c1 --- /dev/null +++ b/20210107/hanoi-3.c @@ -0,0 +1,44 @@ +#include <stdio.h> +#include <error.h> + +#define DISKS 4 + +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]--; + } +} + +int main (void) +{ + n[0] = DISKS; + for (int i = 0; i < DISKS; i++) + tower[0][i] = DISKS - i; + n[1] = 0; + n[2] = 0; + move (2, 0, 1); + display (); + return 0; +} diff --git a/20210107/hanoi-4.c b/20210107/hanoi-4.c new file mode 100644 index 0000000000000000000000000000000000000000..162a5ba7cccedf64bff8ae9a514aaf146088e3a4 --- /dev/null +++ b/20210107/hanoi-4.c @@ -0,0 +1,44 @@ +#include <stdio.h> +#include <error.h> + +#define DISKS 4 + +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]--; + } +} + +int main (void) +{ + n[0] = DISKS; + for (int i = 0; i < DISKS; i++) + tower[0][i] = DISKS - i; + n[1] = 0; + n[2] = 0; + move (0, 2, 1); + display (); + return 0; +} diff --git a/20210107/hanoi-5.c b/20210107/hanoi-5.c new file mode 100644 index 0000000000000000000000000000000000000000..e04ad85f63fe92e5c15d22646beb9e9107bf78d6 --- /dev/null +++ b/20210107/hanoi-5.c @@ -0,0 +1,45 @@ +#include <stdio.h> +#include <error.h> + +#define DISKS 4 + +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]--; + display (); + } +} + +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, 1); + return 0; +} diff --git a/20210107/hanoi-6.c b/20210107/hanoi-6.c new file mode 100644 index 0000000000000000000000000000000000000000..1267605f802246e857c32d29cc0a132609cb24ce --- /dev/null +++ b/20210107/hanoi-6.c @@ -0,0 +1,52 @@ +#include <stdio.h> +#include <error.h> + +#define DISKS 4 + +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]--; + display (); + } + 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); + return 0; +} diff --git a/20210107/hanoi-7.c b/20210107/hanoi-7.c new file mode 100644 index 0000000000000000000000000000000000000000..f71fcdc0c3f66047ee3ad870d019a06eb328050f --- /dev/null +++ b/20210107/hanoi-7.c @@ -0,0 +1,52 @@ +#include <stdio.h> +#include <error.h> + +#define DISKS 5 + +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]--; + display (); + } + 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); + return 0; +} diff --git a/20210107/hanoi-8.c b/20210107/hanoi-8.c new file mode 100644 index 0000000000000000000000000000000000000000..6d804063531d424a82924a89d85424c33994793b --- /dev/null +++ b/20210107/hanoi-8.c @@ -0,0 +1,52 @@ +#include <stdio.h> +#include <error.h> + +#define DISKS 64 + +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]--; + display (); + } + 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); + return 0; +} diff --git a/20210107/hanoi-9.c b/20210107/hanoi-9.c new file mode 100644 index 0000000000000000000000000000000000000000..7694b373b7011e96f6be0ad0f00ba54b67086158 --- /dev/null +++ b/20210107/hanoi-9.c @@ -0,0 +1,58 @@ +#include <stdio.h> +#include <error.h> + +#define DISKS 64 + +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/20210107/hanoi-9a.c b/20210107/hanoi-9a.c new file mode 100644 index 0000000000000000000000000000000000000000..55e8d74db6d7284d6db225a6f8f3e08ab4efdc06 --- /dev/null +++ b/20210107/hanoi-9a.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/20210107/qsort-1.c b/20210107/qsort-1.c new file mode 100644 index 0000000000000000000000000000000000000000..c5431b853862e62812016f4fd3a031eeb0b5c9df --- /dev/null +++ b/20210107/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/20210107/qsort-1a.c b/20210107/qsort-1a.c new file mode 100644 index 0000000000000000000000000000000000000000..a3d4d0011834aa3d85907c2a0d0e90e3bf80799e --- /dev/null +++ b/20210107/qsort-1a.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 - 1; + 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/20210107/qsort-2.c b/20210107/qsort-2.c new file mode 100644 index 0000000000000000000000000000000000000000..cfdc2ec11a92a4292c9443e01453cce818e9e73a --- /dev/null +++ b/20210107/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/20210107/qsort-3.c b/20210107/qsort-3.c new file mode 100644 index 0000000000000000000000000000000000000000..e6b7d9909473af6d8c13d3745dc6b07e8ea75699 --- /dev/null +++ b/20210107/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/20210107/sort-0.c b/20210107/sort-0.c new file mode 100644 index 0000000000000000000000000000000000000000..70e0e717cbfd42a27bf90f419c216fa18b0783c7 --- /dev/null +++ b/20210107/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/20210107/sort-1.c b/20210107/sort-1.c new file mode 100644 index 0000000000000000000000000000000000000000..3f009d45657f84e462db48a67fa22821819216ad --- /dev/null +++ b/20210107/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/20210107/sort-2.c b/20210107/sort-2.c new file mode 100644 index 0000000000000000000000000000000000000000..638e6e2caa670ceace0a854d0827292dfcb59ca5 --- /dev/null +++ b/20210107/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/20210107/sort-3.c b/20210107/sort-3.c new file mode 100644 index 0000000000000000000000000000000000000000..430a6fc0e95fcb9bc225e3572566c8d187016130 --- /dev/null +++ b/20210107/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/20210107/sort-4.c b/20210107/sort-4.c new file mode 100644 index 0000000000000000000000000000000000000000..19d4f416a2d30c5181ad289fe1d0dcba91ee81cf --- /dev/null +++ b/20210107/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/20210107/sort-5.c b/20210107/sort-5.c new file mode 100644 index 0000000000000000000000000000000000000000..f0742bc8665d0b13edf4ad4ad3fe4790c1e6b929 --- /dev/null +++ b/20210107/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/20210107/sort-6.c b/20210107/sort-6.c new file mode 100644 index 0000000000000000000000000000000000000000..94a1a16abfacda7bb728f8d3317cd00b4cf8ccde --- /dev/null +++ b/20210107/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/20210107/sort-7.c b/20210107/sort-7.c new file mode 100644 index 0000000000000000000000000000000000000000..46132dd247a061dda0e5ebade9c5b818a1a74fcb --- /dev/null +++ b/20210107/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; +}