Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented a few sorting algorithms in Python
- Loading branch information
0 parents
commit 1c0a77c
Showing
3 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.pyc | ||
*.swp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
#!/usr/bin/env python2 | ||
from copy import deepcopy | ||
from random import randrange, shuffle | ||
|
||
def selection(data): | ||
''' | ||
Probably the most naive sort. It is only useful in the rare cases when you're looking for a minimum number of swaps | ||
Complexity: | ||
Time: O(n^2) | ||
Space: O(1) | ||
Adaptive: NO | ||
Stable: NO | ||
''' | ||
data = deepcopy( data ) | ||
for repeat in range(0, len(data)): | ||
for index, number in enumerate(data): | ||
if index < len(data)-1 and data[index] > data[index+1]: | ||
data[index], data[index+1] = data[index+1], data[index] | ||
|
||
return data | ||
|
||
def bubble(data): | ||
''' | ||
The classic bubble sort | ||
Complexity: | ||
Time: O(n^2) | ||
Space: O(1) | ||
Adaptive: YES, O(n) when nearly sorted | ||
Stable: YES | ||
''' | ||
data = deepcopy( data ) | ||
for repeat in range(0, len(data)-1): | ||
index = 0 | ||
while(index < len(data) - 1): | ||
if data[index] > data[index + 1] : | ||
# Swap variables | ||
data[index], data[index + 1] = data[index + 1], data[index] | ||
index += 1 | ||
return data | ||
|
||
def insertion(data): | ||
''' | ||
The classic insertion sort | ||
Complexity: | ||
Time: O(n^2) | ||
Space: O(1) | ||
Adaptive: YES, O(n) when nearly sorted | ||
Stable: YES | ||
''' | ||
data = deepcopy( data ) | ||
for repeat in range(0, len(data)): | ||
index = repeat | ||
while index > 0 and data[index] < data[index - 1]: | ||
data[index], data[index - 1] = data[index - 1], data[index] | ||
index -= 1 | ||
return data | ||
|
||
def merge(data): | ||
''' | ||
Recursive merge sort | ||
''' | ||
data = deepcopy( data ) | ||
if len(data) <= 1: return data | ||
left, right = [], [] | ||
middle = len(data) // 2 | ||
|
||
left = data[:middle] | ||
right = data[middle:] | ||
|
||
left = merge(left) | ||
right = merge(right) | ||
|
||
result = __merge(left, right) | ||
return result | ||
|
||
def __merge(left, right): | ||
''' Sorts the contents of two lists ''' | ||
result = [] | ||
while len(left) > 0 and len(right) > 0: | ||
if left[0] < right[0]: result.append(left.pop(0)) | ||
else: result.append(right.pop(0)) | ||
|
||
if len(left) > 0: result += left | ||
elif len(right) > 0: result += right | ||
|
||
return result | ||
|
||
def validate(data): | ||
''' | ||
Checks if a list is sorted | ||
''' | ||
for index in range(0, len(data)-1): | ||
if data[index] > data[index+1]: return False | ||
|
||
return True | ||
|
||
|
||
def bogo(data): | ||
''' | ||
This is a joke sort | ||
works by shuffling the list until the order is right :) | ||
''' | ||
data = deepcopy( data ) | ||
while( not validate(data) ): | ||
shuffle(data) | ||
return data | ||
|
||
def quick(data): | ||
if data == []: return [] | ||
else: | ||
pivot = data.pop(randrange(len(data))) | ||
lesser = quick([l for l in data if l < pivot]) | ||
greater = quick([l for l in data if l >= pivot]) | ||
return lesser + [pivot] + greater | ||
|
||
|
||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/usr/bin/env python2 | ||
import unittest | ||
from random import randint | ||
from sorting import * | ||
|
||
class TestSortingAlgorithms(unittest.TestCase): | ||
def validate(self, data): | ||
''' | ||
Checks if a list is sorted | ||
''' | ||
for index in range(0, len(data)-1): | ||
if data[index] > data[index+1]: return False | ||
|
||
return True | ||
|
||
def randomlist(self, minsize=5, maxsize=1000): | ||
''' | ||
Create a random list of random integers | ||
''' | ||
size = randint(minsize, maxsize) | ||
return [ randint(0, 100) for item in range(0, size) ] | ||
|
||
def generic_test(self, algorithm, repeats=5): | ||
for repeat in range(0, repeats): | ||
data = self.randomlist() | ||
self.assertTrue(self.validate(algorithm(data))) | ||
|
||
def test_selection(self): | ||
self.generic_test(selection) | ||
|
||
def test_bubble(self): | ||
self.generic_test(selection) | ||
|
||
def test_insertion(self): | ||
self.generic_test(insertion) | ||
|
||
def test_merge(self): | ||
self.generic_test(merge) | ||
|
||
def test_bogo(self): | ||
data = self.randomlist(minsize=5, maxsize=6) | ||
self.assertTrue(self.validate(bogo(data))) | ||
|
||
def test_quick(self): | ||
self.generic_test(quick) | ||
|
||
if __name__ == "__main__": | ||
suite = unittest.TestLoader().loadTestsFromTestCase(TestSortingAlgorithms) | ||
unittest.TextTestRunner(verbosity=1).run(suite) | ||
|