fork download
  1.  
  2. // This code is in C++11
  3.  
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <functional>
  7. #include <memory>
  8. #include <climits>
  9. #include <iterator>
  10.  
  11. using namespace std;
  12.  
  13. // :), generic functor
  14. template <typename ArgType, typename ResultType>
  15. struct PredUniqueElements : public std::unary_function<ArgType, ResultType> {
  16.  
  17. public:
  18. PredUniqueElements(ArgType ref) : prev(ref) {}
  19.  
  20. ResultType operator() (ArgType curr) {
  21. ArgType old = prev;
  22. prev = curr;
  23. return curr != old;
  24. }
  25.  
  26. private:
  27. ArgType prev;
  28. };
  29.  
  30. int GenerateDataSet(int A[], int size) {
  31. for(int i = 0; i < size; i++)
  32. A[i] = rand()%size;
  33.  
  34. sort(A, A+size);
  35.  
  36. // predicate funtor definition
  37. PredUniqueElements<int, bool> pred(INT_MIN);
  38.  
  39. // Retain only unique elements
  40. auto it = copy_if (A, A+size, A, pred);
  41. return distance(A, it);
  42. }
  43.  
  44. // Returns location of key, or -1 if not found
  45. int BinarySearchSimple(int A[], int l, int r, int key, int &count)
  46. {
  47. int m;
  48.  
  49. while( l <= r )
  50. {
  51. m = l + (r-l)/2;
  52.  
  53. count++;
  54. if( A[m] == key ) // first comparison
  55. return m;
  56.  
  57. count++;
  58. if( A[m] < key ) // second comparison
  59. l = m + 1;
  60. else
  61. r = m - 1;
  62. }
  63.  
  64. return -1;
  65. }
  66.  
  67. int BinarySearch(int A[], int l, int r, int key, int &count)
  68. {
  69. int m;
  70.  
  71. while( r - l > 1 )
  72. {
  73. m = l + (r-l)/2;
  74.  
  75. count++;
  76. if( A[m] <= key )
  77. l = m;
  78. else
  79. r = m;
  80. }
  81.  
  82. count++;
  83. if( A[l] == key )
  84. return l;
  85. else
  86. return -1;
  87. }
  88.  
  89. void TestCase_01() {
  90. int size = 2048;
  91. auto_ptr<int> buf(new int[size]);
  92. int *A = buf.get();
  93.  
  94. size = GenerateDataSet(A, size);
  95.  
  96. copy(A, A+size, ostream_iterator<int>(cout, " ")), cout << endl;
  97.  
  98. int key = 193;
  99. int count;
  100.  
  101. // We are only interested in comparison count
  102. count = 0;
  103. BinarySearchSimple(A, 0, size-1, key, count);
  104. cout << "BinarySearchSimple " << count << endl;
  105.  
  106. count = 0;
  107. BinarySearch(A, 0, size-1, key, count);
  108. cout << "BinarySearch " << count << endl;
  109. }
  110.  
  111. int main() {
  112. srand(unsigned(time(NULL)));
  113.  
  114. TestCase_01();
  115.  
  116. return 0;
  117. }
  118.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
