Skip to content

Commit dcb4ced

Browse files
committed
Merge branch 'master' of https://github.com/TheAlgorithms/Python
2 parents 6fde848 + 9f982a8 commit dcb4ced

File tree

3 files changed

+73
-27
lines changed

3 files changed

+73
-27
lines changed

sorts/bucket_sort.py

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,22 @@
1414
# Best Case O(n); Average Case O(n); Worst Case O(n)
1515

1616
DEFAULT_BUCKET_SIZE=5
17-
def bucket_sort(my_list,bucket_size=DEFAULT_BUCKET_SIZE):
18-
if(my_list==0):
19-
print("you don't have any elements in array!")
2017

18+
def bucket_sort(my_list, bucket_size=DEFAULT_BUCKET_SIZE):
19+
if len(my_list) == 0:
20+
raise Exception("Please add some elements in the array.")
2121

22-
min_value=min(my_list)
23-
max_value=max(my_list)
22+
min_value, max_value = (min(my_list), max(my_list))
23+
bucket_count = ((max_value - min_value) // bucket_size + 1)
24+
buckets = [[] for _ in range(int(bucket_count))]
2425

25-
bucket_count=(max_value-min_value)//bucket_size+1
26-
buckets=[]
27-
for i in range(bucket_count):
28-
buckets.append([])
2926
for i in range(len(my_list)):
30-
buckets[(my_list[i]-min_value)//bucket_size].append(my_list[i])
27+
buckets[int((my_list[i] - min_value) // bucket_size)].append(my_list[i])
3128

29+
return sorted([buckets[i][j] for i in range(len(buckets))
30+
for j in range(len(buckets[i]))])
3231

33-
sorted_array=[]
34-
for i in range(len(buckets)):
35-
buckets[i].sort()
36-
for j in range(len(buckets[i])):
37-
sorted_array.append(buckets[i][j])
38-
return sorted_array
39-
40-
41-
42-
43-
#test
44-
#best on python 3.7.3
45-
user_input =input('Enter numbers separated by a comma:').strip()
46-
unsorted =[int(item) for item in user_input.split(',')]
47-
print(bucket_sort(unsorted))
32+
if __name__ == "__main__":
33+
user_input = input('Enter numbers separated by a comma:').strip()
34+
unsorted = [float(n) for n in user_input.split(',') if len(user_input) > 0]
35+
print(bucket_sort(unsorted))

sorts/pigeon_sort.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'''
2+
This is an implementation of Pigeon Hole Sort.
3+
'''
4+
5+
from __future__ import print_function
6+
7+
def pigeon_sort(array):
8+
# Manually finds the minimum and maximum of the array.
9+
min = array[0]
10+
max = array[0]
11+
12+
for i in range(len(array)):
13+
if(array[i] < min): min = array[i]
14+
elif(array[i] > max): max = array[i]
15+
16+
# Compute the variables
17+
holes_range = max-min + 1
18+
holes = [0 for _ in range(holes_range)]
19+
holes_repeat = [0 for _ in range(holes_range)]
20+
21+
# Make the sorting.
22+
for i in range(len(array)):
23+
index = array[i] - min
24+
if(holes[index] != array[i]):
25+
holes[index] = array[i]
26+
holes_repeat[index] += 1
27+
else: holes_repeat[index] += 1
28+
29+
# Makes the array back by replacing the numbers.
30+
index = 0
31+
for i in range(holes_range):
32+
while(holes_repeat[i] > 0):
33+
array[index] = holes[i]
34+
index += 1
35+
holes_repeat[i] -= 1
36+
37+
# Returns the sorted array.
38+
return array
39+
40+
if __name__ == '__main__':
41+
try:
42+
raw_input # Python2
43+
except NameError:
44+
raw_input = input # Python 3
45+
46+
user_input = raw_input('Enter numbers separated by comma:\n')
47+
unsorted = [int(x) for x in user_input.split(',')]
48+
sorted = pigeon_sort(unsorted)
49+
50+
print(sorted)

sorts/quick_sort.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,16 @@ def quick_sort(collection):
3434
return collection
3535
else:
3636
pivot = collection[0]
37-
greater = [element for element in collection[1:] if element > pivot]
38-
lesser = [element for element in collection[1:] if element <= pivot]
37+
# Modify the list comprehensions to reduce the number of judgments, the speed has increased by more than 50%.
38+
greater = []
39+
lesser = []
40+
for element in collection[1:]:
41+
if element > pivot:
42+
greater.append(element)
43+
else:
44+
lesser.append(element)
45+
# greater = [element for element in collection[1:] if element > pivot]
46+
# lesser = [element for element in collection[1:] if element <= pivot]
3947
return quick_sort(lesser) + [pivot] + quick_sort(greater)
4048

4149

0 commit comments

Comments
 (0)