File tree 1 file changed +5
-11
lines changed
1 file changed +5
-11
lines changed Original file line number Diff line number Diff line change 5
5
https://en.wikipedia.org/wiki/Insertion_sort#Variants
6
6
"""
7
7
8
+ from bisect import bisect
9
+
8
10
9
11
def binary_insertion_sort (collection : list ) -> list :
10
12
"""
@@ -36,18 +38,10 @@ def binary_insertion_sort(collection: list) -> list:
36
38
n = len (collection )
37
39
for i in range (1 , n ):
38
40
value_to_insert = collection [i ]
39
- low = 0
40
- high = i - 1
41
-
42
- while low <= high :
43
- mid = (low + high ) // 2
44
- if value_to_insert < collection [mid ]:
45
- high = mid - 1
46
- else :
47
- low = mid + 1
48
- for j in range (i , low , - 1 ):
41
+ insertion_pos = bisect (collection [:i ], value_to_insert )
42
+ for j in range (i , insertion_pos , - 1 ):
49
43
collection [j ] = collection [j - 1 ]
50
- collection [low ] = value_to_insert
44
+ collection [insertion_pos ] = value_to_insert
51
45
return collection
52
46
53
47
You can’t perform that action at this time.
0 commit comments