-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat: add dutch national flag 3 way sort #941
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Data-Hero
wants to merge
12
commits into
TheAlgorithms:master
Choose a base branch
from
Data-Hero:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
53d3fc1
feat: add dutch national flag sort
Data-Hero b8ff22e
test: add test for dnf_sort algorithm
Data-Hero a0e86e1
test: added test for dnf_sort and applied test
Data-Hero 9f59081
Update dnf_sort.c
Data-Hero c23da7e
test: assert in dnf instead of print
Data-Hero 4955910
fix: header descriptions
Data-Hero f57931c
fix: details in the comments
Data-Hero 91af654
fix: merged details with include comment
Data-Hero 1cc21d1
fix: run self test comment
Data-Hero 8e716cd
Merge branch 'TheAlgorithms:master' into master
Data-Hero cf67df6
updating DIRECTORY.md
a0c2bfb
chore: add `\n`
Panquesito7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/** | ||
* @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. | ||
* @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. | ||
* @author [Bijan Riesenberg](https://github.com/Data-Hero) Iterative | ||
*/ | ||
#include <assert.h> /// for assert | ||
#include <inttypes.h> /// for the 8 bit sized int8_t type | ||
#include <stdio.h> /// for IO operations | ||
#include <stdlib.h> /// for memory allocation | ||
|
||
/** | ||
* @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) | ||
{ | ||
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 | ||
* @returns void | ||
*/ | ||
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]); | ||
} | ||
} | ||
|
||
Data-Hero marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* @brief Self-test implementations | ||
* @returns void | ||
*/ | ||
static void test() | ||
{ | ||
int size = 6; | ||
int8_t *arr1 = (int8_t *)malloc(sizeof(int8_t) * size); | ||
Data-Hero marked this conversation as resolved.
Show resolved
Hide resolved
|
||
arr1[0] = 1, arr1[1] = 1, arr1[2] = 1, arr1[3] = 2, arr1[4] = 1, | ||
arr1[5] = 0; | ||
dnfSort(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; | ||
int8_t *arr2 = (int8_t *)malloc(sizeof(int8_t) * size); | ||
arr2[0] = 2; | ||
dnfSort(arr2, size); | ||
int8_t compare2[] = {2}; | ||
for (int c = 0; c < size; c++) | ||
{ | ||
assert(arr2[c] == compare2[c]); | ||
} | ||
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 | ||
* @returns int 0 if successful 1 if input is not in {0, 1, 2} | ||
*/ | ||
int main(void) | ||
{ | ||
test(); // run self-test implementations | ||
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 *arr = (int8_t *)malloc(sizeof(int8_t) * size); | ||
for (i = 0; i < size; i++) | ||
{ | ||
scanf("%" SCNi8, &arr[i]); | ||
if (arr[i] > 2 || arr[i] < 0) | ||
{ | ||
printf("Elements need to be in {0, 1, 2}\n"); | ||
free(arr); | ||
return 1; | ||
} | ||
} | ||
printf("Original: \n"); | ||
printArray(arr, size); | ||
dnfSort(arr, size); | ||
printf("Sorted: \n"); | ||
printArray(arr, size); | ||
|
||
free(arr); | ||
return 0; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.