Skip to content

Commit 29ec014

Browse files
author
IanDoarn
committed
Updated sorting package and packages __init__.py. when users are using this from the command line, after importing
the module or its packages they can call help() on the methods or packages. Update modular level __init__.py: - Added proper docstrings to top of files - optimized imports - added __version__, __author__, __maintainers__, __contributors__ - Added __all__ variable to list all modules in package Brought all files in sorting module to PEP8 compliance: - Added proper docstrings to top of files - Added proper docstrings to methods - removed camel casing from variable and method names, they are not PEP8 compliant Fixed sorting module __init__.py: - Added proper docstrings to top of file - Added __all__ variable to list all modules in package - optimized imports Example: ``` import pygorithm help(pygorithm) Help on package pygorithm: NAME pygorithm - Pygorithm DESCRIPTION A Python module to learn all the major algorithms on the go! Purely for educational purposes If you have found my software to be of any use to you, do consider helping me pay my internet bills. This would encourage me to create many such softwares :) PayPal -> https://paypal.me/omkarpathak27 INR - > https://www.instamojo.com/@omkarpathak/ PACKAGE CONTENTS data_structures (package) fibonacci (package) math (package) searching (package) sorting (package) DATA __all__ = ['data_structures', 'fibonacci', 'math', 'searching', 'sorti... __contributors__ = ['Mohamed Kiouaz', 'Ashutosh Gupta'] __maintainers__ = ['Omkar Pathak'] VERSION 1.0.0 AUTHOR Omkar Pathak FILE c:\users\doarni\dev\pygorithm\pygorithm\__init__.py ```
1 parent 243b632 commit 29ec014

File tree

12 files changed

+473
-190
lines changed

12 files changed

+473
-190
lines changed

