Skip to content

Commit

Permalink
分治思想下的递归二分查找兼泛型sort算法
Browse files Browse the repository at this point in the history
  • Loading branch information
iguoya committed Oct 7, 2020
1 parent 858a96e commit 4c5a2ff
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
6 changes: 6 additions & 0 deletions 23-divide_and_conconquer/binary_search/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.17)
project(binary_search)

set(CMAKE_CXX_STANDARD 14)

add_executable(binary_search main.cpp)
69 changes: 69 additions & 0 deletions 23-divide_and_conconquer/binary_search/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <iostream>
#include <vector>
#include <random>
#include <algorithm>

#include <iomanip>

using namespace std;


template <typename T>
int binary_search(const vector<T> &sample,const T item, int begin, int end) {

if(begin > end) {
return -1;
} else {
int middle_key = begin+(end-begin)/2;
if(sample[middle_key] == item) {
return middle_key;
} else if(sample[middle_key] > item) {
return binary_search(sample, item, begin, middle_key-1);
} else {
return binary_search(sample, item, middle_key+1, end);
}
}
}

template <typename T>
void show(const T &sample) {
for(int i = 1; i <= 100; ++i) {
cout<<setw(8)<<sample[i-1];
if(i%10 == 0) {
cout<<endl;
}
}
cout<<endl;
}
int main() {
std::cout << "Hello, World!" << std::endl;
default_random_engine e(0);
uniform_int_distribution<int> u(1, 10000);
vector<int> sample;
for(int i = 0; i< 100; ++i) {
sample.push_back(u(e));
}
show(sample);


sort(sample.begin(), sample.end(), greater<int>());
show(sample);

sort(sample.begin(), sample.end());
show(sample);




cout<<binary_search(sample, 4940, 0, sample.size())<<endl;
cout<<binary_search(sample, 7534, 0, sample.size())<<endl;
cout<<binary_search(sample, 9999, 0, sample.size())<<endl;
//
// cout<< setw(4) <<123 <<1234<<endl;
// cout << 1 <<setw(7) << 1234 << endl;
// cout << 1 << setw(7) << 1234567891234 << endl;
// cout << 1 << 1234 << setw(7) << endl;


return 0;
}

0 comments on commit 4c5a2ff

Please sign in to comment.