Skip to content

Commit 6a2caae

Browse files
Added selection_sort and insertion_sort APIs (#462)
Co-authored-by: Gagandeep Singh <gdp.1807@gmail.com>
1 parent c0d2786 commit 6a2caae

File tree

5 files changed

+164
-5
lines changed

5 files changed

+164
-5
lines changed

docs/source/pydatastructs/linear_data_structures/algorithms.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ Algorithms
2121

2222
.. autofunction:: pydatastructs.bubble_sort
2323

24+
.. autofunction:: pydatastructs.selection_sort
25+
26+
.. autofunction:: pydatastructs.insertion_sort
27+
2428
.. autofunction:: pydatastructs.longest_common_subsequence
2529

2630
.. autofunction:: pydatastructs.is_ordered
@@ -39,4 +43,4 @@ Algorithms
3943

4044
.. autofunction:: pydatastructs.binary_search
4145

42-
.. autofunction:: pydatastructs.jump_search
46+
.. autofunction:: pydatastructs.jump_search

pydatastructs/linear_data_structures/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
bubble_sort,
4343
linear_search,
4444
binary_search,
45-
jump_search
45+
jump_search,
46+
selection_sort,
47+
insertion_sort
4648
)
4749
__all__.extend(algorithms.__all__)

pydatastructs/linear_data_structures/algorithms.py

Lines changed: 147 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
'bubble_sort',
2727
'linear_search',
2828
'binary_search',
29-
'jump_search'
29+
'jump_search',
30+
'selection_sort',
31+
'insertion_sort'
3032
]
3133

3234
def _merge(array, sl, el, sr, er, end, comp):
@@ -1373,6 +1375,150 @@ def bubble_sort(array, **kwargs):
13731375

13741376
return array
13751377

