From 5b028ab3c32774c88e078f1f4d954063e94c09ac Mon Sep 17 00:00:00 2001 From: Ayush Parikh Date: Tue, 1 Jun 2021 20:36:31 +0530 Subject: [PATCH] chore(Python): shell sort algorithm (#331) --- algorithms/Python/README.md | 1 + algorithms/Python/sorting/shell-sort.py | 33 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 algorithms/Python/sorting/shell-sort.py diff --git a/algorithms/Python/README.md b/algorithms/Python/README.md index d1caa623..ec9510de 100644 --- a/algorithms/Python/README.md +++ b/algorithms/Python/README.md @@ -30,6 +30,7 @@ 5. [Selection Sort](sorting/selection_sort.py) 6. [Heap Sort](sorting/heap-sort.py) 7. [Radix Sort](sorting/radix-sort.py) +8. [Shell Sort](sorting/shell-sort.py) ## Strings 1. [Is Good Str](strings/is_good_str.py) diff --git a/algorithms/Python/sorting/shell-sort.py b/algorithms/Python/sorting/shell-sort.py new file mode 100644 index 00000000..5f2e8107 --- /dev/null +++ b/algorithms/Python/sorting/shell-sort.py @@ -0,0 +1,33 @@ +def shellSort(input_list): + + gap = len(input_list) // 2 + while gap > 0: + + for i in range(gap, len(input_list)): + temp = input_list[i] + j = i +# Sort the sub list for this gap + + while j >= gap and input_list[j - gap] > temp: + input_list[j] = input_list[j - gap] + j = j-gap + input_list[j] = temp + +# Reduce the gap for the next element + + gap = gap//2 + +list = [19,2,31,45,30,11,121,27] + +shellSort(list) +print(list) + + + +#Time Complexity + +#Worst Case Complexity: less than or equal to O(n2) Worst case complexity for shell sort is always less than or equal to O(n2) . ... + +#Best Case Complexity: O(n*log n) ... + +#Average Case Complexity: O(n*log n)#