Update binary_insertion_sort.cpp

This commit is contained in:
Siddhartha Shankar Padhy
2021-12-07 18:58:41 +05:30
committed by GitHub
parent 772f0b4d07
commit d48126b70d

View File

@@ -61,15 +61,22 @@ template<class T>
int64_t binary_search(std::vector<T> &arr,T val,int64_t low,int64_t high) int64_t binary_search(std::vector<T> &arr,T val,int64_t low,int64_t high)
{ {
if (high <= low) if (high <= low)
{
return (val > arr[low]) ? (low + 1) : low; return (val > arr[low]) ? (low + 1) : low;
}
int64_t mid = low + (high-low)/2; int64_t mid = low + (high-low)/2;
if(arr[mid]>val) if(arr[mid]>val)
{
return binary_search(arr,val,low,mid-1); return binary_search(arr,val,low,mid-1);
}
else if(arr[mid]<val) else if(arr[mid]<val)
{
return binary_search(arr,val,mid+1,high); return binary_search(arr,val,mid+1,high);
}
else else
{
return mid+1; return mid+1;
}
} }
/** /**
@@ -104,7 +111,7 @@ static void test() {
/* 1st test: /* 1st test:
[5, -3, -1, -2, 7] returns [-3, -2, -1, 5, 7] */ [5, -3, -1, -2, 7] returns [-3, -2, -1, 5, 7] */
std::vector<int64_t> arr1({5, -3, -1, -2, 7}); std::vector<int64_t> arr1({5, -3, -1, -2, 7});
std::cout << "Test 1... "; std::cout << "1st test... ";
sorting::insertionSort_binsrch(arr1); sorting::insertionSort_binsrch(arr1);
assert(std::is_sorted(std::begin(arr1), std::end(arr1))); assert(std::is_sorted(std::begin(arr1), std::end(arr1)));
std::cout << "passed" << std::endl; std::cout << "passed" << std::endl;
@@ -112,7 +119,7 @@ static void test() {
/* 2nd test: /* 2nd test:
[12, 26, 15, 91, 32, 54, 41] returns [12, 15, 26, 32, 41, 54, 91] */ [12, 26, 15, 91, 32, 54, 41] returns [12, 15, 26, 32, 41, 54, 91] */
std::vector<int64_t> arr2({12, 26, 15, 91, 32, 54, 41}); std::vector<int64_t> arr2({12, 26, 15, 91, 32, 54, 41});
std::cout << "Test 2... "; std::cout << "2nd test... ";
sorting::insertionSort_binsrch(arr2); sorting::insertionSort_binsrch(arr2);
assert(std::is_sorted(std::begin(arr2), std::end(arr2))); assert(std::is_sorted(std::begin(arr2), std::end(arr2)));
std::cout << "passed" << std::endl; std::cout << "passed" << std::endl;
@@ -120,7 +127,7 @@ static void test() {
/* 3rd test: /* 3rd test:
[7.1, -2.5, -4.0, -2.1, 5.7] returns [-4.0, -2.5, -2.1, 5.7, 7.1] */ [7.1, -2.5, -4.0, -2.1, 5.7] returns [-4.0, -2.5, -2.1, 5.7, 7.1] */
std::vector<float> arr3({7.1, -2.5, -4.0, -2.1, 5.7}); std::vector<float> arr3({7.1, -2.5, -4.0, -2.1, 5.7});
std::cout << "Test 3... "; std::cout << "3rd test... ";
sorting::insertionSort_binsrch(arr3); sorting::insertionSort_binsrch(arr3);
assert(std::is_sorted(std::begin(arr3), std::end(arr3))); assert(std::is_sorted(std::begin(arr3), std::end(arr3)));
std::cout << "passed" << std::endl; std::cout << "passed" << std::endl;
@@ -128,7 +135,7 @@ static void test() {
/* 4th test: /* 4th test:
[12.8, -3.7, -20.7, -7.1, 2.2] returns [-20.7, -7.1, -3.7, 2.2, 12.8] */ [12.8, -3.7, -20.7, -7.1, 2.2] returns [-20.7, -7.1, -3.7, 2.2, 12.8] */
std::vector<float> arr4({12.8, -3.7, -20.7, -7.1, 2.2}); std::vector<float> arr4({12.8, -3.7, -20.7, -7.1, 2.2});
std::cout << "Test 4... "; std::cout << "4th test... ";
sorting::insertionSort_binsrch(arr4); sorting::insertionSort_binsrch(arr4);
assert(std::is_sorted(std::begin(arr4), std::end(arr4))); assert(std::is_sorted(std::begin(arr4), std::end(arr4)));
std::cout << "passed" << std::endl; std::cout << "passed" << std::endl;