0 1 3 5 6 7 10 11 12 13 14 15 16 17 18 19 20 23 26 29 30 32 33 34 36 38 40 42 44 45 47 48 49 51 52 53 54 55 56 58 59 61 62 63 64 65 67 68 69 70 73 74 75 76 77 78 79 80 81 82 83 84 85 86 88 89 92 93 94 96 99 100 102 103 104 105 106 107 108 109 111 112 113 114 115 117 118 120 122 123 124 125 127 128 129 130 131 135 137 138 139 140 141 144 145 147 150 151 152 153 154 156 157 161 162 164 166 167 168 169 171 175 181 182 183 184 186 187 188 189 190 191 192 195 196 197 198 199 200 202 203 204 205 206 207 208 209 210 212 213 214 215 218 219 220 221 222 224 225 226 236 239 240 242 244 245 247 248 249 250 252 253 254 255 257 258 259 263 266 267 268 270 271 272 273 274 275 276 277 278 279 280 281 282 283 285 288 290 291 292 293 295 296 298 300 302 307 308 310 311 312 313 314 315 316 317 318 320 323 324 325 326 328 331 332 334 336 337 338 340 341 343 344 345 347 348 350 351 354 357 358 359 360 361 363 364 365 367 368 369 374 377 379 380 381 383 384 385 387 388 389 391 392 393 394 397 398 399 400 402 403 406 407 410 411 412 413 414 419 420 423 424 425 426 427 429 433 434 436 437 438 439 442 445 446 447 450 454 456 457 460 463 465 467 468 469 470 471 473 475 476 478 484 489 491 493 494 497 499 500 501 502 503 507 508 511 513 514 515 516 518 520 522 525 526 528 529 531 533 534 535 536 539 542 543 544 545 546 548 551 552 553 557 558 559 563 565 567 568 571 572 573 575 576 577 579 580 581 582 585 586 587 588 589 590 592 593 594 595 596 598 599 600 601 602 605 607 608 609 613 614 615 616 618 619 620 621 623 624 625 626 627 628 630 631 632 634 635 639 641 642 644 646 647 648 651 652 655 658 659 660 661 663 665 667 669 671 672 675 676 677 678 679 680 681 682 684 685 686 687 688 689 690 691 692 693 695 700 704 705 706 707 708 709 710 711 714 716 717 719 721 722 725 726 729 730 731 732 735 736 737 738 742 743 744 745 747 750 751 752 754 758 759 760 761 763 765 766 767 768 770 771 772 773 775 776 777 779 781 782 783 784 787 790 791 792 794 795 798 799 800 802 803 804 805 807 809 811 812 813 815 816 817 819 821 824 827 828 829 830 832 833 834 835 836 837 839 840 841 842 843 844 845 846 848 849 850 851 852 856 857 861 862 866 867 869 870 871 872 875 877 878 880 883 884 885 886 889 890 891 892 893 894 895 896 898 900 901 902 903 904 905 908 909 910 913 914 915 916 919 920 921 922 926 927 928 929 931 932 933 934 935 936 937 938 939 941 942 943 945 946 947 948 949 950 951 952 953 954 955 956 957 959 960 962 963 964 965 968 969 971 972 973 974 978 979 981 982 983 984 985 987 988 991 995 996 997 999 1001 1002 1005 1006 1009 1010 1014 1016 1017 1018 1020 1021 1022 1023 1026 1027 1028 1029 1031 1033 1034 1037 1038 1040 1042 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1059 1060 1061 1065 1068 1069 1070 1071 1072 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1086 1087 1088 1089 1090 1093 1094 1095 1096 1098 1099 1100 1101 1102 1103 1106 1107 1108 1109 1112 1115 1116 1117 1118 1119 1120 1121 1122 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1137 1138 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1152 1154 1155 1156 1159 1160 1162 1163 1166 1167 1171 1172 1173 1175 1176 1177 1179 1180 1181 1182 1183 1186 1191 1195 1196 1197 1198 1199 1200 1203 1205 1206 1208 1213 1214 1216 1219 1227 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1244 1245 1246 1249 1250 1251 1252 1253 1255 1256 1257 1259 1260 1261 1262 1263 1264 1266 1268 1271 1275 1276 1277 1278 1280 1281 1282 1284 1286 1287 1289 1290 1291 1292 1294 1295 1296 1300 1301 1302 1303 1304 1305 1306 1309 1311 1312 1314 1315 1316 1321 1323 1326 1331 1333 1334 1335 1336 1337 1339 1341 1343 1344 1346 1347 1349 1350 1352 1353 1354 1355 1357 1358 1359 1360 1361 1362 1363 1365 1369 1371 1374 1375 1377 1378 1381 1382 1384 1385 1386 1387 1388 1389 1391 1392 1393 1394 1396 1397 1401 1402 1403 1404 1405 1408 1410 1411 1413 1414 1415 1416 1417 1418 1419 1420 1422 1424 1425 1426 1428 1429 1432 1434 1435 1438 1439 1440 1441 1443 1444 1446 1447 1448 1449 1450 1451 1453 1455 1457 1459 1461 1462 1463 1465 1466 1467 1468 1469 1472 1473 1474 1475 1476 1478 1480 1481 1482 1483 1484 1485 1487 1488 1489 1495 1497 1499 1501 1503 1505 1507 1509 1510 1511 1515 1516 1517 1518 1519 1520 1523 1524 1527 1528 1529 1531 1532 1533 1534 1535 1536 1537 1538 1542 1543 1546 1547 1549 1551 1552 1555 1557 1558 1559 1561 1565 1566 1567 1568 1570 1571 1573 1574 1575 1577 1578 1579 1580 1582 1584 1585 1587 1588 1589 1591 1595 1598 1600 1601 1602 1603 1604 1606 1607 1610 1612 1613 1614 1615 1616 1617 1618 1619 1622 1623 1624 1625 1626 1629 1630 1632 1633 1634 1636 1638 1639 1640 1643 1645 1646 1647 1648 1650 1651 1652 1653 1655 1658 1659 1662 1663 1665 1666 1667 1668 1669 1672 1673 1675 1676 1677 1679 1681 1683 1686 1687 1688 1689 1693 1694 1698 1700 1703 1704 1706 1708 1709 1710 1711 1712 1713 1714 1715 1719 1721 1722 1725 1727 1729 1730 1731 1732 1733 1734 1735 1737 1738 1741 1742 1744 1746 1747 1748 1751 1752 1753 1755 1756 1757 1758 1759 1760 1761 1764 1765 1766 1767 1769 1774 1776 1778 1782 1786 1787 1788 1789 1791 1793 1794 1796 1798 1799 1800 1801 1802 1803 1804 1805 1806 1808 1810 1811 1812 1813 1818 1820 1822 1823 1827 1828 1830 1831 1833 1835 1836 1837 1838 1839 1841 1843 1844 1845 1846 1848 1851 1852 1853 1854 1856 1857 1858 1859 1861 1863 1864 1865 1866 1867 1868 1870 1873 1874 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1903 1904 1906 1907 1908 1911 1912 1913 1914 1915 1918 1919 1920 1921 1922 1924 1925 1926 1927 1930 1931 1932 1935 1936 1937 1938 1939 1940 1942 1943 1947 1952 1955 1956 1961 1962 1968 1969 1970 1972 1973 1979 1980 1981 1982 1986 1987 1988 1989 1990 1991 1993 1995 1998 2003 2006 2007 2008 2009 2011 2016 2018 2019 2020 2021 2022 2024 2025 2026 2027 2028 2030 2032 2033 2034 2035 2038 2039 2042 2043 2046 
BinarySearchSimple 20
BinarySearch 12