From 53d3fc101a6f835ce3cfc7d43c8259394005e11e Mon Sep 17 00:00:00 2001 From: BijanRiesenberg Date: Tue, 22 Feb 2022 01:28:52 +0100 Subject: [PATCH 01/10] feat: add dutch national flag sort --- sorting/dnf_sort.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 sorting/dnf_sort.c diff --git a/sorting/dnf_sort.c b/sorting/dnf_sort.c new file mode 100644 index 0000000000..8efd8204dc --- /dev/null +++ b/sorting/dnf_sort.c @@ -0,0 +1,92 @@ +/** + * @file + * @brief Program to perform [dutch national + * flag](https://en.wikipedia.org/wiki/Dutch_national_flag_problem) of a target + * value in a given {0, 1, 2} containing array. + * @authors [Bijan Riesenberg](https://github.com/Data-Hero) Iterative + */ +#include +#include +#include + +/** + * @brief Dutch National Flag 3-Way sort algorithm + * @param arr array containting elements from {0, 1, 2} to be sorted + * @param size size of the array + * @returns void + */ +void dnfSort(int8_t *arr, int size) +{ + int low = 0, mid = 0, high = size - 1; + int8_t temp = 0; + while (mid <= high) + { + switch (arr[mid]) + { + case 0: + temp = arr[low]; + arr[low++] = arr[mid]; + arr[mid++] = temp; + break; + case 1: + ++mid; + break; + case 2: + temp = arr[mid]; + arr[mid++] = arr[high]; + arr[high--] = temp; + break; + default: + break; + } + } +} + +/** + * @brief Print an array containing int8_t elements + * + * @param arr array containting elements from {0, 1, 2} (int8_t) to be printed + * @param size size of the array + */ +void printArray(int8_t *arr, int size) +{ + for (int c = 0; c < size; c++) + { + c != size - 1 ? printf("%" PRIi8 ", ", arr[c]) + : printf("%" PRIi8 "\n", arr[c]); + } +} + +/** + * @brief input an array of {0, 1, 2} and use the + * Dutch National Flag 3-Way sort algorithm to sort + * + * @return int 0 if successful 1 if input is not in {0, 1, 2} + */ +int main(void) +{ + int n; + printf("Enter size of array:\n"); + scanf("%d", &n); // E.g. 8 + + printf("Enter the elements in {0, 1, 2} of the array\n"); + int i; + int8_t *arr = (int8_t *)malloc(sizeof(int8_t) * n); + for (i = 0; i < n; i++) + { + scanf("%" SCNi8, &arr[i]); + if (arr[i] > 2 || arr[i] < 0) + { + printf("Elements need to be in {0, 1, 2}"); + return 1; + } + } + printf("Original array: \n"); + printArray(arr, n); + dnfSort(arr, n); + printf("Sorted array: \n"); + printArray(arr, n); + + free(arr); + return 0; +} From b8ff22e81a81756c8fbbb204255d3ff5cb3ec74e Mon Sep 17 00:00:00 2001 From: BijanRiesenberg Date: Tue, 22 Feb 2022 02:16:52 +0100 Subject: [PATCH 02/10] test: add test for dnf_sort algorithm --- sorting/dnf_sort.c | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/sorting/dnf_sort.c b/sorting/dnf_sort.c index 8efd8204dc..3b3f1525ca 100644 --- a/sorting/dnf_sort.c +++ b/sorting/dnf_sort.c @@ -10,10 +10,9 @@ #include /** - * @brief Dutch National Flag 3-Way sort algorithm + * @brief Dutch National Flag 3-Way inplace sort algorithm * @param arr array containting elements from {0, 1, 2} to be sorted * @param size size of the array - * @returns void */ void dnfSort(int8_t *arr, int size) { @@ -58,35 +57,45 @@ void printArray(int8_t *arr, int size) } /** - * @brief input an array of {0, 1, 2} and use the + * @brief Input an array of {0, 1, 2} and use the * Dutch National Flag 3-Way sort algorithm to sort * * @return int 0 if successful 1 if input is not in {0, 1, 2} */ int main(void) { - int n; + int n = 3; + int8_t *arr1 = (int8_t *)malloc(sizeof(int8_t) * n); + arr1[0] = 2, arr1[1] = 1, arr1[2] = 0; + printf("Original: \n"); + printArray(arr1, n); + dnfSort(arr1, n); + printf("Sorted: \n"); + printArray(arr1, n); + free(arr1); + + n = 0; printf("Enter size of array:\n"); scanf("%d", &n); // E.g. 8 printf("Enter the elements in {0, 1, 2} of the array\n"); int i; - int8_t *arr = (int8_t *)malloc(sizeof(int8_t) * n); + int8_t *arr2 = (int8_t *)malloc(sizeof(int8_t) * n); for (i = 0; i < n; i++) { - scanf("%" SCNi8, &arr[i]); - if (arr[i] > 2 || arr[i] < 0) + scanf("%" SCNi8, &arr2[i]); + if (arr2[i] > 2 || arr2[i] < 0) { printf("Elements need to be in {0, 1, 2}"); return 1; } } - printf("Original array: \n"); - printArray(arr, n); - dnfSort(arr, n); - printf("Sorted array: \n"); - printArray(arr, n); + printf("Original: \n"); + printArray(arr2, n); + dnfSort(arr2, n); + printf("Sorted: \n"); + printArray(arr2, n); - free(arr); + free(arr2); return 0; } From a0e86e1a1569c8508663f6543f05e71125c80e91 Mon Sep 17 00:00:00 2001 From: BijanRiesenberg Date: Tue, 22 Feb 2022 14:25:06 +0100 Subject: [PATCH 03/10] test: added test for dnf_sort and applied test --- sorting/dnf_sort.c | 66 ++++++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 23 deletions(-) diff --git a/sorting/dnf_sort.c b/sorting/dnf_sort.c index 3b3f1525ca..1e3d55d3f8 100644 --- a/sorting/dnf_sort.c +++ b/sorting/dnf_sort.c @@ -32,7 +32,7 @@ void dnfSort(int8_t *arr, int size) break; case 2: temp = arr[mid]; - arr[mid++] = arr[high]; + arr[mid] = arr[high]; arr[high--] = temp; break; default: @@ -57,45 +57,65 @@ void printArray(int8_t *arr, int size) } /** - * @brief Input an array of {0, 1, 2} and use the - * Dutch National Flag 3-Way sort algorithm to sort - * - * @return int 0 if successful 1 if input is not in {0, 1, 2} + * @brief Self-test implementations + * @returns void */ -int main(void) +static void test() { - int n = 3; - int8_t *arr1 = (int8_t *)malloc(sizeof(int8_t) * n); - arr1[0] = 2, arr1[1] = 1, arr1[2] = 0; + int size = 6; + int8_t *arr1 = (int8_t *)malloc(sizeof(int8_t) * size); + arr1[0] = 1, arr1[1] = 1, arr1[2] = 1, arr1[3] = 2, arr1[4] = 1, + arr1[5] = 0; printf("Original: \n"); - printArray(arr1, n); - dnfSort(arr1, n); + printArray(arr1, size); + dnfSort(arr1, size); printf("Sorted: \n"); - printArray(arr1, n); + printArray(arr1, size); free(arr1); - n = 0; - printf("Enter size of array:\n"); - scanf("%d", &n); // E.g. 8 + size = 1; + int8_t *arr2 = (int8_t *)malloc(sizeof(int8_t) * size); + arr2[0] = 2; + printf("Original: \n"); + printArray(arr2, size); + dnfSort(arr2, size); + printf("Sorted: \n"); + printArray(arr2, size); + free(arr2); +} + +/** + * @brief Main Functiuon that reads an array of {0, 1, 2} and use the + * Dutch National Flag 3-Way sort algorithm to sort + * + * @return int 0 if successful 1 if input is not in {0, 1, 2} + */ +int main(void) +{ + test(); + int size = 0; + printf("Enter size of the array:\n"); + scanf("%d", &size); // E.g. 8 printf("Enter the elements in {0, 1, 2} of the array\n"); int i; - int8_t *arr2 = (int8_t *)malloc(sizeof(int8_t) * n); - for (i = 0; i < n; i++) + int8_t *arr = (int8_t *)malloc(sizeof(int8_t) * size); + for (i = 0; i < size; i++) { - scanf("%" SCNi8, &arr2[i]); - if (arr2[i] > 2 || arr2[i] < 0) + scanf("%" SCNi8, &arr[i]); + if (arr[i] > 2 || arr[i] < 0) { printf("Elements need to be in {0, 1, 2}"); + free(arr); return 1; } } printf("Original: \n"); - printArray(arr2, n); - dnfSort(arr2, n); + printArray(arr, size); + dnfSort(arr, size); printf("Sorted: \n"); - printArray(arr2, n); + printArray(arr, size); - free(arr2); + free(arr); return 0; } From 9f590819cebfaeca4ec473c462cf76751cda3fcd Mon Sep 17 00:00:00 2001 From: Bijan Riesenberg <25099093+Data-Hero@users.noreply.github.com> Date: Mon, 28 Feb 2022 18:49:19 +0100 Subject: [PATCH 04/10] Update dnf_sort.c --- sorting/dnf_sort.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/sorting/dnf_sort.c b/sorting/dnf_sort.c index 1e3d55d3f8..08eec6d465 100644 --- a/sorting/dnf_sort.c +++ b/sorting/dnf_sort.c @@ -8,6 +8,7 @@ #include #include #include +#include /** * @brief Dutch National Flag 3-Way inplace sort algorithm @@ -71,6 +72,12 @@ static void test() dnfSort(arr1, size); printf("Sorted: \n"); printArray(arr1, size); + + int8_t compare1[] = {0, 1, 1, 1, 1, 2}; + for (int c = 0; c < size; c++) + { + assert(arr1[c] == compare1[c]); + } free(arr1); size = 1; @@ -81,6 +88,12 @@ static void test() dnfSort(arr2, size); printf("Sorted: \n"); printArray(arr2, size); + + int8_t compare2[] = {2}; + for (int c = 0; c < size; c++) + { + assert(arr2[c] == compare2[c]); + } free(arr2); } From c23da7e961ecdf084bafb762794dbe934448b55e Mon Sep 17 00:00:00 2001 From: Bijan Riesenberg <25099093+Data-Hero@users.noreply.github.com> Date: Wed, 2 Mar 2022 08:14:34 +0100 Subject: [PATCH 05/10] test: assert in dnf instead of print --- sorting/dnf_sort.c | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/sorting/dnf_sort.c b/sorting/dnf_sort.c index 08eec6d465..804287225e 100644 --- a/sorting/dnf_sort.c +++ b/sorting/dnf_sort.c @@ -67,12 +67,7 @@ static void test() int8_t *arr1 = (int8_t *)malloc(sizeof(int8_t) * size); arr1[0] = 1, arr1[1] = 1, arr1[2] = 1, arr1[3] = 2, arr1[4] = 1, arr1[5] = 0; - printf("Original: \n"); - printArray(arr1, size); dnfSort(arr1, size); - printf("Sorted: \n"); - printArray(arr1, size); - int8_t compare1[] = {0, 1, 1, 1, 1, 2}; for (int c = 0; c < size; c++) { @@ -83,12 +78,7 @@ static void test() size = 1; int8_t *arr2 = (int8_t *)malloc(sizeof(int8_t) * size); arr2[0] = 2; - printf("Original: \n"); - printArray(arr2, size); dnfSort(arr2, size); - printf("Sorted: \n"); - printArray(arr2, size); - int8_t compare2[] = {2}; for (int c = 0; c < size; c++) { From 49559103f9e3e390abe2055417d36088515264d9 Mon Sep 17 00:00:00 2001 From: Bijan Riesenberg <25099093+Data-Hero@users.noreply.github.com> Date: Wed, 2 Mar 2022 15:55:53 +0100 Subject: [PATCH 06/10] fix: header descriptions --- sorting/dnf_sort.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sorting/dnf_sort.c b/sorting/dnf_sort.c index 804287225e..40f1455a35 100644 --- a/sorting/dnf_sort.c +++ b/sorting/dnf_sort.c @@ -3,12 +3,12 @@ * @brief Program to perform [dutch national * flag](https://en.wikipedia.org/wiki/Dutch_national_flag_problem) of a target * value in a given {0, 1, 2} containing array. - * @authors [Bijan Riesenberg](https://github.com/Data-Hero) Iterative + * @author [Bijan Riesenberg](https://github.com/Data-Hero) Iterative */ -#include -#include -#include -#include +#include /// for the 8 bit sized int8_t type +#include /// for IO operations +#include /// for memory allocation +#include /// for assert /** * @brief Dutch National Flag 3-Way inplace sort algorithm From f57931c5652c401961cd39a6832c6eb23bd6f20f Mon Sep 17 00:00:00 2001 From: BijanRiesenberg Date: Sun, 6 Mar 2022 18:53:45 +0100 Subject: [PATCH 07/10] fix: details in the comments --- sorting/dnf_sort.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/sorting/dnf_sort.c b/sorting/dnf_sort.c index 804287225e..0e4698298e 100644 --- a/sorting/dnf_sort.c +++ b/sorting/dnf_sort.c @@ -3,12 +3,17 @@ * @brief Program to perform [dutch national * flag](https://en.wikipedia.org/wiki/Dutch_national_flag_problem) of a target * value in a given {0, 1, 2} containing array. + * @details The dutch national flag problem is a computer science programming + * problem proposed by Edsger Dijkstra. + * Given a length n array of thee values, say red(0), white(1) and blue(2), the + * task is to order them by group. This is achieved by moving three pointers and + * swapping values depending on the value of the middle pointer. * @authors [Bijan Riesenberg](https://github.com/Data-Hero) Iterative */ +#include #include #include #include -#include /** * @brief Dutch National Flag 3-Way inplace sort algorithm From 1cc21d171c6af478798564beb24845759112592a Mon Sep 17 00:00:00 2001 From: Bijan Riesenberg <25099093+Data-Hero@users.noreply.github.com> Date: Sun, 6 Mar 2022 18:58:02 +0100 Subject: [PATCH 08/10] fix: run self test comment Co-authored-by: David Leal --- sorting/dnf_sort.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorting/dnf_sort.c b/sorting/dnf_sort.c index 7bd7c6e053..34b9b748ed 100644 --- a/sorting/dnf_sort.c +++ b/sorting/dnf_sort.c @@ -100,7 +100,7 @@ static void test() */ int main(void) { - test(); + test(); // run self-test implementations int size = 0; printf("Enter size of the array:\n"); scanf("%d", &size); // E.g. 8 From cf67df61ad2279ee6d11879b05c1d17b997f7e1b Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 20 Mar 2022 16:32:16 +0000 Subject: [PATCH 09/10] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index b5a7be6641..6892cc5420 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -396,6 +396,7 @@ * [Comb Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/comb_sort.c) * [Counting Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/counting_sort.c) * [Cycle Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/cycle_sort.c) + * [Dnf Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/dnf_sort.c) * [Gnome Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/gnome_sort.c) * [Heap Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/heap_sort.c) * [Heap Sort 2](https://github.com/TheAlgorithms/C/blob/master/sorting/heap_sort_2.c) From a0c2bfbbb1b9e8a379265eec7a9f056a12574992 Mon Sep 17 00:00:00 2001 From: David Leal Date: Tue, 27 Sep 2022 16:19:31 +0000 Subject: [PATCH 10/10] chore: add `\n` --- sorting/dnf_sort.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorting/dnf_sort.c b/sorting/dnf_sort.c index 34b9b748ed..7220f3abe7 100644 --- a/sorting/dnf_sort.c +++ b/sorting/dnf_sort.c @@ -113,7 +113,7 @@ int main(void) scanf("%" SCNi8, &arr[i]); if (arr[i] > 2 || arr[i] < 0) { - printf("Elements need to be in {0, 1, 2}"); + printf("Elements need to be in {0, 1, 2}\n"); free(arr); return 1; }