Skip to content

Commit 0217d88

Browse files
authored
Merge pull request #9 from martmists/master
Add Bubble, Insertion and Selection sort
2 parents 9896eb0 + fd4c021 commit 0217d88

11 files changed

+179
-10
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,6 @@ venv.bak/
102102

103103
# mypy
104104
.mypy_cache/
105+
106+
# PyCharm
107+
.idea/

allalgorithms/sorting/__init__.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
from .merge_sort import *
1+
from .merge_sort import merge_sort
2+
from .insertion_sort import insertion_sort
3+
from .selection_sort import selection_sort
4+
from .bubble_sort import bubble_sort

allalgorithms/sorting/bubble_sort.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# -*- coding: UTF-8 -*-
2+
#
3+
# Bubble Sort Algorithm
4+
# The All ▲lgorithms library for python
5+
#
6+
# Contributed by: Martmists
7+
# Github: @martmists
8+
#
9+
10+
11+
def bubble_sort(seq):
12+
for i in range(len(seq)):
13+
for j in range(len(seq)-i-1):
14+
if seq[j] > seq[j+1]:
15+
# Swap if both are not in order
16+
seq[j], seq[j+1] = seq[j+1], seq[j]
17+
18+
return seq
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: UTF-8 -*-
2+
#
3+
# Insertion Sort Algorithm
4+
# The All ▲lgorithms library for python
5+
#
6+
# Contributed by: Martmists
7+
# Github: @martmists
8+
#
9+
10+
11+
def insertion_sort(arr):
12+
if len(arr) == 1:
13+
return arr
14+
15+
for i in range(1, len(arr)):
16+
j = i - 1
17+
while j >= 0 and arr[j] > arr[i]:
18+
j -= 1
19+
arr.insert(j + 1, arr.pop(i))
20+
21+
return arr

allalgorithms/sorting/merge_sort.py

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
# Contributed by: Carlos Abraham Hernandez
77
# Github: @abranhe
88
#
9+
10+
911
def merge_sort(arr):
1012
if len(arr) == 1:
1113
return arr
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- coding: UTF-8 -*-
2+
#
3+
# Bubble Sort Algorithm
4+
# The All ▲lgorithms library for python
5+
#
6+
# Contributed by: Martmists
7+
# Github: @martmists
8+
#
9+
10+
11+
def selection_sort(seq):
12+
for i in range(len(seq)):
13+
min_idx = i
14+
for j in range(min_idx, len(seq)):
15+
if seq[j] < seq[min_idx]:
16+
min_idx = j
17+
18+
seq[i], seq[min_idx] = seq[min_idx], seq[i]
19+
return seq

docs/sorting/bubble-sort.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Bubble Sort
2+
3+
Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm, which is a comparison sort, is named for the way smaller or larger elements "bubble" to the top of the list. Although the algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort. Bubble sort can be practical if the input is in mostly sorted order with some out-of-order elements nearly in position.
4+
5+
## Install
6+
7+
```
8+
pip install allalgorithms
9+
```
10+
11+
## Usage
12+
13+
```py
14+
from allalgorithms.sorting import bubble_sort
15+
16+
arr = [77, 2, 10, -2, 1, 7]
17+
18+
print(bubble_sort(arr))
19+
# -> [-2, 1, 2, 7, 10, 77]
20+
```
21+
22+
## API
23+
24+
### bubble_sort(array)
25+
26+
> Returns a sorted array
27+
28+
##### Params:
29+
30+
- `array`: Unsorted Array

docs/sorting/insertion-sort.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Insertion Sort
2+
3+
Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort.
4+
5+
## Install
6+
7+
```
8+
pip install allalgorithms
9+
```
10+
11+
## Usage
12+
13+
```py
14+
from allalgorithms.sorting import insertion_sort
15+
16+
arr = [77, 2, 10, -2, 1, 7]
17+
18+
print(insertion_sort(arr))
19+
# -> [-2, 1, 2, 7, 10, 77]
20+
```
21+
22+
## API
23+
24+
### insertion_sort(array)
25+
26+
> Returns a sorted array
27+
28+
##### Params:
29+
30+
- `array`: Unsorted Array

docs/sorting/selection-sort.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Selection Sort
2+
3+
In computer science, selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n^2) time complexity, making it inefficient on large lists, and generally performs worse than the similar insertion sort. Selection sort is noted for its simplicity, and it has performance advantages over more complicated algorithms in certain situations, particularly where auxiliary memory is limited.
4+
5+
## Install
6+
7+
```
8+
pip install allalgorithms
9+
```
10+
11+
## Usage
12+
13+
```py
14+
from allalgorithms.sorting import selection_sort
15+
16+
arr = [77, 2, 10, -2, 1, 7]
17+
18+
print(selection_sort(arr))
19+
# -> [-2, 1, 2, 7, 10, 77]
20+
```
21+
22+
## API
23+
24+
### selection_sort(array)
25+
26+
> Returns a sorted array
27+
28+
##### Params:
29+
30+
- `array`: Unsorted Array

tests/test_searches.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
from allalgorithms.searches import (
2-
binary_search
3-
)
4-
51
import unittest
62

3+
from allalgorithms.searches import binary_search
4+
5+
76
class TestSearches(unittest.TestCase):
87

98
def test_binary_search(self):
@@ -13,5 +12,6 @@ def test_binary_search(self):
1312
self.assertEqual(None, binary_search(arr, 8))
1413
self.assertEqual(None, binary_search(arr, -1))
1514

15+
1616
if __name__ == '__main__':
1717
unittest.main()

tests/test_sorting.py

+18-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
1+
import unittest
2+
13
from allalgorithms.sorting import (
2-
merge_sort
4+
bubble_sort,
5+
insertion_sort,
6+
merge_sort,
7+
selection_sort
38
)
4-
import unittest
59

610
class TestSorting(unittest.TestCase):
11+
def test_merge_sort(self):
12+
self.assertEqual([-44, 1, 2, 3, 7, 19], merge_sort([7, 3, 2, 19, -44, 1]))
13+
14+
def test_bubble_sort(self):
15+
self.assertEqual([-44, 1, 2, 3, 7, 19], bubble_sort([7, 3, 2, 19, -44, 1]))
16+
17+
def test_insertion_sort(self):
18+
self.assertEqual([-44, 1, 2, 3, 7, 19], insertion_sort([7, 3, 2, 19, -44, 1]))
19+
20+
def test_selection_sort(self):
21+
self.assertEqual([-44, 1, 2, 3, 7, 19], selection_sort([7, 3, 2, 19, -44, 1]))
722

8-
def test_merge_sort(self):
9-
self.assertEqual([-44, 1, 2, 3, 7, 19], merge_sort([7, 3, 2, 19, -44, 1]))
1023

1124
if __name__ == "__main__":
12-
unittest.main()
25+
unittest.main()

0 commit comments

Comments
 (0)