bool recursive_binary_search (int *array, int max, int min, int value) //O(1)?O(n)? { if (max < min) return false; int mid = (max + min) / 2; if (value == array[mid]) return true; if (value > array[mid]) return recursive_binary_search (array, max, mid+1, value); else return recursive_binary_search (array, mid-1, min, value); }
int recursive_binary_search(const std::vector<int>& vec, int begin, int end, const int key) { if (begin < end) { int mid = (begin + end) / 2; if (key == vec[mid]) { return mid; } else if (key < vec[mid]) { return recursive_binary_search(vec, begin, mid - 1, key); } return recursive_binary_search(vec, mid + 1, end, key); } if (begin == end && key == vec[begin]) { return begin; } return -1; }
bool recursive (int* array, int size, int value)//if not found,it always return 4. why? //O(log n) { if (NULL == array || size <= 0) return false; int min = 0; int max = size - 1; return recursive_binary_search (array, max, min, value); }
int main(int argc, char** argv) { if(argc != 2) { std::cerr << "Usage: " << argv[0] << " <filename>\n"; return 1; } std::vector<unsigned> vec; std::ofstream times_file{argv[1]}; std::mt19937_64 gen{std::random_device{}()}; while (vec.size() < config::max_elements) { { std::uniform_int_distribution<unsigned> dist{1, 4}; unsigned tmp = (vec.empty() ? 0 : vec.back()) + dist(gen); for(std::size_t i = 0; i < config::step; ++i) { tmp += dist(gen); vec.push_back(tmp); } } const auto elements = vec.size(); std::cout << "n=" << elements << std::endl; times_file << elements; // don't take the middle one, since all binary searches would be constant then: const auto value = elements > 2 ? vec.at(1 + elements/2) : vec.front(); // In order to prevent wrong meassurments due to cache-effects always switch between // a linear and a binary search. // // Also: the enforces are needed in order to prevent the compiler from completely // optimizing away the searches. { auto before = std::chrono::high_resolution_clock::now(); auto it = iterative_binary_search(vec.begin(), vec.end(), value); auto after = std::chrono::high_resolution_clock::now(); enforce(it != vec.end(), "iterative binary search"); times_file << " " << time_diff(before, after); } { auto before = std::chrono::high_resolution_clock::now(); auto it = linear_search(vec.begin(), vec.end(), value); auto after = std::chrono::high_resolution_clock::now(); enforce(it != vec.end(), "linear search"); times_file << " " << time_diff(before, after); } { auto before = std::chrono::high_resolution_clock::now(); auto it = recursive_binary_search(vec.begin(), vec.end(), value); auto after = std::chrono::high_resolution_clock::now(); enforce(it != vec.end(), "recursive binary search"); times_file << " " << time_diff(before, after); } { auto before = std::chrono::high_resolution_clock::now(); auto it = std::find(vec.begin(), vec.end(), value); auto after = std::chrono::high_resolution_clock::now(); enforce(it != vec.end(), "std::find"); times_file << " " << time_diff(before, after); } { auto before = std::chrono::high_resolution_clock::now(); auto it = std::lower_bound(vec.begin(), vec.end(), value); auto after = std::chrono::high_resolution_clock::now(); enforce(it != vec.end(), "std::lower_bound"); times_file << " " << time_diff(before, after); } times_file << '\n'; } }