Skip to content

Add Bubble, Insertion and Selection sort #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,6 @@ venv.bak/

# mypy
.mypy_cache/

# PyCharm
.idea/
5 changes: 4 additions & 1 deletion allalgorithms/sorting/__init__.py
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions allalgorithms/sorting/bubble_sort.py
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions allalgorithms/sorting/insertion_sort.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions allalgorithms/sorting/merge_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
# Contributed by: Carlos Abraham Hernandez
# Github: @abranhe
#


def merge_sort(arr):
if len(arr) == 1:
return arr
Expand Down
19 changes: 19 additions & 0 deletions allalgorithms/sorting/selection_sort.py
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions docs/sorting/bubble-sort.md
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions docs/sorting/insertion-sort.md
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions docs/sorting/selection-sort.md
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions tests/test_searches.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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()
23 changes: 18 additions & 5 deletions tests/test_sorting.py
Original file line number Diff line number Diff line change
@@ -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()