From d48126b70d8d33e751ac7bf3fc6d61562c0b770f Mon Sep 17 00:00:00 2001 From: Siddhartha Shankar Padhy <83332618+Siddhartha-Padhy@users.noreply.github.com> Date: Tue, 7 Dec 2021 18:58:41 +0530 Subject: [PATCH] Update binary_insertion_sort.cpp --- sorting/binary_insertion_sort.cpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/sorting/binary_insertion_sort.cpp b/sorting/binary_insertion_sort.cpp index 47ed06572..96b18669b 100644 --- a/sorting/binary_insertion_sort.cpp +++ b/sorting/binary_insertion_sort.cpp @@ -61,15 +61,22 @@ template int64_t binary_search(std::vector &arr,T val,int64_t low,int64_t high) { if (high <= low) + { return (val > arr[low]) ? (low + 1) : low; - + } int64_t mid = low + (high-low)/2; if(arr[mid]>val) + { return binary_search(arr,val,low,mid-1); + } else if(arr[mid] arr1({5, -3, -1, -2, 7}); - std::cout << "Test 1... "; + std::cout << "1st test... "; sorting::insertionSort_binsrch(arr1); assert(std::is_sorted(std::begin(arr1), std::end(arr1))); std::cout << "passed" << std::endl; @@ -112,7 +119,7 @@ static void test() { /* 2nd test: [12, 26, 15, 91, 32, 54, 41] returns [12, 15, 26, 32, 41, 54, 91] */ std::vector arr2({12, 26, 15, 91, 32, 54, 41}); - std::cout << "Test 2... "; + std::cout << "2nd test... "; sorting::insertionSort_binsrch(arr2); assert(std::is_sorted(std::begin(arr2), std::end(arr2))); std::cout << "passed" << std::endl; @@ -120,7 +127,7 @@ static void test() { /* 3rd test: [7.1, -2.5, -4.0, -2.1, 5.7] returns [-4.0, -2.5, -2.1, 5.7, 7.1] */ std::vector arr3({7.1, -2.5, -4.0, -2.1, 5.7}); - std::cout << "Test 3... "; + std::cout << "3rd test... "; sorting::insertionSort_binsrch(arr3); assert(std::is_sorted(std::begin(arr3), std::end(arr3))); std::cout << "passed" << std::endl; @@ -128,7 +135,7 @@ static void test() { /* 4th test: [12.8, -3.7, -20.7, -7.1, 2.2] returns [-20.7, -7.1, -3.7, 2.2, 12.8] */ std::vector arr4({12.8, -3.7, -20.7, -7.1, 2.2}); - std::cout << "Test 4... "; + std::cout << "4th test... "; sorting::insertionSort_binsrch(arr4); assert(std::is_sorted(std::begin(arr4), std::end(arr4))); std::cout << "passed" << std::endl;