Download code

Jump to: navigation, search

Back to Binary_search_(C_Plus_Plus)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

build.log

1 /tmp/litprog8913891/binary_search.cpp: In function 'int main(int, char**)':
2 /tmp/litprog8913891/binary_search.cpp:67:25: error: 'puts' was not declared in this scope
3 /tmp/litprog8913891/binary_search.cpp:75:25: error: 'puts' was not declared in this scope
4 /tmp/litprog8913891/binary_search.cpp:81:21: error: 'puts' was not declared in this scope


binary_search.cpp

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Binary_search_(C_Plus_Plus)?oldid=19019
14 */
15 
16 #include <algorithm>
17 #include <iostream>
18 #include <vector>
19 #include <cstdlib>  // atoi
20 
21 template< typename T, typename IterT, typename compare_less >
22 IterT generic_binary_search(IterT begin, IterT end, T target) {
23     IterT initial_end = end;
24     while (begin < end) {
25         IterT middle = begin + (end - begin - 1)/2;
26         if (compare_less(target, *middle))
27             end = middle;
28         else if (compare_less(*middle, target))
29             begin = middle + 1;
30         else
31             return middle;
32     }
33     return initial_end;
34 }
35 template< typename T, typename IterT >
36 IterT generic_binary_search(IterT begin, IterT end, T target) {
37     IterT initial_end = end;
38     while (begin < end) {
39         IterT middle = begin + (end - begin - 1)/2;
40         if (target < *middle)
41             end = middle;
42         else if (*middle < target)
43             begin = middle + 1;
44         else
45             return middle;
46     }
47     return initial_end;
48 }
49 
50 int main(int argc, char* argv[]) {
51     int num_elements = std::atoi(argv[1]);
52     int* a = new int[num_elements];
53     std::vector<int> v;
54     for(int i=0; i<num_elements; i++) {
55         do {
56             a[i] = rand() % num_elements;
57         } while(a[i] == num_elements/7);
58         v.push_back(a[i]);
59     }
60     std::sort(a, a + num_elements);
61     std::sort(v.begin(), v.end());
62 
63     {
64         int present_val = a[rand() % num_elements];
65         int* found_at = generic_binary_search(a, a + num_elements, present_val);
66         if (found_at == a + num_elements || *found_at != present_val) {
67             puts("ERROR");
68         }
69     }
70     {
71         int present_val = v[rand() % num_elements];
72         std::vector<int>::iterator found_at =
73             generic_binary_search(v.begin(), v.end(), present_val);
74         if (found_at == v.end() || *found_at != present_val) {
75             puts("ERROR");
76         }
77     }
78     if (generic_binary_search(a, a + num_elements, num_elements/7) != a + num_elements ||
79         generic_binary_search(v.begin(), v.end(),  num_elements/7) != v.end())
80     {
81         puts("ERROR");
82     }
83     return 0;
84 }


hijacker
hijacker
hijacker
hijacker