1378+
def selection_sort(array, **kwargs):
1379+
"""
1380+
Implements selection sort algorithm.
1381+
1382+
Parameters
1383+
==========
1384+
1385+
array: Array
1386+
The array which is to be sorted.
1387+
start: int
1388+
The starting index of the portion
1389+
which is to be sorted.
1390+
Optional, by default 0
1391+
end: int
1392+
The ending index of the portion which
1393+
is to be sorted.
1394+
Optional, by default the index
1395+
of the last position filled.
1396+
comp: lambda/function
1397+
The comparator which is to be used
1398+
for sorting. If the function returns
1399+
False then only swapping is performed.
1400+
Optional, by default, less than or
1401+
equal to is used for comparing two
1402+
values.
1403+
backend: pydatastructs.Backend
1404+
The backend to be used.
1405+
Optional, by default, the best available
1406+
backend is used.
1407+
1408+
Returns
1409+
=======
1410+
1411+
output: Array
1412+
The sorted array.
1413+
1414+
Examples
1415+
========
1416+
1417+
>>> from pydatastructs import OneDimensionalArray, selection_sort
1418+
>>> arr = OneDimensionalArray(int,[3, 2, 1])
1419+
>>> out = selection_sort(arr)
1420+
>>> str(out)
1421+
'[1, 2, 3]'
1422+
>>> out = selection_sort(arr, comp=lambda u, v: u > v)
1423+
>>> str(out)
1424+
'[3, 2, 1]'
1425+
1426+
References
1427+
==========
1428+
1429+
.. [1] https://en.wikipedia.org/wiki/Selection_sort
1430+
"""
1431+
raise_if_backend_is_not_python(
1432+
selection_sort, kwargs.get('backend', Backend.PYTHON))
1433+
start = kwargs.get('start', 0)
1434+
end = kwargs.get('end', len(array) - 1)
1435+
comp = kwargs.get('comp', lambda u, v: u <= v)
1436+
1437+
for i in range(start, end + 1):
1438+
jMin = i
1439+
for j in range(i + 1, end + 1):
1440+
if not _comp(array[jMin], array[j], comp):
1441+
jMin = j
1442+
if jMin != i:
1443+
array[i], array[jMin] = array[jMin], array[i]
1444+
1445+
if _check_type(array, DynamicArray):
1446+
array._modify(force=True)
1447+
1448+
return array
1449+
1450+
def insertion_sort(array, **kwargs):
1451+
"""
1452+
Implements insertion sort algorithm.
1453+
1454+
Parameters
1455+
==========
1456+
1457+
array: Array
1458+
The array which is to be sorted.
1459+
start: int
1460+
The starting index of the portion
1461+
which is to be sorted.
1462+
Optional, by default 0
1463+
end: int
1464+
The ending index of the portion which
1465+
is to be sorted.
1466+
Optional, by default the index
1467+
of the last position filled.
1468+
comp: lambda/function
1469+
The comparator which is to be used
1470+
for sorting. If the function returns
1471+
False then only swapping is performed.
1472+
Optional, by default, less than or
1473+
equal to is used for comparing two
1474+
values.
1475+
backend: pydatastructs.Backend
1476+
The backend to be used.
1477+
Optional, by default, the best available
1478+
backend is used.
1479+
1480+
Returns
1481+
=======
1482+
1483+
output: Array
1484+
The sorted array.
1485+
1486+
Examples
1487+
========
1488+
1489+
>>> from pydatastructs import OneDimensionalArray, insertion_sort
1490+
>>> arr = OneDimensionalArray(int,[3, 2, 1])
1491+
>>> out = insertion_sort(arr)
1492+
>>> str(out)
1493+
'[1, 2, 3]'
1494+
>>> out = insertion_sort(arr, comp=lambda u, v: u > v)
1495+
>>> str(out)
1496+
'[3, 2, 1]'
1497+
1498+
References
1499+
==========
1500+
1501+
.. [1] https://en.wikipedia.org/wiki/Insertion_sort
1502+
"""
1503+
raise_if_backend_is_not_python(
1504+
insertion_sort, kwargs.get('backend', Backend.PYTHON))
1505+
start = kwargs.get('start', 0)
1506+
end = kwargs.get('end', len(array) - 1)
1507+
comp = kwargs.get('comp', lambda u, v: u <= v)
1508+
1509+
for i in range(start + 1, end + 1):
1510+
temp = array[i]
1511+
j = i
1512+
while j > start and not _comp(array[j - 1], temp, comp):
1513+
array[j] = array[j - 1]
1514+
j -= 1
1515+
array[j] = temp
1516+
1517+
if _check_type(array, DynamicArray):
1518+
array._modify(force=True)
1519+
1520+
return array
1521+
13761522
def linear_search(array, value, **kwargs):
13771523
"""
13781524
Implements linear search algorithm.

pydatastructs/linear_data_structures/tests/test_algorithms.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
heapsort, matrix_multiply_parallel, counting_sort, bucket_sort,
55
cocktail_shaker_sort, quick_sort, longest_common_subsequence, is_ordered,
66
upper_bound, lower_bound, longest_increasing_subsequence, next_permutation,
7-
prev_permutation, bubble_sort, linear_search, binary_search, jump_search)
7+
prev_permutation, bubble_sort, linear_search, binary_search, jump_search, selection_sort, insertion_sort)
88

99
from pydatastructs.utils.raises_util import raises
1010
import random
@@ -83,6 +83,12 @@ def test_quick_sort():
8383
def test_bubble_sort():
8484
_test_common_sort(bubble_sort)
8585

86+
def test_selection_sort():
87+
_test_common_sort(selection_sort)
88+
89+
def test_insertion_sort():
90+
_test_common_sort(insertion_sort)
91+
8692
def test_matrix_multiply_parallel():
8793
ODA = OneDimensionalArray
8894

pydatastructs/utils/tests/test_code_quality.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ def _apis():
103103
pyds.AdjacencyMatrixGraphNode, pyds.GraphEdge, pyds.Set, pyds.BinaryIndexedTree,
104104
pyds.CartesianTree, pyds.CartesianTreeNode, pyds.Treap, pyds.RedBlackTreeNode, pyds.RedBlackTree,
105105
pyds.Trie, pyds.TrieNode, pyds.SkipList, pyds.RangeQueryStatic, pyds.SparseTable,
106-
pyds.bubble_sort, pyds.linear_search, pyds.binary_search, pyds.jump_search]
106+
pyds.bubble_sort, pyds.linear_search, pyds.binary_search, pyds.jump_search,
107+
pyds.selection_sort, pyds.insertion_sort]
107108

108109
def test_public_api():
109110
pyds = pydatastructs

0 commit comments

Comments
 (0)