diff --git a/allalgorithms/sorting/shell_sort.py b/allalgorithms/sorting/shell_sort.py new file mode 100644 index 0000000..b415af8 --- /dev/null +++ b/allalgorithms/sorting/shell_sort.py @@ -0,0 +1,30 @@ +# -*- coding: UTF-8 -*- +# +# Shell Sort Algorithm +# The All ▲lgorithms library for python +# +# Contributed by: Elias +# Github: @eliasbayona +# + +def shell_sort(arr): + n = len(arr) + h = int(n/2) + + + while h > 0: + for i in range(h,n): + + temp = arr[i] + j = i + + while j >= h and arr[j-h] >temp: + arr[j] = arr[j-h] + j -= h + + arr[j] = temp + + h = int(h/2) + + return arr + diff --git a/docs/sorting/shell-sort.md b/docs/sorting/shell-sort.md new file mode 100644 index 0000000..f984897 --- /dev/null +++ b/docs/sorting/shell-sort.md @@ -0,0 +1,29 @@ +# Selection Sort + +In computer science, shell sort improves upon insertion sort by moving out of order elements more than one position at a time. It has a best case O(n log n) time complexity, and for other cases, it depends on the gap sequence. According to Poonen Theorem, worst case complexity for shell sort is Θ(NlogN)^2/(log logN)^2) or Θ(NlogN)^2/log logN) or Θ(N(logN)^2) or something in between. +## Install + +``` +pip install allalgorithms +``` + +## Usage + +```py +from allalgorithms.sorting import shell_sort + +arr = [77, 2, 10, -2, 1, 7] + +print(shell_sort(arr)) +# -> [-2, 1, 2, 7, 10, 77] +``` + +## API + +### selection_sort(array) + +> Returns a sorted array + +##### Params: + +- `array`: Unsorted Array diff --git a/tests/test_sorting.py b/tests/test_sorting.py index 127f341..32c9435 100644 --- a/tests/test_sorting.py +++ b/tests/test_sorting.py @@ -8,7 +8,8 @@ pigeonhole_sort, stooge_sort, cocktail_shaker_sort, - tree_sort + tree_sort, + shell_sort, ) @@ -37,6 +38,8 @@ def test_cocktail_shaker_sort(self): def tree_sort(self): self.assertEqual([-44, 1, 2, 3, 7, 19], tree_sort([7, 3, 2, 19, -44, 1])) + def test_shell_sort(self): + self.assertEqual([-44, 1, 2, 3, 7, 19], shell_sort([7, 3, 2, 19, -44, 1])) if __name__ == "__main__": unittest.main()
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: