patternpythonMinor
Basic sorting in Python
Viewed 0 times
sortingpythonbasic
Problem
Previously asked here.
I would like to get my code for implementation of simple sorting algorithms in idiomatic python code reviewed. I am looking for feedback on python idioms used, better ways of doing the same thing. I am familiar with FP, and I am willing to put in effort to get myself familiar with pythonic idioms such as list comprehensions and enumerators. Hence I would also like feedback on if any of the explicit loops can be converted to these.
Insertion Sort
Selection Sort
Bubble Sort
```
def bubblesort(items):
"""
>>> bubblesort([])
[]
>>> bubblesort([1])
[1]
>>> bubblesort([1, 0])
[0, 1]
>>> bubblesort([2, 1, 0])
[0, 1, 2]
>>> bubblesort([1,2,3,4,5])
[1, 2, 3, 4, 5]
I would like to get my code for implementation of simple sorting algorithms in idiomatic python code reviewed. I am looking for feedback on python idioms used, better ways of doing the same thing. I am familiar with FP, and I am willing to put in effort to get myself familiar with pythonic idioms such as list comprehensions and enumerators. Hence I would also like feedback on if any of the explicit loops can be converted to these.
import doctest
from hypothesis import given
import hypothesis.strategies as stInsertion Sort
def insertionsort(items):
"""
>>> insertionsort([])
[]
>>> insertionsort([1])
[1]
>>> insertionsort([1,2,3,4,5])
[1, 2, 3, 4, 5]
>>> insertionsort([4,5,3,1,2])
[1, 2, 3, 4, 5]
"""
for k, v in enumerate(items):
for i, val in enumerate(items[:k]):
if v < val:
items[i], items[i + 1:k + 1] = items[k], items[i:k]
break
return items
@given(st.lists(elements=st.integers()))
def test_insertionsort(x):
assert insertionsort(x) == sorted(x)Selection Sort
def selectionsort(items):
"""
>>> selectionsort([])
[]
>>> selectionsort([1])
[1]
>>> selectionsort([1, 0])
[0, 1]
>>> selectionsort([2, 1, 0])
[0, 1, 2]
>>> selectionsort([1,2,3,4,5])
[1, 2, 3, 4, 5]
>>> selectionsort([4,5,3,1,2])
[1, 2, 3, 4, 5]
"""
for k in reversed(range(1, len(items))):
v, i = max((v, i) for i, v in enumerate(items[:k]))
if items[k] < v:
items[k], items[i] = items[i], items[k]
return items
@given(st.lists(elements=st.integers()))
def test_selectionsort(x):
assert selectionsort(x) == sorted(x)Bubble Sort
```
def bubblesort(items):
"""
>>> bubblesort([])
[]
>>> bubblesort([1])
[1]
>>> bubblesort([1, 0])
[0, 1]
>>> bubblesort([2, 1, 0])
[0, 1, 2]
>>> bubblesort([1,2,3,4,5])
[1, 2, 3, 4, 5]
Solution
Your code looks nice and well tested: congratulations. I have a few comments anyway.
A little design/API comment
Compare
sorted(iterable[, key][, reverse])
Return a new sorted list from the items in iterable.
and
sort(*, key=None, reverse=None)
This method sorts the list in place[...] it does not return the sorted sequence.
The difference is that one operates in place and the other one returns a new list. Your code does both which is likely to be confusing.
Other various comments
You could (and probably should) pass the sorting function as a parameter to your testing function. You'd have something like :
and :
The variable name
Suggestion
It might be worth defining a
A little design/API comment
Compare
sorted:sorted(iterable[, key][, reverse])
Return a new sorted list from the items in iterable.
and
list.sort:sort(*, key=None, reverse=None)
This method sorts the list in place[...] it does not return the sorted sequence.
The difference is that one operates in place and the other one returns a new list. Your code does both which is likely to be confusing.
Other various comments
You could (and probably should) pass the sorting function as a parameter to your testing function. You'd have something like :
@given(st.lists(elements=st.integers()))
def test_sort(x, my_sorted):
assert my_sorted(x) == sorted(x)and :
for s in [selection, bubble, insertion, quicksort, mergesort, heapsort, shellsort]:
test_sort(s)The variable name
mod might a bit confusing as one might think about the modulo operator. I guess modif or change would be better.Suggestion
It might be worth defining a
swap(i, j) function/method but it might affect performances.Code Snippets
@given(st.lists(elements=st.integers()))
def test_sort(x, my_sorted):
assert my_sorted(x) == sorted(x)for s in [selection, bubble, insertion, quicksort, mergesort, heapsort, shellsort]:
test_sort(s)Context
StackExchange Code Review Q#123318, answer score: 2
Revisions (0)
No revisions yet.