Skip to content
Open
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
17 changes: 16 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,22 @@ language: python
python:
- "2.7"
- "3.6"

notifications:
email: false

install:
- pip install tox-travis
- pip install coveralls

script:
- tox

after_success:
- coveralls

install:
- pip install -e .
- pip install -r requirements.txt
script: py.test
script: py.test

14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@

# data-structures

### Bubble Sort

```
Bubble sort takes in a list of numbers and uses a bubble sort method to return a sorted list.

To use bubble_sort, from bubble_sort import bubble_sort.
Pass in a list of numbers bubble_sort(list).

* _bubble_sort(list) (O(n^2))_



# Data-Structures

Where a variety of data-structures found in python are being explored, such as:
Expand Down
53 changes: 53 additions & 0 deletions src/bubble_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""Function to run a bubble sort on a given list of numbers."""
import time
from random import randint


def bubble_sort(list):
"""Bubble sort function."""
global looped
looped = 1
for i in range(len(list) - looped):
if list[i] > list[i + 1]:
list[i], list[i + 1] = list[i + 1], list[i]
else:
continue # pragma: no cover
looped += 1
bubble_sort(list)
return list

if __name__ == '__main__':

short_list = [randint(1, 50) for _ in range(10)]
print('\nCASE 1: A small list to be sorted:\n', short_list)
short_list = timeit.timeit("bubble_sort(short_list)", setup="from __main__ import short_list, bubble_sort")
print('Short list time: ', short_list)

print('\nCASE 2: A list of 100 numbers:\n')
hundred = [randint(1, 100000) for x in range(100)]
start_hundred = time.time()
solve_hundred = (time.time() - start_hundred) * 1000
print(bubble_sort(hundred))
print('\nSorted using bubble_sort() in {} seconds.'.format(solve_hundred))

print('\nCASE 3: A list of 1,000 random numbers:')
thousand = [randint(1, 100000) for x in range(1000)]
start_thousand = time.time()
solve_thousand = (time.time() - start_thousand) * 1000
print(thousand)
print(bubble_sort(thousand))
print('\nSorted using bubble_sort() {} seconds'.format(solve_thousand))

print('\nCASE 4: A list of 10,000 numbers (not shown):\n')
ten = [randint(1, 100000) for x in range(10000)]
start_ten = time.time()
solve_ten = (time.time() - start_ten) * 1000
print(bubble_sort(ten))
print('\nSorted using bubble_sort() in {} seconds.'.format(solve_ten))

print('\nCASE 5: A list of 100,000 words:\n')
mil = [randint(1, 100000) for x in range(100000)]
start_mil = time.time()
solve_mil = (time.time() - start_mil) * 1000
print(bubble_sort(mil))
print('\nSorted using bubble_sort() in {} seconds.'.format(solve_mil))
39 changes: 39 additions & 0 deletions src/test_bubble_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Test bubble sort."""

from bubble_sort import bubble_sort


def test_bubble_sort_on_empty_list_returns_empty():
"""Test bubble sort on empty list retruns empty list."""
test = []
assert bubble_sort(test) == []


def test_bubble_sort_on_list_of_three():
"""Test bubble sort on list of three retuned sorted."""
test = [6, 8, 2]
assert bubble_sort(test) == [2, 6, 8]


def test_bubble_sort_with_sorted_list_no_change():
"""Test bubble sort with sorted list returns no change."""
test = [1, 2, 3, 4, 5, 6, 7, 8, 9]
assert bubble_sort(test) == [1, 2, 3, 4, 5, 6, 7, 8, 9]


def test_bubble_sort_on_reverse_sort_returns_sorted():
"""Test bubble sort with reverse sort returns sorted."""
test = [9, 8, 7, 6, 5, 4, 3, 2, 1]
assert bubble_sort(test) == [1, 2, 3, 4, 5, 6, 7, 8, 9]


def test_bubble_sort_of_all_nums_same_returns_same():
"""Test bubble sort if all nums same returns the same."""
test = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
assert bubble_sort(test) == test


def test_bubble_sort_with_one_variant_returns_sorted():
"""Test bubble sort with one variant returns sorted."""
test = [0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
assert bubble_sort(test) == [0, 0, 0, 0, 0, 0, 0, 0, 0, 1]