-
-
Notifications
You must be signed in to change notification settings - Fork 49.4k
Add Dutch National Flag Algorithm for Sorting Arrays of 0s, 1s, and 2s #13094
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
arvinder004
wants to merge
5
commits into
TheAlgorithms:master
Choose a base branch
from
arvinder004:dutch_national_flag
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
5 commits
Select commit
Hold shift + click to select a range
a7db73d
added dutch national flag algorithm
arvinder004 2fc6917
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] eaa5121
reviewed
arvinder004 57abc1a
Merge branch 'dutch_national_flag' of https://github.com/arvinder004/…
arvinder004 3df8737
added doctest
arvinder004 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,39 @@ | ||
| def dutch_national_flag(arr: list[int]) -> list[int]: | ||
| """ | ||
| Sorts an array containing only 0s, 1s and 2s | ||
|
|
||
| Args: | ||
| arr(list[int]): The input array (containing only 0s, 1s and 2s) | ||
|
|
||
| Returns: | ||
| list[int]: Sorted array | ||
|
|
||
| Examples: | ||
| >>> dutch_national_flag([2, 0, 2, 1, 2, 0, 1]) | ||
| [0, 0, 1, 1, 2, 2, 2] | ||
| >>> dutch_national_flag([0, 1, 2, 0, 1, 2]) | ||
| [0, 0, 1, 1, 2, 2] | ||
| >>> dutch_national_flag([1, 1, 1]) | ||
| [1, 1, 1] | ||
| >>> dutch_national_flag([]) | ||
| [] | ||
| """ | ||
|
|
||
| low, mid, high = 0, 0, len(arr) - 1 | ||
|
|
||
| while mid <= high: | ||
| if arr[mid] == 0: | ||
| arr[low], arr[mid] = arr[mid], arr[low] | ||
| low += 1 | ||
| mid += 1 | ||
| elif arr[mid] == 1: | ||
| mid += 1 | ||
| else: | ||
| arr[mid], arr[high] = arr[high], arr[mid] | ||
| high -= 1 | ||
| return arr | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| arr = [2, 0, 2, 1, 2, 0, 1] | ||
| print("Sorted array: ", dutch_national_flag(arr)) | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file
data_structures/arrays/dutch_national_flag.py, please provide doctest for the functiondutch_national_flag