File tree 5 files changed +19
-24
lines changed
5 files changed +19
-24
lines changed Original file line number Diff line number Diff line change 13
13
14
14
15
15
def linear_search (sequence , target ):
16
- """Pure implementation of binary search algorithm in Python
16
+ """Pure implementation of linear search algorithm in Python
17
17
18
18
:param sequence: some sorted collection with comparable items
19
19
:param target: item value to search
Original file line number Diff line number Diff line change @@ -30,7 +30,7 @@ def bubble_sort(collection):
30
30
[-45, -5, -2]
31
31
"""
32
32
length = len (collection )
33
- for i in range (length ):
33
+ for i in range (length - 1 ):
34
34
for j in range (length - 1 ):
35
35
if collection [j ] > collection [j + 1 ]:
36
36
collection [j ], collection [j + 1 ] = collection [j + 1 ], collection [j ]
Original file line number Diff line number Diff line change 7
7
python3 -m doctest -v heap_sort.py
8
8
9
9
For manual testing run:
10
- python insertion_sort .py
10
+ python heap_sort .py
11
11
'''
12
12
13
13
from __future__ import print_function
@@ -46,7 +46,7 @@ def heap_sort(unsorted):
46
46
n = len (unsorted )
47
47
for i in range (n // 2 - 1 , - 1 , - 1 ):
48
48
heapify (unsorted , i , n )
49
- for i in range (n - 1 , - 1 , - 1 ):
49
+ for i in range (n - 1 , 0 , - 1 ):
50
50
unsorted [0 ], unsorted [i ] = unsorted [i ], unsorted [0 ]
51
51
heapify (unsorted , 0 , i )
52
52
return unsorted
Original file line number Diff line number Diff line change @@ -29,14 +29,10 @@ def insertion_sort(collection):
29
29
>>> insertion_sort([-2, -5, -45])
30
30
[-45, -5, -2]
31
31
"""
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
40
36
41
37
return collection
42
38
Original file line number Diff line number Diff line change @@ -35,21 +35,20 @@ def quick_sort(collection):
35
35
>>> quick_sort([-2, -5, -45])
36
36
[-45, -5, -2]
37
37
"""
38
+ if len (collection ) <= 1 :
39
+ return collection
38
40
less = []
39
41
equal = []
40
42
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 )
53
52
54
53
55
54
if __name__ == '__main__' :
You can’t perform that action at this time.
0 commit comments