Skip to content

Commit e1366ff

Browse files
committed
Merge branch 'master' of https://github.com/TheAlgorithms/Python
local merge
2 parents f5c73a6 + 5a14f2c commit e1366ff

File tree

5 files changed

+19
-24
lines changed

5 files changed

+19
-24
lines changed

searches/linear_search.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414

1515
def linear_search(sequence, target):
16-
"""Pure implementation of binary search algorithm in Python
16+
"""Pure implementation of linear search algorithm in Python
1717
1818
:param sequence: some sorted collection with comparable items
1919
:param target: item value to search

sorts/bubble_sort.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def bubble_sort(collection):
3030
[-45, -5, -2]
3131
"""
3232
length = len(collection)
33-
for i in range(length):
33+
for i in range(length-1):
3434
for j in range(length-1):
3535
if collection[j] > collection[j+1]:
3636
collection[j], collection[j+1] = collection[j+1], collection[j]

sorts/heap_sort.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
python3 -m doctest -v heap_sort.py
88
99
For manual testing run:
10-
python insertion_sort.py
10+
python heap_sort.py
1111
'''
1212

1313
from __future__ import print_function
@@ -46,7 +46,7 @@ def heap_sort(unsorted):
4646
n = len(unsorted)
4747
for i in range(n//2 - 1, -1, -1):
4848
heapify(unsorted, i, n)
49-
for i in range(n - 1, -1, -1):
49+
for i in range(n - 1, 0, -1):
5050
unsorted[0], unsorted[i] = unsorted[i], unsorted[0]
5151
heapify(unsorted, 0, i)
5252
return unsorted

sorts/insertion_sort.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,10 @@ def insertion_sort(collection):
2929
>>> insertion_sort([-2, -5, -45])
3030
[-45, -5, -2]
3131
"""
32-
length = len(collection)
33-
for i in range(length):
34-
current_item = collection[i]
35-
j = i - 1
36-
while j >= 0 and current_item < collection[j]:
37-
collection[j+1] = collection[j]
38-
j -= 1
39-
collection[j+1] = current_item
32+
for index in range(1, len(collection)):
33+
while 0 < index and collection[index] < collection[index-1]:
34+
collection[index], collection[index-1] = collection[index-1], collection[index]
35+
index -= 1
4036

4137
return collection
4238

sorts/quick_sort.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,20 @@ def quick_sort(collection):
3535
>>> quick_sort([-2, -5, -45])
3636
[-45, -5, -2]
3737
"""
38+
if len(collection) <= 1:
39+
return collection
3840
less = []
3941
equal = []
4042
greater = []
41-
if len(collection) > 1:
42-
pivot = collection[0]
43-
for x in collection:
44-
if x < pivot:
45-
less.append(x)
46-
if x == pivot:
47-
equal.append(x)
48-
if x > pivot:
49-
greater.append(x)
50-
return quick_sort(less) + equal + quick_sort(greater)
51-
else:
52-
return collection
43+
pivot = collection[0]
44+
for x in collection:
45+
if x < pivot:
46+
less.append(x)
47+
elif x == pivot:
48+
equal.append(x)
49+
else:
50+
greater.append(x)
51+
return quick_sort(less) + equal + quick_sort(greater)
5352

5453

5554
if __name__ == '__main__':

0 commit comments

Comments
 (0)