Skip to content

Commit

Permalink
Merge from dev -> dev.
Browse files Browse the repository at this point in the history
  • Loading branch information
Certseeds committed Aug 5, 2020
1 parent 027729c commit bb9737f
Show file tree
Hide file tree
Showing 10 changed files with 347 additions and 63 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: 0.0.6.4
release_name: 0.0.6.4
tag_name: 0.0.7
release_name: 0.0.7
draft: false
prerelease: false

Expand Down
1 change: 1 addition & 0 deletions .idea/fileTemplates/internal/C Header File.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/fileTemplates/internal/C Source File.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/fileTemplates/internal/C++ Class.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions algorithm/binary_search/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ project(basic_algorithms_binary_search LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
add_executable(${PROJECT_NAME}
./basic.cpp
)
#./basic.cpp
./triple_search.cpp
#rotate_array.cpp
)
72 changes: 53 additions & 19 deletions algorithm/binary_search/basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@
*/

#include <algorithm>
#include <iostream>
#include <tuple>
#include <vector>

#include "catch_main.hpp"

using Catch::Matchers::Contains;
using Catch::Matchers::Equals;
using Catch::Matchers::UnorderedEquals;
using std::cout;
using std::endl;
using std::tie;
using std::tuple;
using std::vector;

using std::vector;

