Skip to content

Commit 01e63a7

Browse files
author
Brett
committed
update version to 0.3.0
added new argument: -c / --compare that allows a user to display a comparison between the default sorted() method built in to python with the selected custom sort algorithm. added new tests to reflect this new argument. added new tests for sorter.py helper methods. readme updated to reflect changes.
1 parent ed1ae6c commit 01e63a7

File tree

3 files changed

+97
-29
lines changed

3 files changed

+97
-29
lines changed

README.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Sort Integers using different sorting algorithms!
66

77
## Getting Started
88

9-
Check the [releases](https://github.com/becurrie/sorters-py/releases) section to find the latest working zip/tar.
9+
Check the [releases](https://github.com/becurrie/py-sorts/releases) section to find the latest working zip/tar.
1010

1111
### ```python -m py_sorter [args]```
1212

@@ -19,30 +19,33 @@ Check the [releases](https://github.com/becurrie/sorters-py/releases) section to
1919

2020
#### Arguments
2121

22-
| Argument | Description | Example |
23-
|-----------------|------------------------------------------------------------|---------------------------------------|
24-
| -s / --sort | type of sort being performed on list | ```python -m py_sorter -s radix``` |
25-
| -i / --integers | provide a list of integers to be sorted | ```python -m py_sorter -i 9 34 5 4``` |
26-
| -g / --generate | generate a random list of integers to be sorted | ```python -m py_sorter -g 1000``` |
27-
| -l / --list | displays the original/sorted lists | ```python -m py_sorter -l``` |
28-
| -a / --allsorts | perform sort on list with each algorithm, except bogo sort | ```python -m py_sorter -a``` |
22+
| Argument | Description | Example |
23+
|-----------------|--------------------------------------------------------------------------------------|---------------------------------------|
24+
| -s / --sort | type of sort being performed on list. | ```python -m py_sorter -s radix``` |
25+
| -i / --integers | provide a list of integers to be sorted. | ```python -m py_sorter -i 9 34 5 4``` |
26+
| -g / --generate | generate a random list of integers to be sorted. | ```python -m py_sorter -g 1000``` |
27+
| -l / --list | displays the original/sorted lists. | ```python -m py_sorter -l``` |
28+
| -a / --allsorts | perform sort on list with each algorithm, except bogo sort. | ```python -m py_sorter -a``` |
29+
| -c / --compare | display a time comparison between chosen sort and pythons default sorted() function. | ```python -m py_sorter -c``` |
2930

3031
## Examples
3132

3233
```bash
33-
python -m py_sorter --generate 8 --sort bogo --list
34+
python -m py_sorter -g 8 -s bogo -l -c
3435
```
3536

3637
Output:
3738

3839
```
39-
Shuffles: 20,222
40-
Algorithm: [BOGO]
40+
Shuffles: 63,257
41+
Algorithm: [bogo]
4142
Original List:
42-
126, 61, 946, 874, 990, 488, 601, 787
43+
468, 846, 801, 976, 261, 641, 72, 698
4344
Sorted List:
44-
61, 126, 488, 601, 787, 874, 946, 990
45-
Time(seconds): 0.10767681062733224
45+
72, 261, 468, 641, 698, 801, 846, 976
46+
Time(seconds) bogo: 0.3453168464965863
47+
Time(seconds) sorted(): 3.5189852184980275e-06
48+
bogo was 0.3453133275113678 seconds slower.
4649
```
4750
***
4851

@@ -53,8 +56,8 @@ python -m py_sorter --generate 10000 --sort quick
5356
Output:
5457

5558
```
56-
Algorithm: [QUICK]
57-
Time(seconds): 0.026681231351216618
59+
Algorithm: [quick]
60+
Time(seconds): 0.02490532463518387
5861
```
5962

6063
### Caution

py_sorter/sorter.py

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
"""py_sorter.sorter: provides argument parsing and entry point main()."""
2-
3-
__version__ = "0.2.1"
4-
1+
"""py_sorter.sorter: provides argument parsing and entry point main().
2+
Any calls to the sorts methods are called from here based on the -s/--sort
3+
argument specified by the user.
4+
"""
5+
__version__ = "0.3.0"
56

67
import argparse
78
import os
@@ -17,16 +18,16 @@ def parse_args(args):
1718
"""
1819
parser = argparse.ArgumentParser(description='sort some integers.')
1920

20-
# Parser group created to take in either one argument.
21+
# Parser group created to take in either argument. (-i or -g).
2122
# Integers manually specified or generated on the fly.
2223
integers_group = parser.add_mutually_exclusive_group(required=True)
2324
integers_group.add_argument('-i', '--integers', type=int, nargs='+',
2425
help='integer(s) being sorted.')
2526
integers_group.add_argument('-g', '--generate', type=int,
2627
help='generate a random list of integers to sort.')
2728

29+
# Specific sort algorithm or use all sort algorithms.
2830
sorting_group = parser.add_mutually_exclusive_group(required=True)
29-
# Specify a specific sorting algorithm.
3031
sorting_group.add_argument('-s', '--sort', type=str,
3132
choices=['bubble', 'bogo', 'merge', 'selection', 'quick', 'radix', 'insertion',
3233
'insertion_recursive', 'heap'],
@@ -38,19 +39,23 @@ def parse_args(args):
3839
parser.add_argument('-l', '--list', action='store_true',
3940
help='displays the original and unsorted lists if present.')
4041

42+
# Compare argument to display the time difference compared to the default python sorted() function.
43+
parser.add_argument('-c', '--compare', action='store_true',
44+
help='display the difference in time compared to pythons default \'sorted()\' function.')
45+
4146
return parser.parse_args(args)
4247

4348

4449
def print_error(args, error):
4550
"""Print an error to the screen when a sort fails."""
46-
print('\tAlgorithm: [%s]' % str.upper(args.sort))
51+
print('\tAlgorithm: [%s]' % str(args.sort))
4752
print("\terror occurred while sorting: %s" % error)
4853
print("\t")
4954

5055

5156
def print_results(args, sorted_list, time):
5257
"""Print an original list, sorted list and time required to sort the original list."""
53-
print('\tAlgorithm: [%s]' % str.upper(args.sort))
58+
print('\tAlgorithm: [%s]' % str(args.sort))
5459

5560
# Determine whether or not the original and sorted lists will be printed
5661
# as part of the results output.
@@ -62,11 +67,31 @@ def print_results(args, sorted_list, time):
6267
for sorted_chunk in print_list_chunks(sorted_list, 20):
6368
print('\t' + str(sorted_chunk)[1:-1])
6469

65-
# Print time required to sort list.
66-
print('\tTime(seconds): %s' % time)
70+
# Check for the --compare flag being present, and print out the difference
71+
# between this results time and the default sorted() function time.
72+
if args.compare:
73+
print('\tTime(seconds) %s: %s' % (args.sort, time))
74+
print('\tTime(seconds) sorted(): %s' % args.compare_time)
75+
print('\t%s' % calculate_compare_time_difference(time, args.compare_time, args.sort))
76+
else:
77+
print('\tTime(seconds): %s' % time)
78+
6779
print('\t')
6880

6981

82+
def calculate_compare_time_difference(algorithm_time, default_time, algorithm):
83+
"""Calculate the difference between a custom algorithms time taken to sort, and
84+
pythons sorted() function time taken to sort the same list, returns a readable string
85+
detailing which was faster.
86+
"""
87+
difference = algorithm_time - default_time
88+
89+
if difference > 0:
90+
return '%s was %s seconds slower.' % (algorithm, difference)
91+
else:
92+
return '%s was %s seconds faster.' % (algorithm, -difference)
93+
94+
7095
def print_list_chunks(integer_list, n):
7196
"""Simple helper method to print out a list in chunks. This allows
7297
the console output to never surpass a specific width. Giving a cleaner
@@ -130,6 +155,14 @@ def sort(args):
130155

131156
# Final default_timer() method call to grab time after sort is completed.
132157
final = timeit.default_timer()
158+
159+
# Check for compare flag and get the time taken for the default sorted() call on list.
160+
if args.compare:
161+
default_initial = timeit.default_timer()
162+
default_list = sorted(original_list)
163+
default_final = timeit.default_timer()
164+
args.compare_time = default_final - default_initial
165+
133166
print_results(args, sorted_list, final - initial)
134167

135168

tests/run_all.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
sys.path.insert(0, os.path.abspath('..'))
77

88
from py_sorter.sorts import *
9-
from py_sorter.sorter import parse_args
9+
from py_sorter.sorter import parse_args, calculate_compare_time_difference
1010

1111

1212
class TestParsing(unittest.TestCase):
1313
"""Test each argument available in the parser located in the sorter.py file."""
14-
1514
def test_integers(self):
1615
"""Test the optional one of: integers argument."""
1716
parser = parse_args(['-i', '1', '3', '6', '9', '-s', 'bubble'])
@@ -45,10 +44,43 @@ def test_list(self):
4544
parser = parse_args(['-g', '10', '-s', 'bubble'])
4645
self.assertEqual(False, parser.list)
4746

47+
def test_compare(self):
48+
"""Test the optional compare argument."""
49+
parser = parse_args(['-g', '10', '-s', 'bubble', '-c'])
50+
self.assertTrue(parser.compare)
51+
self.assertEqual(True, parser.compare)
52+
53+
parser = parse_args(['-g', '10', '-s', 'bubble'])
54+
self.assertEqual(False, parser.compare)
55+
56+
57+
class TestHelpers(unittest.TestCase):
58+
"""Test any helper methods present in the sorter.py file."""
59+
def test_compare_difference_string_slower(self):
60+
"""Test that the calculate_compare_time_difference function will always
61+
return the proper string when the custom algorithm takes longer then the
62+
default python sorted() function.
63+
"""
64+
test_algorithm = 'bubble'
65+
test_algorithm_time = 5
66+
test_sorted_time = 1
67+
result = calculate_compare_time_difference(test_algorithm_time, test_sorted_time, test_algorithm)
68+
self.assertEqual('bubble was 4 seconds slower.', result)
69+
70+
def test_compare_difference_string_faster(self):
71+
"""Test that the calculate_compare_time_difference function will always
72+
return the proper string when the custom algorithm is faster then the
73+
default python sorted() function.
74+
"""
75+
test_algorithm = 'bubble'
76+
test_algorithm_time = 2
77+
test_sorted_time = 4
78+
result = calculate_compare_time_difference(test_algorithm_time, test_sorted_time, test_algorithm)
79+
self.assertEqual('bubble was 2 seconds faster.', result)
80+
4881

4982
class TestSorts(unittest.TestCase):
5083
"""Test each sorting method located in the sorts.py file."""
51-
5284
def setUp(self):
5385
"""Simple setup function to create the actual/expected
5486
lists that each sort method should produce if working as

0 commit comments

Comments
 (0)