diff --git a/.gitignore b/.gitignore index 894a44c..1fa2656 100644 --- a/.gitignore +++ b/.gitignore @@ -102,3 +102,6 @@ venv.bak/ # mypy .mypy_cache/ + +# PyCharm +.idea/ diff --git a/allalgorithms/sorting/__init__.py b/allalgorithms/sorting/__init__.py index 173bb67..d84d4f4 100644 --- a/allalgorithms/sorting/__init__.py +++ b/allalgorithms/sorting/__init__.py @@ -1 +1,4 @@ -from .merge_sort import * +from .merge_sort import merge_sort +from .insertion_sort import insertion_sort +from .selection_sort import selection_sort +from .bubble_sort import bubble_sort diff --git a/allalgorithms/sorting/bubble_sort.py b/allalgorithms/sorting/bubble_sort.py new file mode 100644 index 0000000..a22f20f --- /dev/null +++ b/allalgorithms/sorting/bubble_sort.py @@ -0,0 +1,18 @@ +# -*- coding: UTF-8 -*- +# +# Bubble Sort Algorithm +# The All ▲lgorithms library for python +# +# Contributed by: Martmists +# Github: @martmists +# + + +def bubble_sort(seq): + for i in range(len(seq)): + for j in range(len(seq)-i-1): + if seq[j] > seq[j+1]: + # Swap if both are not in order + seq[j], seq[j+1] = seq[j+1], seq[j] + + return seq diff --git a/allalgorithms/sorting/insertion_sort.py b/allalgorithms/sorting/insertion_sort.py new file mode 100644 index 0000000..4ab366d --- /dev/null +++ b/allalgorithms/sorting/insertion_sort.py @@ -0,0 +1,21 @@ +# -*- coding: UTF-8 -*- +# +# Insertion Sort Algorithm +# The All ▲lgorithms library for python +# +# Contributed by: Martmists +# Github: @martmists +# + + +def insertion_sort(arr): + if len(arr) == 1: + return arr + + for i in range(1, len(arr)): + j = i - 1 + while j >= 0 and arr[j] > arr[i]: + j -= 1 + arr.insert(j + 1, arr.pop(i)) + + return arr diff --git a/allalgorithms/sorting/merge_sort.py b/allalgorithms/sorting/merge_sort.py index ec80a5c..ed00a06 100644 --- a/allalgorithms/sorting/merge_sort.py +++ b/allalgorithms/sorting/merge_sort.py @@ -6,6 +6,8 @@ # Contributed by: Carlos Abraham Hernandez # Github: @abranhe # + + def merge_sort(arr): if len(arr) == 1: return arr diff --git a/allalgorithms/sorting/selection_sort.py b/allalgorithms/sorting/selection_sort.py new file mode 100644 index 0000000..4b17245 --- /dev/null +++ b/allalgorithms/sorting/selection_sort.py @@ -0,0 +1,19 @@ +# -*- coding: UTF-8 -*- +# +# Bubble Sort Algorithm +# The All ▲lgorithms library for python +# +# Contributed by: Martmists +# Github: @martmists +# + + +def selection_sort(seq): + for i in range(len(seq)): + min_idx = i + for j in range(min_idx, len(seq)): + if seq[j] < seq[min_idx]: + min_idx = j + + seq[i], seq[min_idx] = seq[min_idx], seq[i] + return seq diff --git a/docs/sorting/bubble-sort.md b/docs/sorting/bubble-sort.md new file mode 100644 index 0000000..fc3742f --- /dev/null +++ b/docs/sorting/bubble-sort.md @@ -0,0 +1,30 @@ +# Bubble Sort + +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. + +## Install + +``` +pip install allalgorithms +``` + +## Usage + +```py +from allalgorithms.sorting import bubble_sort + +arr = [77, 2, 10, -2, 1, 7] + +print(bubble_sort(arr)) +# -> [-2, 1, 2, 7, 10, 77] +``` + +## API + +### bubble_sort(array) + +> Returns a sorted array + +##### Params: + +- `array`: Unsorted Array diff --git a/docs/sorting/insertion-sort.md b/docs/sorting/insertion-sort.md new file mode 100644 index 0000000..8b8caa8 --- /dev/null +++ b/docs/sorting/insertion-sort.md @@ -0,0 +1,30 @@ +# Insertion Sort + +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. + +## Install + +``` +pip install allalgorithms +``` + +## Usage + +```py +from allalgorithms.sorting import insertion_sort + +arr = [77, 2, 10, -2, 1, 7] + +print(insertion_sort(arr)) +# -> [-2, 1, 2, 7, 10, 77] +``` + +## API + +### insertion_sort(array) + +> Returns a sorted array + +##### Params: + +- `array`: Unsorted Array diff --git a/docs/sorting/selection-sort.md b/docs/sorting/selection-sort.md new file mode 100644 index 0000000..b18043b --- /dev/null +++ b/docs/sorting/selection-sort.md @@ -0,0 +1,30 @@ +# Selection Sort + +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. + +## Install + +``` +pip install allalgorithms +``` + +## Usage + +```py +from allalgorithms.sorting import selection_sort + +arr = [77, 2, 10, -2, 1, 7] + +print(selection_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_searches.py b/tests/test_searches.py index edee228..622beeb 100644 --- a/tests/test_searches.py +++ b/tests/test_searches.py @@ -1,9 +1,8 @@ -from allalgorithms.searches import ( - binary_search -) - import unittest +from allalgorithms.searches import binary_search + + class TestSearches(unittest.TestCase): def test_binary_search(self): @@ -13,5 +12,6 @@ def test_binary_search(self): self.assertEqual(None, binary_search(arr, 8)) self.assertEqual(None, binary_search(arr, -1)) + if __name__ == '__main__': unittest.main() diff --git a/tests/test_sorting.py b/tests/test_sorting.py index 09db9de..e11902b 100644 --- a/tests/test_sorting.py +++ b/tests/test_sorting.py @@ -1,12 +1,25 @@ +import unittest + from allalgorithms.sorting import ( - merge_sort + bubble_sort, + insertion_sort, + merge_sort, + selection_sort ) -import unittest class TestSorting(unittest.TestCase): + def test_merge_sort(self): + self.assertEqual([-44, 1, 2, 3, 7, 19], merge_sort([7, 3, 2, 19, -44, 1])) + + def test_bubble_sort(self): + self.assertEqual([-44, 1, 2, 3, 7, 19], bubble_sort([7, 3, 2, 19, -44, 1])) + + def test_insertion_sort(self): + self.assertEqual([-44, 1, 2, 3, 7, 19], insertion_sort([7, 3, 2, 19, -44, 1])) + + def test_selection_sort(self): + self.assertEqual([-44, 1, 2, 3, 7, 19], selection_sort([7, 3, 2, 19, -44, 1])) - def test_merge_sort(self): - self.assertEqual([-44, 1, 2, 3, 7, 19], merge_sort([7, 3, 2, 19, -44, 1])) if __name__ == "__main__": - unittest.main() + unittest.main()