// !IMPORTANT [first,last)!!
// nums升序,寻找[first,last)内,第一个不小于value的值(x>=value的第一个)
int lower_bound(vector<int32_t> nums, int32_t first, int32_t last, int value) {
int lower_bound(vector<int32_t> &nums, int32_t first, int32_t last, int value) {
while (first < last) {
int middle = first + (last - first) / 2;
if (nums[middle] < value) {
Expand All @@ -35,20 +35,27 @@ int lower_bound(vector<int32_t> nums, int32_t first, int32_t last, int value) {
}
return first;
}

TEST_CASE("lower_bound", "[test basic binary search]") {
vector<int32_t> vec{1, 1, 2, 3, 4, 4, 4, 5, 5, 6};
vector<tuple<int32_t, int32_t>> input_result = {
{1, 0}, {2, 2}, {3, 3}, {4, 4}, {5, 7}, {6, 9}};
for (const auto& i : input_result) {
{1, 0},
{2, 2},
{3, 3},
{4, 4},
{5, 7},
{6, 9}};
for (const auto &i : input_result) {
int input{0};
int output{0};
tie(input, output) = i;
CHECK(output == lower_bound(vec, 0, vec.size(), input));
}
}

// nums升序,寻找[first,last)内,任意等于value的值.
// 任意
int any_equal(vector<int32_t> nums, int32_t first, int32_t last, int value) {
// 任意!
int any_equal(vector<int32_t> &nums, int32_t first, int32_t last, int value) {
while (first < last) {
int middle = first + (last - first) / 2;
if (nums[middle] == value) {
Expand All @@ -61,12 +68,18 @@ int any_equal(vector<int32_t> nums, int32_t first, int32_t last, int value) {
}
return first;
}

TEST_CASE("any_equal", "[test basic binary search]") {
vector<int32_t> vec{1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6};
vector<tuple<int32_t, int32_t, int32_t>> input_result = {
{1, 0, 1}, {2, 1, 3}, {3, 3, 6}, {4, 6, 10}, {5, 10, 15}, {6, 15, 16}};
{1, 0, 1},
{2, 1, 3},
{3, 3, 6},
{4, 6, 10},
{5, 10, 15},
{6, 15, 16}};
// 0 2 4 8 12 15
for (const auto& i : input_result) {
for (const auto &i : input_result) {
int32_t input{0};
int32_t left{0};
int32_t right{0};
Expand All @@ -76,8 +89,9 @@ TEST_CASE("any_equal", "[test basic binary search]") {
CHECK(result < right);
}
}

// nums升序,寻找[first,last)内,第一个大于value的值(x>value的第一个)
int upper_bound(vector<int32_t> nums, int32_t first, int32_t last, int value) {
int upper_bound(vector<int32_t> &nums, int32_t first, int32_t last, int value) {
while (first < last) {
int middle = first + (last - first) / 2;
if (!(nums[middle] > value)) {
Expand All @@ -88,41 +102,61 @@ int upper_bound(vector<int32_t> nums, int32_t first, int32_t last, int value) {
}
return first;
}

TEST_CASE("upper_bound", "[test basic binary search]") {
vector<int32_t> vec{1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6};
vector<tuple<int32_t, int32_t>> input_result = {
{1, 1}, {2, 3}, {3, 6}, {4, 10}, {5, 15}, {6, 16}};
for (const auto& i : input_result) {
{1, 1},
{2, 3},
{3, 6},
{4, 10},
{5, 15},
{6, 16}};
for (const auto &i : input_result) {
int input{0};
int output{0};
tie(input, output) = i;
CHECK(output == upper_bound(vec, 0, vec.size(), input));
}
}

// 升序,【first,last)内,最后一个小于value的值(x<value的最大值)
int lower_bound_warpper(vector<int32_t> nums, int32_t first, int32_t last, int value) {
int lower_bound_warpper(vector<int32_t> &nums, int32_t first, int32_t last, int value) {
return lower_bound(nums, first, last, value) - 1;
}

TEST_CASE("lower_bound_warpper", "[test basic binary search]") {
vector<int32_t> vec{1, 1, 2, 3, 4, 4, 4, 5, 5, 6};
vector<tuple<int32_t, int32_t>> input_result = {
{1, -1}, {2, 1}, {3, 2}, {4, 3}, {5, 6}, {6, 8}};
for (const auto& i : input_result) {
{1, -1},
{2, 1},
{3, 2},
{4, 3},
{5, 6},
{6, 8}};
for (const auto &i : input_result) {
int input{0};
int output{0};
tie(input, output) = i;
CHECK(output == lower_bound_warpper(vec, 0, vec.size(), input));
}
}

//升序,【first,last)内,最后一个小于等于value的值,(x<=value 最大值)
int upper_bound_warpper(vector<int32_t> nums, int32_t first, int32_t last, int value) {
int upper_bound_warpper(vector<int32_t> &nums, int32_t first, int32_t last, int value) {
return upper_bound(nums, first, last, value) - 1;
}

TEST_CASE("upper_bound_warpper", "[test basic binary search]") {
vector<int32_t> vec{1, 1, 2, 3, 4, 4, 4, 5, 5, 6};
vector<tuple<int32_t, int32_t>> input_result = {
{1, 1}, {2, 2}, {3, 3}, {4, 6}, {5, 8}, {6, 9}};
for (const auto& i : input_result) {
{1, 1},
{2, 2},
{3, 3},
{4, 6},
{5, 8},
{6, 9}};
for (const auto &i : input_result) {
int input{0};
int output{0};
tie(input, output) = i;
Expand Down
102 changes: 102 additions & 0 deletions algorithm/binary_search/rotate_array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* @Github: https://github.com/Certseeds/CS203_DSAA_template
* @Organization: SUSTech
* @Author: nanoseeds
* @Date: 2020-08-01 17:39:45
* @LastEditors: nanoseeds
* @LICENSE: MIT
*/
/*
MIT License
CS203_DSAA_template
Copyright (C) 2020 nanoseeds
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// refer https://leetcode-cn.com/problems/rotate-array/
// 189
#include <algorithm>
#include <vector>

#include "catch_main.hpp"

using Catch::Matchers::Contains;
using Catch::Matchers::Equals;
using Catch::Matchers::UnorderedEquals;
using std::tie;
using std::tuple;
using std::vector;

// nums 一个有序数组, 集体右移k个位置(k>=0)
// [first,last] !!
int rorate_array_min(vector<int32_t> &nums, int32_t first, int32_t last) {
while (first < last) {
int middle = (last - first) / 2 + first;
if (nums[middle] > nums[last]) {
first = middle + 1;
} else if (nums[middle] < nums[last]) {
last = middle;
} else {
last--;
}
}
return nums[first];
}

TEST_CASE("test case 1", "[test binary search - rotate_array_min]") {
vector<int32_t> vec{3, 4, 5, 1, 2};
CHECK(*std::min_element(std::begin(vec), std::end(vec)) == rorate_array_min(vec, 0, vec.size() - 1));
}

TEST_CASE("test case 2", "[test binary search - rotate_array_min]") {
vector<int32_t> vec{2, 2, 2, 0, 1};
CHECK(*std::min_element(std::begin(vec), std::end(vec)) == rorate_array_min(vec, 0, vec.size() - 1));
}

TEST_CASE("test case 3", "[test binary search - rotate_array_min]") {
vector<int32_t> vec{1, 3, 5};
CHECK(*std::min_element(std::begin(vec), std::end(vec)) == rorate_array_min(vec, 0, vec.size() - 1));
}

TEST_CASE("test case 4", "[test binary search - rotate_array_min]") {
vector<int32_t> vec{3, 3, 1, 3};
CHECK(*std::min_element(std::begin(vec), std::end(vec)) == rorate_array_min(vec, 0, vec.size() - 1));
}

TEST_CASE("test case 5", "[test binary search - rotate_array_min]") {
vector<int32_t> vec{3, 1};
CHECK(*std::min_element(std::begin(vec), std::end(vec)) == rorate_array_min(vec, 0, vec.size() - 1));
}

TEST_CASE("test case 6", "[test binary search - rotate_array_min]") {
vector<int32_t> vec{3, 1, 3};
CHECK(*std::min_element(std::begin(vec), std::end(vec)) == rorate_array_min(vec, 0, vec.size() - 1));
}

TEST_CASE("test case 7", "[test binary search - rotate_array_min]") {
vector<int32_t> vec{5, 6, 7, 1, 2, 3, 4};
CHECK(*std::min_element(std::begin(vec), std::end(vec)) == rorate_array_min(vec, 0, vec.size() - 1));
}

TEST_CASE("test case 8", "[test binary search - rotate_array_min]") {
vector<int32_t> vec{-1, -100, 3, 99};
CHECK(*std::min_element(std::begin(vec), std::end(vec)) == rorate_array_min(vec, 0, vec.size() - 1));
}
Loading

0 comments on commit bb9737f

Please sign in to comment.