mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-03 02:25:57 +08:00
Update binary_insertion_sort.cpp
This commit is contained in:
committed by
GitHub
parent
772f0b4d07
commit
d48126b70d
@@ -61,15 +61,22 @@ template<class T>
|
||||
int64_t binary_search(std::vector<T> &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]<val)
|
||||
{
|
||||
return binary_search(arr,val,mid+1,high);
|
||||
}
|
||||
else
|
||||
{
|
||||
return mid+1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -104,7 +111,7 @@ static void test() {
|
||||
/* 1st test:
|
||||
[5, -3, -1, -2, 7] returns [-3, -2, -1, 5, 7] */
|
||||
std::vector<int64_t> 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<int64_t> 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<float> 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<float> 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;
|
||||
|
||||
Reference in New Issue
Block a user