From 25b9da4c978f6e221e37582b26e33c493407d7ea Mon Sep 17 00:00:00 2001 From: yennanliu Date: Fri, 23 Feb 2024 18:24:49 +0800 Subject: [PATCH] update readme, add code comment, rename script --- README.md | 4 ++-- ...ort_bottomup.py => merge_sort_bottomup.py} | 18 ++++++++++++++--- algorithm/python/merge_sort_topdown.py | 18 ++++++++++++++--- algorithm/python/quick_sort.py | 20 ++++++++++++++++++- algorithm/python/quick_sort_v2.py | 17 ++++++++++++++++ 5 files changed, 68 insertions(+), 9 deletions(-) rename algorithm/python/{mergesort_bottomup.py => merge_sort_bottomup.py} (86%) diff --git a/README.md b/README.md index 6ec892c63..42dd42192 100644 --- a/README.md +++ b/README.md @@ -185,9 +185,9 @@ ||Bubble sort| [Python](./algorithm/python/bubble_sort.py), [JS](./algorithm/js/bubble_sort.js) , [C](./algorithm/c/bubble_sort.c)| | | | | OK* (3)| ||Insertion sort| [Python](./algorithm/python/insertion_sort.py), [JS](./algorithm/js/insertion_sort.js) |`stable` sort| work very fast for `nearly sorted` array| | | AGAIN| ||Bucket sort| [Python](./algorithm/python/bucket_sort.py) | | | | | AGAIN| -||Quick sort| [quick_sort.py](./algorithm/python/quick_sort.py), [quick_sort_v2.py](./algorithm/python/quick_sort_v2.py), [QuickSort.java](https://github.com/yennanliu/CS_basics/blob/master/leetcode_java/src/main/java/AlgorithmJava/QuickSort.java)| NOTE !!!, avg is NLogN, worst is N**2, [complexity ref](https://www.geeksforgeeks.org/time-and-space-complexity-analysis-of-quick-sort/) | `Best : O(N Log N), Avg : O(N Log N), Worst : O(N^2)` | | AGAIN***| +||Quick sort| [quick_sort.py](./algorithm/python/quick_sort.py), [quick_sort_v2.py](./algorithm/python/quick_sort_v2.py), [QuickSort.java](https://github.com/yennanliu/CS_basics/blob/master/leetcode_java/src/main/java/AlgorithmJava/QuickSort.java)| NOTE !!!, `avg is NLogN, worst is N**2`, [big O ref](https://www.geeksforgeeks.org/time-and-space-complexity-analysis-of-quick-sort/), [big O ref 2](https://rust-algo.club/sorting/quicksort/) | `Best : O(N Log N), Avg : O(N Log N), Worst : O(N^2)` | | AGAIN***| ||Heap sort| [Python](./algorithm/python/heap_sort.py)| | | AGAIN**| -||Merge sort|[merge_sort_topdown.py](./algorithm/python/merge_sort_topdown.py), [mergesort_bottomup.py](./algorithm/python/mergesort_bottomup.py), [MergeSortTopDown.java](https://github.com/yennanliu/CS_basics/blob/master/leetcode_java/src/main/java/AlgorithmJava/MergeSortTopDown.java), [SQL](./algorithm/sql/Mergesort.sql) | | | | | OK* (2)| +||Merge sort|[merge_sort_topdown.py](./algorithm/python/merge_sort_topdown.py), [mergesort_bottomup.py](./algorithm/python/merge_sort_bottomup.py), [MergeSortTopDown.java](https://github.com/yennanliu/CS_basics/blob/master/leetcode_java/src/main/java/AlgorithmJava/MergeSortTopDown.java), [SQL](./algorithm/sql/Mergesort.sql) | | | `Best : O(N Log N), Avg : O(N Log N), Worst : O(N Log N)` | O(N)| OK* (2)| ||Pancake sort| [Python](./algorithm/python/pancake_sort.py) | | | | | AGAIN| ||Selection sort| [Python](./algorithm/python/selection_sort.py), [JS](./algorithm/js/selection_sort.js) | | | | | AGAIN| ||Topological sort| [Python](./algorithm/python/topological_sort.py) | | Topological Sort is a algorithm can find "ordering" on an "order dependency" graph | | | AGAIN| diff --git a/algorithm/python/mergesort_bottomup.py b/algorithm/python/merge_sort_bottomup.py similarity index 86% rename from algorithm/python/mergesort_bottomup.py rename to algorithm/python/merge_sort_bottomup.py index 4c9daa99d..e4d8385e3 100644 --- a/algorithm/python/mergesort_bottomup.py +++ b/algorithm/python/merge_sort_bottomup.py @@ -1,12 +1,24 @@ #--------------------------------------------------------------- -# MERGE SORT bottomup +# MERGE SORT - bottomup #--------------------------------------------------------------- # https://leetcode.com/explore/learn/card/recursion-ii/470/divide-and-conquer/2868/ # https://leetcode.com/problems/sort-an-array/discuss/568255/Python-Merge-Sort +# https://rust-algo.club/sorting/mergesort/ -# time : O(N log N) -# space : O(N) +""" + +Time complexity + +Best : O(N Log N) +Avg : O(N Log N) +Worst : O(N Log N)` + + +Space complexity + +O(N) +""" class Solution: def sortArray(self, nums: List[int]) -> List[int]: diff --git a/algorithm/python/merge_sort_topdown.py b/algorithm/python/merge_sort_topdown.py index f732247db..ef249234f 100644 --- a/algorithm/python/merge_sort_topdown.py +++ b/algorithm/python/merge_sort_topdown.py @@ -1,11 +1,23 @@ #--------------------------------------------------------------- -# MERGE SORT topdown +# MERGE SORT - topdown #--------------------------------------------------------------- # https://leetcode.com/explore/learn/card/recursion-ii/470/divide-and-conquer/2868/ +# https://rust-algo.club/sorting/mergesort/ -# time : O(N) -# space : O(N) +""" + +Time complexity + +Best : O(N Log N) +Avg : O(N Log N) +Worst : O(N Log N)` + + +Space complexity + +O(N) +""" def merge_sort(nums): # bottom cases: empty or list of a single element. diff --git a/algorithm/python/quick_sort.py b/algorithm/python/quick_sort.py index 57eb38d93..d7972e720 100644 --- a/algorithm/python/quick_sort.py +++ b/algorithm/python/quick_sort.py @@ -2,6 +2,24 @@ # QUICK SORT V1 #--------------------------------------------------------------- +""" + +Time complexity + +Best O(n log n) +Average O(n log n) +Worst O(n^2) + + + +Space complexity + +Worst space $O(log n) $ or $O(n) $ auxiliary + +""" + +# https://rust-algo.club/sorting/quicksort/index.html + # V0 # https://www.bilibili.com/video/BV1at411T75o/ # https://hackmd.io/@Aquamay/H1nxBOLcO/https%3A%2F%2Fhackmd.io%2F%40Aquamay%2FB1SPnfRq_ @@ -202,4 +220,4 @@ def three_way_radix_quicksort(sorting: list) -> list: three_way_radix_quicksort([i for i in sorting if i < sorting[0]]) + [i for i in sorting if i == sorting[0]] + three_way_radix_quicksort([i for i in sorting if i > sorting[0]]) - ) + ) \ No newline at end of file diff --git a/algorithm/python/quick_sort_v2.py b/algorithm/python/quick_sort_v2.py index 247aa955c..0a6cad3c5 100644 --- a/algorithm/python/quick_sort_v2.py +++ b/algorithm/python/quick_sort_v2.py @@ -3,6 +3,23 @@ #--------------------------------------------------------------- # https://leetcode.com/explore/learn/card/recursion-ii/470/divide-and-conquer/2870/ +# https://rust-algo.club/sorting/quicksort/index.html + +""" + +Time complexity + +Best O(n log n) +Average O(n log n) +Worst O(n^2) + + + +Space complexity + +Worst space $O(log n) $ or $O(n) $ auxiliary + +""" def quicksort(lst): """