Skip to content

Commit d79bceb

Browse files
committedDec 4, 2015
updated readme and clean ups
1 parent 890b0bb commit d79bceb

File tree

3 files changed

+119
-7
lines changed

3 files changed

+119
-7
lines changed
 

‎README.md

+110-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,115 @@
1-
# python-sorts
2-
Comparison of sorting algorithms in Python. Pass arguments to generate shuffled array of arbitrary length.
1+
## Python Sorting Algorithms
2+
Comparison of sorting algorithms in Python. Run algorithms on arrays of user specified lengths and analyze time taken by each algorithm to sort the array. All algorithms are tested so be confident that arrays are sorted correctly.
33

4-
Requirements:
4+
Python's builtin 'sorted' method uses Timsort algorithm created by Tim Peters, The man behind python's easter egg (import this). Though in these tests 'sorted' function has upper hand on other algorithms because of being a builtin function, but still you have to admit Tim Peters is awesome. Get started ...
5+
``` sh
6+
git clone https://github.com/falloutcoder/python-sorts.git sorts
7+
```
8+
## Requirements:
59
Currently there is only one dependency 'Tabulate', To print summary table for algorithms in better form.
610

7-
Run following command to install:
11+
Run following command to install it:
12+
13+
``` sh
814
pip install -r requirements.txt
15+
```
16+
17+
## Run Tests Before Using
18+
19+
``` sh
20+
$ python sorts/test.py
21+
Testing shell_sort
22+
Testing insertion_sort
23+
Testing quick_sort
24+
Testing bubble_sort
25+
Testing tim_sort
26+
Testing merge_sort
27+
Testing selection_sort
28+
.
29+
----------------------------------------------------------------------
30+
Ran 1 test in 0.009s
31+
32+
OK
33+
34+
```
35+
36+
On some machines below error is expected:
37+
``` sh
38+
ImportError: Import by filename is not supported.
39+
```
40+
## Usage
41+
42+
Default run without any arguments
43+
44+
```sh
45+
$ python sorts
46+
||||||||||||||||||||||||||||||||||||||||||||||||||
47+
Sorting Algorithms Ran!
48+
Array Details And Algorithms Summary As Follow
49+
||||||||||||||||||||||||||||||||||||||||||||||||||
50+
--------------------------------------------------
51+
Length Of Array: 1000
52+
Range Of Numbers In Array: 0 to 999
53+
Number Of Times Array Were Sorted: 1
54+
--------------------------------------------------
55+
╒══════════════════╤═════════════════════════════════╕
56+
│ Algorithm Name │ Time Taken (Less Is Better!!) │
57+
╞══════════════════╪═════════════════════════════════╡
58+
│ Bubble Sort │ 0.03952097892761230469 sec │
59+
├──────────────────┼─────────────────────────────────┤
60+
│ Insertion Sort │ 0.02639412879943847656 sec │
61+
├──────────────────┼─────────────────────────────────┤
62+
│ Selection Sort │ 0.02118921279907226562 sec │
63+
├──────────────────┼─────────────────────────────────┤
64+
│ Merge Sort │ 0.00230097770690917969 sec │
65+
├──────────────────┼─────────────────────────────────┤
66+
│ Shell Sort │ 0.00182795524597167969 sec │
67+
├──────────────────┼─────────────────────────────────┤
68+
│ Quick Sort │ 0.00158691406250000000 sec │
69+
├──────────────────┼─────────────────────────────────┤
70+
│ Tim Sort │ 0.00016212463378906250 sec │
71+
╘══════════════════╧═════════════════════════════════╛
72+
73+
```
74+
75+
### Avaiable Parameters
76+
```sh
77+
$ python sorts -h
78+
usage: sorts [-h] [-s SIZE] [-l LOOP] [-r RANGE]
79+
80+
optional arguments:
81+
-h, --help show this help message and exit
82+
-s SIZE, --size SIZE Size of array to be sorted
83+
-l LOOP, --loop LOOP Number of times array sorting should be repeated
84+
-r RANGE, --range RANGE
85+
Max range of number that should be present in array
86+
87+
$ python sorts -s 4500 -r 60000
88+
||||||||||||||||||||||||||||||||||||||||||||||||||
89+
Sorting Algorithms Ran!
90+
Array Details And Algorithms Summary As Follow
91+
||||||||||||||||||||||||||||||||||||||||||||||||||
92+
--------------------------------------------------
93+
Length Of Array: 4500
94+
Range Of Numbers In Array: 0 to 59999
95+
Number Of Times Array Were Sorted: 1
96+
--------------------------------------------------
97+
╒══════════════════╤═════════════════════════════════╕
98+
│ Algorithm Name │ Time Taken (Less Is Better!!) │
99+
╞══════════════════╪═════════════════════════════════╡
100+
│ Bubble Sort │ 0.76305413246154785156 sec │
101+
├──────────────────┼─────────────────────────────────┤
102+
│ Insertion Sort │ 0.51503109931945800781 sec │
103+
├──────────────────┼─────────────────────────────────┤
104+
│ Selection Sort │ 0.45219206809997558594 sec │
105+
├──────────────────┼─────────────────────────────────┤
106+
│ Merge Sort │ 0.01211810111999511719 sec │
107+
├──────────────────┼─────────────────────────────────┤
108+
│ Shell Sort │ 0.01209998130798339844 sec │
109+
├──────────────────┼─────────────────────────────────┤
110+
│ Quick Sort │ 0.00762200355529785156 sec │
111+
├──────────────────┼─────────────────────────────────┤
112+
│ Tim Sort │ 0.00092196464538574219 sec │
113+
╘══════════════════╧═════════════════════════════════╛
9114

115+
```

‎__main__.py

+1
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,6 @@ def run():
7171
# Run all sorting algorithms
7272
sort_algos(sample, size, no_of_times=repeat, sample_range=max_range)
7373

74+
7475
if __name__ == "__main__":
7576
run()

‎test.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import importlib
55
import unittest
66

7+
78
class SortAlgosTest(unittest.TestCase):
9+
810
def setUp(self):
911
"""
1012
Common setup to load all sorting modules in current package
@@ -24,9 +26,12 @@ def test_algos_with_sequential_array(self):
2426
"""
2527
self.array = random.sample(range(50), 50)
2628
for algo, module in self.sorting_algos:
27-
print ("Testing {}".format(algo))
28-
result = getattr(module, algo)(array=self.array[:], repeat=1).output
29+
print("Testing {}".format(algo))
30+
result = getattr(module, algo)(
31+
array=self.array[:], repeat=1).output
2932
for index, val in enumerate(range(50)):
3033
assert val == result[index]
34+
35+
3136
if __name__ == '__main__':
32-
unittest.main()
37+
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.