Skip to content

Commit f99c42f

Browse files
committed
added shell sort with tests and documentation
1 parent eb5fab1 commit f99c42f

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ Pratik Goyal <pratikgoyal2712@gmail.com>
1111
Jay Thorat <j.thorat10@gmail.com>
1212
Rajveer Singh Bharadwaj <rsb3256@gmail.com>
1313
Kishan Ved <kishanved123456@gmail.com>
14+
Arvinder Singh Dhoul <asdhoul004@gmail.com>

pydatastructs/linear_data_structures/algorithms.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1850,3 +1850,78 @@ def partition(array, lower, upper):
18501850
intro_sort(array, start=p+1, end=upper, maxdepth=maxdepth-1, ins_threshold=ins_threshold)
18511851

18521852
return array
1853+
1854+
def shell_sort(array, **kwargs):
1855+
"""
1856+
Implements shell sort algorithm.
1857+
1858+
Parameters
1859+
==========
1860+
1861+
array: Array
1862+
The array which is to be sorted.
1863+
start: int
1864+
The starting index of the portion
1865+
which is to be sorted.
1866+
Optional, by default 0
1867+
end: int
1868+
The ending index of the portion which
1869+
is to be sorted.
1870+
Optional, by default the index
1871+
of the last position filled.
1872+
comp: lambda/function
1873+
The comparator which is to be used
1874+
for sorting. If the function returns
1875+
False then only swapping is performed.
1876+
Optional, by default, less than or
1877+
equal to is used for comparing two
1878+
values.
1879+
backend: pydatastructs.Backend
1880+
The backend to be used.
1881+
Optional, by default, the best available
1882+
backend is used.
1883+
1884+
Returns
1885+
=======
1886+
1887+
output: Array
1888+
The sorted array.
1889+
1890+
Examples
1891+
========
1892+
1893+
>>> from pydatastructs.linear_data_structures.algorithms import OneDimensionalArray, shell_sort
1894+
>>> arr = OneDimensionalArray(int, [3, 2, 1])
1895+
>>> out = shell_sort(arr)
1896+
>>> str(out)
1897+
'[1, 2, 3]'
1898+
>>> out = shell_sort(arr, comp=lambda u, v: u > v)
1899+
>>> str(out)
1900+
'[3, 2, 1]'
1901+
1902+
References
1903+
==========
1904+
1905+
.. [1] https://en.wikipedia.org/wiki/Shellsort
1906+
"""
1907+
backend = kwargs.pop("backend", Backend.PYTHON)
1908+
if backend == Backend.CPP:
1909+
return _algorithms.shell_sort(array, **kwargs)
1910+
start = kwargs.get('start', 0)
1911+
end = kwargs.get('end', len(array) - 1)
1912+
comp = kwargs.get('comp', lambda u, v: u <= v)
1913+
n = end - start + 1
1914+
gap = n // 2
1915+
while gap > 0:
1916+
for i in range(start + gap, end + 1):
1917+
temp = array[i]
1918+
j = i
1919+
while j >= start + gap and not _comp(array[j - gap], temp, comp):
1920+
array[j] = array[j - gap]
1921+
j -= gap
1922+
array[j] = temp
1923+
gap //= 2
1924+
if _check_type(array, (DynamicArray, _arrays.DynamicOneDimensionalArray)):
1925+
array._modify(True)
1926+
1927+
return array

pydatastructs/linear_data_structures/tests/test_algorithms.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
prev_permutation, bubble_sort, linear_search, binary_search, jump_search,
88
selection_sort, insertion_sort, intro_sort, Backend)
99

10+
from pydatastructs.linear_data_structures.algorithms import shell_sort
1011
from pydatastructs.utils.raises_util import raises
1112
import random
1213

@@ -414,3 +415,24 @@ def test_binary_search():
414415
def test_jump_search():
415416
_test_common_search(jump_search)
416417
_test_common_search(jump_search, backend=Backend.CPP)
418+
419+
def test_shell_sort():
420+
assert shell_sort([]) == []
421+
422+
assert shell_sort([42]) == [42]
423+
424+
input_data = [1, 2, 3, 4, 5]
425+
expected = [1, 2, 3, 4, 5]
426+
assert shell_sort(input_data) == expected
427+
428+
input_data = [5, 4, 3, 2, 1]
429+
expected = [1, 2, 3, 4, 5]
430+
assert shell_sort(input_data) == expected
431+
432+
input_data = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3]
433+
expected = [1, 1, 2, 3, 3, 4, 5, 5, 6, 9]
434+
assert shell_sort(input_data) == expected
435+
436+
input_data = [-5, 3, -10, 7, 0, -2]
437+
expected = [-10, -5, -2, 0, 3, 7]
438+
assert shell_sort(input_data) == expected

0 commit comments

Comments
 (0)