pygorithm/__init__.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
Pygorithm
3+
4+
A Python module to learn all the major algorithms on the go!
5+
Purely for educational purposes
6+
7+
If you have found my software to be of any use to you,
8+
do consider helping me pay my internet bills.
9+
This would encourage me to create many such softwares :)
10+
11+
PayPal -> https://paypal.me/omkarpathak27
12+
INR - > https://www.instamojo.com/@omkarpathak/
13+
"""
14+
from pygorithm import data_structures
15+
from pygorithm import fibonacci
16+
from pygorithm import math
17+
from pygorithm import searching
18+
from pygorithm import sorting
19+
20+
__version__ = '1.0.0'
21+
__author__ = 'Omkar Pathak'
22+
23+
# List maintainers here
24+
__maintainers__ = ['Omkar Pathak']
25+
# List contributors here
26+
__contributors__ = ['Mohamed Kiouaz',
27+
'Ashutosh Gupta']
28+
29+
__all__ = ['data_structures',
30+
'fibonacci',
31+
'math',
32+
'searching',
33+
'sorting']

pygorithm/sorting/__init__.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,22 @@
1-
from . modules import modules
1+
"""
2+
Collection of sorting methods
3+
"""
4+
from . import quick_sort
5+
from . import bucket_sort
6+
from . import bubble_sort
7+
from . import heap_sort
8+
from . import counting_sort
9+
from . import insertion_sort
10+
from . import merge_sort
11+
from . import selection_sort
12+
from . import shell_sort
13+
14+
__all__ = ['bubble_sort',
15+
'bucket_sort',
16+
'counting_sort',
17+
'heap_sort',
18+
'insertion_sort',
19+
'merge_sort',
20+
'quick_sort',
21+
'selection_sort',
22+
'shell_sort']

pygorithm/sorting/bubble_sort.py

Lines changed: 56 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,63 @@
1-
# Author: OMKAR PATHAK
2-
# Contributors: Mohamed Kiouaz
3-
# Created On: 31st July 2017
4-
5-
# Best O(n); Average O(n*(n-1)/4); Worst O(n^2)
6-
7-
# Bubble Sorting algorithm
8-
def sort(List):
9-
for i in range(len(List)):
10-
for j in range(len(List) - 1, i, -1):
11-
if List[j] < List[j - 1]:
12-
List[j], List[j - 1] = List[j - 1], List[j]
13-
return List
14-
15-
# Improved Bubble Sorting algorithm
16-
def improved_sort(List):
17-
for i in range(len(List)):
1+
"""
2+
Author: OMKAR PATHAK
3+
Contributors: Mohamed Kiouaz
4+
Created On: 31st July 2017
5+
6+
Best O(n); Average O(n*(n-1)/4); Worst O(n^2)
7+
"""
8+
import inspect
9+
10+
11+
def sort(_list):
12+
"""
13+
Bubble Sorting algorithm
14+
15+
:param _list: list of values to sort
16+
:return: sorted values
17+
"""
18+
for i in range(len(_list)):
19+
for j in range(len(_list) - 1, i, -1):
20+
if _list[j] < _list[j - 1]:
21+
_list[j], _list[j - 1] = _list[j - 1], _list[j]
22+
return _list
23+
24+
25+
def improved_sort(_list):
26+
"""
27+
Improved Bubble Sorting algorithm
28+
29+
:param _list: list of values to sort
30+
:return: sorted values
31+
"""
32+
for i in range(len(_list)):
1833
stop = True
19-
for j in range(len(List) - 1, i, -1):
20-
if List[j] < List[j - 1]:
34+
for j in range(len(_list) - 1, i, -1):
35+
if _list[j] < _list[j - 1]:
2136
stop = False
22-
List[j], List[j - 1] = List[j - 1], List[j]
23-
if(stop == True):
24-
return List
25-
return List
37+
_list[j], _list[j - 1] = _list[j - 1], _list[j]
38+
if stop:
39+
return _list
40+
return _list
41+
2642

27-
# time complexities
43+
# TODO: Are these necessary?
2844
def time_complexities():
29-
return '''Best Case: O(n) , Average Case: O(n ^ 2), Worst Case: O(n ^ 2) \n For Improved Bubble Sort: \n Best Case: O(n); Average Case: O(n * (n - 1) / 4); Worst Case: O(n ^ 2) '''
45+
"""
46+
Return information on functions
47+
time complexity
48+
:return: string
49+
"""
50+
return "Best Case: O(n), " \
51+
"Average Case: O(n ^ 2), " \
52+
"Worst Case: O(n ^ 2).\n\n" \
53+
"For Improved Bubble Sort:\nBest Case: O(n); Average Case: O(n * (n - 1) / 4); Worst Case: O(n ^ 2)"
54+
3055

31-
# easily retrieve the source code of the sort function
3256
def get_code():
33-
import inspect
34-
return inspect.getsource(sort)
57+
"""
58+
easily retrieve the source code
59+
of the sort function
3560
36-
# easily retrieve the source code of the sort function
37-
def get_improved_code():
38-
import inspect
39-
return inspect.getsource(improved_sort)
61+
:return: source code
62+
"""
63+
return inspect.getsource(sort)

pygorithm/sorting/bucket_sort.py

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,83 @@
1-
# Author: OMKAR PATHAK
2-
# Created On: 31st July 2017
1+
"""
2+
Author: OMKAR PATHAK
3+
Created On: 31st July 2017
34
4-
# Best O(n); Average O(n); Worst O(n)
5+
- Best O(n)
6+
- Average O(n)
7+
- Worst O(n)
8+
"""
9+
import math
10+
from pygorithm.sorting import insertion_sort
11+
import inspect
512

6-
# bucket sort algorithm
7-
def sort(List, bucketSize = 5):
8-
from pygorithm.sorting import insertion_sort
9-
import math
1013

14+
def sort(_list, bucket_size=5):
15+
"""
16+
bucket sort algorithm
17+
18+
:param _list: list of values to sort
19+
:param bucket_size: Size of the bucket
20+
:return: sorted values
21+
"""
1122
string = False
1223

13-
if(len(List) == 0):
14-
print('You don\'t have any elements in array!')
15-
elif all(isinstance(element, str) for element in List):
24+
if len(_list) == 0:
25+
# print("You don\'t have any elements in array!")
26+
raise ValueError("Array can not be empty.")
27+
28+
elif all(isinstance(element, str) for element in _list):
1629
string = True
17-
List = [ord(element) for element in List]
30+
_list = [ord(element) for element in _list]
1831

19-
minValue = List[0]
20-
maxValue = List[0]
32+
min_value = _list[0]
33+
max_value = _list[0]
2134

2235
# For finding minimum and maximum values
23-
for i in range(0, len(List)):
24-
if List[i] < minValue:
25-
minValue = List[i]
26-
elif List[i] > maxValue:
27-
maxValue = List[i]
36+
for i in range(0, len(_list)):
37+
if _list[i] < min_value:
38+
min_value = _list[i]
39+
elif _list[i] > max_value:
40+
max_value = _list[i]
2841

2942
# Initialize buckets
30-
bucketCount = math.floor((maxValue - minValue) / bucketSize) + 1
43+
bucket_count = math.floor((max_value - min_value) / bucket_size) + 1
3144
buckets = []
32-
for i in range(0, bucketCount):
45+
for i in range(0, int(bucket_count)):
3346
buckets.append([])
3447

3548
# For putting values in buckets
36-
for i in range(0, len(List)):
37-
buckets[math.floor((List[i] - minValue) / bucketSize)].append(List[i])
49+
for i in range(0, len(_list)):
50+
# TODO: floor expects floats but could be receiving int or slice
51+
buckets[math.floor(float((_list[i] - min_value) / bucket_size))].append(_list[i])
3852

3953
# Sort buckets and place back into input array
40-
sortedArray = []
54+
sorted_array = []
4155
for i in range(0, len(buckets)):
4256
insertion_sort.sort(buckets[i])
4357
for j in range(0, len(buckets[i])):
44-
sortedArray.append(buckets[i][j])
58+
sorted_array.append(buckets[i][j])
4559

4660
if string:
47-
return [chr(element) for element in sortedArray]
61+
return [chr(element) for element in sorted_array]
4862
else:
49-
return sortedArray
63+
return sorted_array
5064

51-
# time complexities
65+
66+
# TODO: Are these necessary?
5267
def time_complexities():
53-
return '''Best Case: O(n), Average Case: O(n), Worst Case: O(n)'''
68+
"""
69+
Return information on functions
70+
time complexity
71+
:return: string
72+
"""
73+
return "Best Case: O(n), Average Case: O(n), Worst Case: O(n)"
74+
5475

55-
# easily retrieve the source code of the sort function
5676
def get_code():
57-
import inspect
77+
"""
78+
easily retrieve the source code
79+
of the sort function
80+
81+
:return: source code
82+
"""
5883
return inspect.getsource(sort)

pygorithm/sorting/counting_sort.py

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,58 @@
1-
# Author: OMKAR PATHAK
2-
# Created On: 31st July 2017
1+
"""
2+
Author: OMKAR PATHAK
3+
Created On: 31st July 2017
4+
5+
- Best = Average = Worst = O(n + k)
6+
"""
7+
import inspect
38

4-
# Best = Average = Worst = O(n + k)
59

610
# counting sort algorithm
7-
def sort(List):
11+
def sort(_list):
12+
"""
13+
counting sort algorithm
14+
15+
:param _list: list of values to sort
16+
:return: sorted values
17+
"""
818
try:
9-
maxValue = 0
10-
for i in range(len(List)):
11-
if List[i] > maxValue:
12-
maxValue = List[i]
19+
max_value = 0
20+
for i in range(len(_list)):
21+
if _list[i] > max_value:
22+
max_value = _list[i]
1323

14-
buckets = [0] * (maxValue + 1)
24+
buckets = [0] * (max_value + 1)
1525

16-
for i in List:
26+
for i in _list:
1727
buckets[i] += 1
18-
1928
i = 0
20-
for j in range(maxValue + 1):
21-
for a in range(buckets[j]):
22-
List[i] = j
23-
i += 1
2429

25-
return List
30+
for j in range(max_value + 1):
31+
for a in range(buckets[j]):
32+
_list[i] = j
33+
i += 1
34+
35+
return _list
2636

27-
except TypeError:
28-
print('Counting Sort can only be applied to integers')
37+
except TypeError as error:
38+
print('Counting Sort can only be applied to integers. {}'.format(error))
2939

30-
# time complexities
40+
41+
# TODO: Are these necessary?
3142
def time_complexities():
32-
return '''Best Case: O(n + k), Average Case: O(n + k), Worst Case: O(n + k)'''
43+
"""
44+
Return information on functions
45+
time complexity
46+
:return: string
47+
"""
48+
return "Best Case: O(n + k), Average Case: O(n + k), Worst Case: O(n + k)"
49+
3350

34-
# easily retrieve the source code of the sort function
3551
def get_code():
36-
import inspect
52+
"""
53+
easily retrieve the source code
54+
of the sort function
55+
56+
:return: source code
57+
"""
3758
return inspect.getsource(sort)

0 commit comments

Comments
 (0)