From 0b761f0805a7413dd9e3c1f2c942c639aa6dab2a Mon Sep 17 00:00:00 2001 From: SleepingNerd <79285990+SleepingNerd@users.noreply.github.com> Date: Sat, 26 Feb 2022 18:31:06 +0100 Subject: [PATCH 1/3] StalinSort implemented in python Sorting algorithm where unsorted elements are eliminated. --- Python/StalinSort.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Python/StalinSort.py diff --git a/Python/StalinSort.py b/Python/StalinSort.py new file mode 100644 index 0000000..93c84f6 --- /dev/null +++ b/Python/StalinSort.py @@ -0,0 +1,28 @@ +def stalin_sort(lis): + i = 0 + + # While i is less than len of lis + while i < len(lis) - 1: + + # If element i is bigger than next element + # Remove element i + # and decrease i by one if i is not zero + if lis[i] > lis[i + 1]: + + del lis[i] + if i != 0: + i -= 1 + + # Else + # Add to i by one + else: + i += 1 + + + return lis + +# Driver code +if __name__ == '__main__': + lis = [20, 0, 1, 188, 20, 100, 2, 10] + print(f"Uncorrected list: {lis}") + print(f"Corrected list: {stalin_sort(lis)}") From a9bcf61777e16eeee208c58321a7dbee40b3c020 Mon Sep 17 00:00:00 2001 From: SleepingNerd <79285990+SleepingNerd@users.noreply.github.com> Date: Sat, 26 Feb 2022 18:49:58 +0100 Subject: [PATCH 2/3] Cleaned up version of StalinSort.py Fixed: Line 22:5: E303 too many blank lines (2) Line 25:1: E305 expected 2 blank lines after class or function definition, found 1 --- Python/StalinSort.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Python/StalinSort.py b/Python/StalinSort.py index 93c84f6..5f5dd2d 100644 --- a/Python/StalinSort.py +++ b/Python/StalinSort.py @@ -18,11 +18,11 @@ def stalin_sort(lis): else: i += 1 - return lis + # Driver code if __name__ == '__main__': - lis = [20, 0, 1, 188, 20, 100, 2, 10] - print(f"Uncorrected list: {lis}") - print(f"Corrected list: {stalin_sort(lis)}") + example_lis = [20, 0, 1, 188, 20, 100, 2, 10] + print(f"Uncorrected list: {example_lis}") + print(f"Corrected list: {stalin_sort(example_lis)}") From e71595b18ed7014ef1e760b5b538138fd7a66683 Mon Sep 17 00:00:00 2001 From: SleepingNerd <79285990+SleepingNerd@users.noreply.github.com> Date: Sat, 26 Feb 2022 19:17:20 +0100 Subject: [PATCH 3/3] Final version of StalinSort.py * Added a docstring --- Python/StalinSort.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Python/StalinSort.py b/Python/StalinSort.py index 5f5dd2d..b136f8c 100644 --- a/Python/StalinSort.py +++ b/Python/StalinSort.py @@ -1,3 +1,7 @@ +""" Python 3's implementation of StalinSort (O(N)) +""" + + def stalin_sort(lis): i = 0