Skip to content

Commit abb6e8d

Browse files
committed
Made some changes
1 parent 2a552ca commit abb6e8d

File tree

9 files changed

+54
-54
lines changed

9 files changed

+54
-54
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.

pygorithm/sorting/bucket_sort.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@
44
# Best O(n); Average O(n); Worst O(n)
55

66
# bucket sort algorithm
7-
def sort(myList, bucketSize = 5):
7+
def sort(List, bucketSize = 5):
88
from pygorithm.sorting import insertion_sort
99
import math
1010

1111
string = False
1212

13-
if(len(myList) == 0):
13+
if(len(List) == 0):
1414
print('You don\'t have any elements in array!')
15-
elif all(isinstance(element, str) for element in myList):
15+
elif all(isinstance(element, str) for element in List):
1616
string = True
17-
myList = [ord(element) for element in myList]
17+
List = [ord(element) for element in List]
1818

19-
minValue = myList[0]
20-
maxValue = myList[0]
19+
minValue = List[0]
20+
maxValue = List[0]
2121

2222
# For finding minimum and maximum values
23-
for i in range(0, len(myList)):
24-
if myList[i] < minValue:
25-
minValue = myList[i]
26-
elif myList[i] > maxValue:
27-
maxValue = myList[i]
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]
2828

2929
# Initialize buckets
3030
bucketCount = math.floor((maxValue - minValue) / bucketSize) + 1
@@ -33,8 +33,8 @@ def sort(myList, bucketSize = 5):
3333
buckets.append([])
3434

3535
# For putting values in buckets
36-
for i in range(0, len(myList)):
37-
buckets[math.floor((myList[i] - minValue) / bucketSize)].append(myList[i])
36+
for i in range(0, len(List)):
37+
buckets[math.floor((List[i] - minValue) / bucketSize)].append(List[i])
3838

3939
# Sort buckets and place back into input array
4040
sortedArray = []

pygorithm/sorting/counting_sort.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@
44
# Best = Average = Worst = O(n + k)
55

66
# counting sort algorithm
7-
def sort(myList):
7+
def sort(List):
88
try:
99
maxValue = 0
10-
for i in range(len(myList)):
11-
if myList[i] > maxValue:
12-
maxValue = myList[i]
10+
for i in range(len(List)):
11+
if List[i] > maxValue:
12+
maxValue = List[i]
1313

1414
buckets = [0] * (maxValue + 1)
1515

16-
for i in myList:
16+
for i in List:
1717
buckets[i] += 1
1818

1919
i = 0
2020
for j in range(maxValue + 1):
2121
for a in range(buckets[j]):
22-
myList[i] = j
22+
List[i] = j
2323
i += 1
2424

25-
return myList
25+
return List
2626

2727
except TypeError:
2828
print('Counting Sort can only be applied to integers')

pygorithm/sorting/heap_sort.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,33 @@
44
# Best O(nlog(n)); Average O(nlog(n)); Worst O(nlog(n))
55

66
# heap sort algorithm
7-
def sort(alist):
8-
heapify(alist) # create the heap
9-
end = len(alist) - 1
7+
def sort(List):
8+
heapify(List) # create the heap
9+
end = len(List) - 1
1010
while end > 0:
11-
alist[end], alist[0] = alist[0], alist[end]
12-
shiftDown(alist, 0, end - 1)
11+
List[end], List[0] = List[0], List[end]
12+
shiftDown(List, 0, end - 1)
1313
end -= 1
14-
return alist
14+
return List
1515

16-
def heapify(alist):
16+
def heapify(List):
1717
''' This function helps to maintain the heap property '''
18-
# start = (len(alist) - 2) // 2 (faster execution)
19-
start = len(alist) // 2
18+
# start = (len(List) - 2) // 2 (faster execution)
19+
start = len(List) // 2
2020
while start >= 0:
21-
shiftDown(alist, start, len(alist) - 1)
21+
shiftDown(List, start, len(List) - 1)
2222
start -= 1
2323

24-
def shiftDown(alist, start, end):
24+
def shiftDown(List, start, end):
2525
root = start
2626
while root * 2 + 1 <= end:
2727
child = root * 2 + 1
2828
# right child exists and is greater than left child
29-
if child + 1 <= end and alist[child] < alist[child + 1]:
29+
if child + 1 <= end and List[child] < List[child + 1]:
3030
child += 1
3131
# if child is greater than root(parent), then swap their positions
32-
if child <= end and alist[root] < alist[child]:
33-
alist[root], alist[child] = alist[child], alist[root]
32+
if child <= end and List[root] < List[child]:
33+
List[root], List[child] = List[child], List[root]
3434
root = child
3535
else:
3636
return

pygorithm/sorting/merge_sort.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ def merge(a,b):
2121
return c
2222

2323
# Code for merge sort
24-
def sort(x):
24+
def sort(List):
2525
""" Function to sort an array using merge sort algorithm """
26-
if len(x) == 0 or len(x) == 1:
27-
return x
26+
if len(List) == 0 or len(List) == 1:
27+
return List
2828
else:
2929
middle = len(x)//2
30-
a = sort(x[:middle])
31-
b = sort(x[middle:])
30+
a = sort(List[:middle])
31+
b = sort(List[middle:])
3232
return merge(a,b)
3333

3434
# time complexities

pygorithm/sorting/quick_sort.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
# Best = Average = O(nlog(n)), Worst = O(n ^ 2)
55

66
# quick_sort algorithm
7-
def sort(arr):
8-
if len(arr) <= 1:
9-
return arr
10-
pivot = arr[len(arr) // 2]
11-
left = [x for x in arr if x < pivot]
12-
middle = [x for x in arr if x == pivot]
13-
right = [x for x in arr if x > pivot]
7+
def sort(List):
8+
if len(List) <= 1:
9+
return List
10+
pivot = List[len(List) // 2]
11+
left = [x for x in List if x < pivot]
12+
middle = [x for x in List if x == pivot]
13+
right = [x for x in List if x > pivot]
1414
return sort(left) + middle + sort(right)
1515

1616
# time complexities

pygorithm/sorting/shell_sort.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44
# Best Case O(n logn); Average Case O(depends on gap sequence); Worst Case O(n^2)
55

66
# shell sort algorithm
7-
def sort(myList):
8-
gap = len(myList) // 2
7+
def sort(List):
8+
gap = len(List) // 2
99
while gap > 0:
10-
for i in range(gap, len(myList)):
11-
currentItem = myList[i]
10+
for i in range(gap, len(List)):
11+
currentItem = List[i]
1212
j = i
13-
while j >= gap and myList[j - gap] > currentItem:
14-
myList[j] = myList[j - gap]
13+
while j >= gap and List[j - gap] > currentItem:
14+
List[j] = List[j - gap]
1515
j -= gap
16-
myList[j] = currentItem
16+
List[j] = currentItem
1717
gap //= 2
1818

19-
return myList
19+
return List
2020

2121
# time complexities
2222
def time_complexities():

0 commit comments

Comments
 (0)