From 38a7cea7786e2e0b9a0ccf7e9b6e7ce7d3135b98 Mon Sep 17 00:00:00 2001 From: Vedant Borkar Date: Mon, 11 Apr 2022 19:00:28 +0530 Subject: [PATCH 1/2] docs: add radix sort (#736) --- docs/en/Sorting/Radix-Sort.md | 75 +++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 docs/en/Sorting/Radix-Sort.md diff --git a/docs/en/Sorting/Radix-Sort.md b/docs/en/Sorting/Radix-Sort.md new file mode 100644 index 00000000..2626d40c --- /dev/null +++ b/docs/en/Sorting/Radix-Sort.md @@ -0,0 +1,75 @@ +# Radix Sort + +Radix sort is a sorting algorithm that sorts the elements by first grouping the individual digits of the same place value. Then, sort the elements according to their increasing/decreasing order. + +Radix Sort's time complexity is O(n * x), where n is the size of the array and 'x' is the number of digits in the largest number. + +## Steps + +1. Finds the largest element in the array and calculates the number of digits in it. Number of digits in the largest element are calculated as it is required to go through all the significant places of all elements. + +2. Goes through each significant place one by one. + +3. Uses Counting Sort to sort the digits at each significant place. + +4. Repeats "Step-3" until it sorts the elements based on the digits at last place. + +## Example + +Given array is +``` +[ 121, 432, 564, 23, 1, 45, 788 ] +``` + +Sorted array is +``` +[ 1, 23, 45, 121, 432, 564, 788 ] +``` + + +**FIRST PASS** + +- Sorts the elements based on the unit place digits. + +![image](https://user-images.githubusercontent.com/93431609/161891635-8edf89ec-2ca7-43b5-95ca-23b75f5846bb.png) + +After first pass array looks like.. +``` +[ 121, 1, 432, 23, 564, 45, 788 ] +``` + +**SECOND PASS** + +- Sorts the elements based on digits at tens place. + +![image](https://user-images.githubusercontent.com/93431609/161892564-64b20349-4064-4125-836d-adbdfb517bbd.png) + +After second pass array looks like.. +``` +[ 1, 121, 23, 432, 45, 564, 788 ] +``` + +**THIRD PASS** + +- Finally, sorts the elements based on the digits at hundreds place. + +![image](https://user-images.githubusercontent.com/93431609/161893373-3915ae95-28e9-4b02-bc35-5921b5280f84.png) + +After third pass array looks like.. +``` +[ 1, 23, 45, 121, 432, 564, 788 ] +``` + +## Implementation + +- [C](algorithms/C/sorting/radix-sort.c) +- [C++](algorithms/CPlusPlus/Sorting/radix-sort.cpp) +- [Python](algorithms/Python/sorting/radix_sort.py) + +## Video URL + +[Youtube Video about Radix Sort](https://www.youtube.com/watch?v=XiuSW_mEn7g) + +## Others + +[Wikipedia](https://en.wikipedia.org/wiki/Radix_sort) From dd601a7734764636b189a0626c9c8db942d6e080 Mon Sep 17 00:00:00 2001 From: sam chan <96338098+samchan2022@users.noreply.github.com> Date: Mon, 11 Apr 2022 21:38:45 +0800 Subject: [PATCH 2/2] chore(Python): add dutch national flag algo (#720) --- algorithms/Python/README.md | 1 + .../Python/arrays/dutch_national_flag_algo.py | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 algorithms/Python/arrays/dutch_national_flag_algo.py diff --git a/algorithms/Python/README.md b/algorithms/Python/README.md index 4e132715..3d4a1271 100644 --- a/algorithms/Python/README.md +++ b/algorithms/Python/README.md @@ -6,6 +6,7 @@ - [Rotate Array](arrays/rotate_array.py) - [Missing Number](arrays/missing_number.py) - [Remove duplicate items](arrays/remove_duplicates_list.py) +- [Dutch National Flag Algorithm](arrays/dutch_national_flag_algo.py) ## Linked Lists - [Doubly](linked_lists/doubly.py) diff --git a/algorithms/Python/arrays/dutch_national_flag_algo.py b/algorithms/Python/arrays/dutch_national_flag_algo.py new file mode 100644 index 00000000..4afcffe0 --- /dev/null +++ b/algorithms/Python/arrays/dutch_national_flag_algo.py @@ -0,0 +1,39 @@ +""" +Algorithm Type: Array sorting with 0s,1s and 2s in a single pass +Explain: To separate three different groups +Time Complexity: O(n) +Space Complexity: O(1) + + 0 0 1 1 1 ? ? ? ? 2 2 + | | | + v v v + Low Mid High + + To goal of the algo is to shrink this '?' region + wrapped by Mid and High + + > Low starts at 1 +""" + +numbers = [2, 0, 1, 1, 2, 0, 1, 2] + + +def DNFS(numbers: list) -> list: + length = len(numbers) + low = 0 + high = length - 1 + mid = 0 + while mid <= high: + if numbers[mid] == 0: + numbers[low], numbers[mid] = numbers[mid], numbers[low] + low = low + 1 + mid = mid + 1 + elif numbers[mid] == 1: + mid = mid + 1 + else: + numbers[mid], numbers[high] = numbers[high], numbers[mid] + high = high - 1 + return numbers + +if __name__ == "__main__": + print(DNFS(numbers))