From 959218262d7289b0aa7610ca9d811b1f963ef73a Mon Sep 17 00:00:00 2001
From: Isidro Arias <isidroariass@hotmail.es>
Date: Fri, 28 Feb 2025 10:12:55 +0100
Subject: [PATCH 1/2] update module doc

---
 sorts/binary_insertion_sort.py | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)

diff --git a/sorts/binary_insertion_sort.py b/sorts/binary_insertion_sort.py
index 50653a99e7ce..2d66fae1920a 100644
--- a/sorts/binary_insertion_sort.py
+++ b/sorts/binary_insertion_sort.py
@@ -1,13 +1,8 @@
 """
-This is a pure Python implementation of the binary insertion sort algorithm
+A variant of insertion sort that may yield in better performance if the
+cost of comparisons exceeds the cost of swaps.
 
-For doctests run following command:
-python -m doctest -v binary_insertion_sort.py
-or
-python3 -m doctest -v binary_insertion_sort.py
-
-For manual testing run:
-python binary_insertion_sort.py
+https://en.wikipedia.org/wiki/Insertion_sort#Variants
 """
 
 

From 18699846b35f0fa0c35be28a9bbcb412771a6434 Mon Sep 17 00:00:00 2001
From: Isidro Arias <isidroariass@hotmail.es>
Date: Fri, 28 Feb 2025 10:18:17 +0100
Subject: [PATCH 2/2] don't implement binary search again

---
 sorts/binary_insertion_sort.py | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)

diff --git a/sorts/binary_insertion_sort.py b/sorts/binary_insertion_sort.py
index 2d66fae1920a..841f7478c366 100644
--- a/sorts/binary_insertion_sort.py
+++ b/sorts/binary_insertion_sort.py
@@ -5,6 +5,8 @@
 https://en.wikipedia.org/wiki/Insertion_sort#Variants
 """
 
+from bisect import bisect
+
 
 def binary_insertion_sort(collection: list) -> list:
     """
@@ -36,18 +38,10 @@ def binary_insertion_sort(collection: list) -> list:
     n = len(collection)
     for i in range(1, n):
         value_to_insert = collection[i]
-        low = 0
-        high = i - 1
-
-        while low <= high:
-            mid = (low + high) // 2
-            if value_to_insert < collection[mid]:
-                high = mid - 1
-            else:
-                low = mid + 1
-        for j in range(i, low, -1):
+        insertion_pos = bisect(collection[:i], value_to_insert)
+        for j in range(i, insertion_pos, -1):
             collection[j] = collection[j - 1]
-        collection[low] = value_to_insert
+        collection[insertion_pos] = value_to_insert
     return collection