-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Added Dutch National Flag algorithm #4636 #4639
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ca789b4
Added Dutch national flag sort Algorithm
KiranHipparagi 1a129a7
Changed file name to dnf_sort.py
KiranHipparagi 3cd9ba1
Added descriptive name and type hint
KiranHipparagi 7ace144
Added test cases
KiranHipparagi c17470c
Added doctest cases
KiranHipparagi a812c38
Update sorts/dnf_sort.py
KiranHipparagi ea5b6bb
Added doctest for dutch_national_flag_sort sorts/dnf_sort.py
KiranHipparagi bc9cdc6
Update sorts/dnf_sort.py
KiranHipparagi c35debb
Added doctest for the function
KiranHipparagi 92bdfc5
update file as per black code formatter
KiranHipparagi c19022c
Update dnf_sort.py
KiranHipparagi e08603e
Update and rename dnf_sort.py to dutch_national_flag_sort.py
cclauss 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
""" | ||
Pure implementation of Dutch national flag problem(dnf) sort algorithm in Python | ||
Dutch National Flag algorithm is a popular algorithm originally designed by Edsger Dijkstra. | ||
It is the most optimal sort for 3 unique values (eg. 0,1,2) in an Array. | ||
dnf sort can sort an array of n size with [0<=a[i]<=2] at guaranteed O(n) complexity in a single pass. | ||
|
||
More info on: https://en.wikipedia.org/wiki/Dutch_national_flag_problem | ||
|
||
For doctests run following command: | ||
python -m doctest -v dnf_sort.py | ||
or | ||
python3 -m doctest -v dnf_sort.py | ||
|
||
For manual testing run: | ||
python dnf_sort.py | ||
|
||
""" | ||
|
||
|
||
# Python program to sort an array with | ||
# 0, 1 and 2 in a single pass | ||
|
||
# Function to sort array | ||
def dnf_sort( a, arr_size): | ||
KiranHipparagi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Pure implementation of dnf sort algorithm in Python | ||
KiranHipparagi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
:param data: 3 unique values (eg. 0,1,2) in an Array | ||
KiranHipparagi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
:return: the same collection in ascending order | ||
Examples: | ||
Input: dnf_sort([2, 1, 0, 0,1, 2]) | ||
Output: [0, 0, 1, 1, 2, 2] | ||
Input: dnf_sort([0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1]) | ||
Output: [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2] | ||
""" | ||
low = 0 | ||
high = arr_size - 1 | ||
mid = 0 | ||
while mid <= high: | ||
if a[mid] == 0: | ||
a[low], a[mid] = a[mid], a[low] | ||
low = low + 1 | ||
mid = mid + 1 | ||
elif a[mid] == 1: | ||
mid = mid + 1 | ||
else: | ||
a[mid], a[high] = a[high], a[mid] | ||
high = high - 1 | ||
return a | ||
|
||
if __name__ == "__main__": | ||
from doctest import testmod | ||
|
||
testmod() | ||
|
||
|
||
user_input = input("Enter numbers separated by a comma:\n").strip() | ||
unsorted = [int(item) for item in user_input.split(",")] | ||
print(dnf_sort(unsorted,len(unsorted))) |
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.