Assignment 2

Implement the search algorithm you came up with in pseudocode with Python
Test the search algorithm with a list of 10,100,1000 random numbers (sorted with your sorting algorithm) and compare the result using the %time to time your code and submit your results in code comments

In [15]:
def searchalgo(numberslist,number):
    print("The list is",numberslist)
    import time
    start_time = time.time()
    count=len(numberslist)
    for x in numberslist: 
        if x == number and count!=0:
            print("The number",number,"is present in the list.")
        else:
            count=count-1
            
        if count==0:
            print("We have searched the list. The number",str(number),"does not exist in this list.")
        
    print("--- %s seconds ---" % (time.time() - start_time))

In [16]:
searchalgo([1,2,3,4,5],10)


The list is [1, 2, 3, 4, 5]
We have searched the list. The number 10 does not exist in this list.
--- 0.0 seconds ---

In [17]:
import random
searchalgo(random.sample(range(10), 10),random.sample(range(10), 1))


The list is [2, 4, 8, 6, 1, 7, 9, 0, 5, 3]
We have searched the list. The number [4] does not exist in this list.
--- 0.0 seconds ---

In [19]:
searchalgo(random.sample(range(100), 100),random.sample(range(100), 1))


The list is [36, 60, 64, 84, 96, 82, 80, 92, 7, 75, 81, 50, 52, 15, 85, 29, 98, 88, 39, 2, 4, 5, 97, 20, 11, 0, 66, 53, 23, 55, 19, 42, 43, 25, 70, 28, 69, 76, 58, 6, 61, 56, 40, 79, 41, 26, 31, 44, 94, 46, 59, 35, 77, 1, 83, 34, 37, 21, 22, 38, 68, 91, 49, 57, 17, 14, 24, 30, 47, 67, 62, 16, 8, 72, 65, 45, 95, 13, 99, 63, 90, 93, 71, 51, 3, 54, 32, 74, 73, 33, 18, 78, 48, 89, 87, 10, 27, 12, 86, 9]
We have searched the list. The number [25] does not exist in this list.
--- 0.0 seconds ---

In [20]:
searchalgo(random.sample(range(1000), 1000),random.sample(range(1000), 1))


The list is [926, 807, 418, 783, 639, 262, 493, 284, 461, 446, 481, 125, 853, 290, 646, 10, 990, 323, 340, 425, 747, 681, 727, 522, 873, 21, 874, 452, 981, 187, 294, 602, 740, 868, 774, 231, 642, 215, 766, 188, 278, 265, 546, 802, 32, 96, 484, 759, 144, 901, 621, 892, 375, 885, 815, 661, 656, 519, 37, 282, 314, 402, 1, 852, 622, 437, 400, 186, 205, 738, 138, 620, 773, 664, 872, 961, 13, 124, 653, 907, 824, 71, 510, 563, 993, 670, 12, 754, 156, 232, 530, 536, 443, 552, 50, 673, 804, 951, 551, 235, 494, 391, 875, 707, 513, 267, 59, 112, 399, 515, 417, 113, 919, 963, 566, 336, 263, 488, 896, 736, 56, 641, 179, 491, 713, 949, 726, 654, 947, 521, 47, 988, 104, 801, 577, 942, 676, 538, 665, 444, 225, 227, 465, 758, 704, 571, 322, 590, 569, 785, 176, 279, 245, 863, 719, 537, 922, 950, 703, 271, 671, 73, 345, 449, 658, 748, 831, 389, 75, 763, 0, 199, 514, 584, 697, 682, 320, 891, 700, 395, 964, 408, 88, 126, 614, 192, 820, 420, 219, 462, 84, 413, 776, 364, 777, 871, 468, 239, 688, 487, 228, 778, 854, 534, 281, 588, 472, 198, 100, 526, 326, 70, 887, 319, 33, 189, 966, 220, 241, 923, 632, 248, 412, 941, 216, 983, 829, 662, 307, 791, 938, 908, 388, 832, 709, 731, 858, 351, 663, 153, 705, 643, 735, 647, 940, 325, 105, 250, 109, 591, 397, 102, 502, 948, 69, 977, 800, 185, 589, 221, 525, 324, 712, 975, 255, 42, 732, 527, 3, 44, 223, 331, 238, 151, 441, 846, 143, 61, 448, 909, 178, 878, 277, 996, 945, 68, 373, 398, 708, 347, 214, 541, 672, 310, 118, 757, 333, 34, 107, 471, 668, 619, 436, 841, 884, 611, 132, 193, 26, 677, 553, 301, 361, 823, 751, 623, 806, 140, 617, 403, 7, 710, 652, 638, 65, 480, 63, 483, 108, 266, 86, 272, 80, 139, 711, 337, 390, 540, 870, 916, 22, 469, 103, 308, 547, 106, 760, 423, 36, 910, 613, 383, 528, 72, 27, 261, 283, 94, 114, 911, 379, 152, 212, 696, 183, 203, 782, 464, 574, 973, 587, 585, 814, 224, 285, 482, 937, 730, 851, 119, 25, 898, 344, 739, 579, 689, 849, 603, 366, 386, 684, 549, 309, 159, 929, 148, 116, 767, 43, 918, 414, 866, 779, 376, 601, 788, 979, 924, 512, 657, 458, 150, 303, 306, 615, 618, 217, 358, 631, 957, 206, 888, 174, 992, 134, 55, 210, 542, 291, 913, 971, 350, 786, 834, 570, 181, 965, 367, 457, 505, 243, 737, 633, 694, 770, 691, 769, 24, 822, 431, 987, 244, 752, 984, 381, 634, 473, 810, 115, 78, 817, 341, 273, 976, 543, 649, 335, 635, 640, 296, 813, 52, 429, 844, 840, 349, 644, 377, 555, 715, 869, 532, 768, 208, 496, 404, 89, 54, 881, 952, 165, 680, 256, 222, 935, 793, 236, 716, 9, 837, 821, 847, 269, 789, 14, 270, 258, 479, 627, 230, 242, 610, 35, 825, 991, 545, 29, 110, 39, 667, 93, 455, 893, 659, 467, 317, 60, 191, 750, 725, 327, 299, 197, 121, 660, 879, 729, 531, 318, 572, 38, 917, 133, 906, 6, 931, 507, 816, 81, 53, 529, 292, 343, 771, 836, 286, 939, 201, 985, 565, 19, 260, 463, 954, 600, 87, 142, 97, 247, 435, 495, 184, 775, 877, 490, 862, 842, 280, 329, 989, 780, 833, 295, 137, 489, 674, 803, 90, 967, 355, 359, 592, 743, 297, 392, 645, 168, 466, 648, 51, 200, 415, 819, 339, 808, 595, 826, 166, 380, 838, 902, 792, 275, 702, 254, 855, 890, 506, 517, 378, 956, 889, 237, 509, 117, 734, 276, 679, 5, 897, 196, 520, 368, 550, 604, 753, 15, 556, 288, 573, 477, 195, 145, 346, 83, 360, 410, 687, 867, 447, 213, 535, 998, 500, 886, 433, 31, 171, 764, 136, 683, 163, 920, 424, 678, 456, 20, 818, 608, 860, 497, 548, 624, 856, 365, 91, 962, 268, 427, 586, 440, 428, 393, 915, 92, 925, 559, 629, 450, 40, 353, 636, 354, 460, 797, 746, 655, 811, 880, 46, 11, 772, 130, 421, 864, 524, 946, 741, 302, 8, 675, 348, 835, 568, 454, 609, 321, 666, 356, 316, 593, 580, 406, 146, 246, 499, 313, 560, 131, 784, 23, 101, 492, 968, 204, 445, 141, 394, 264, 374, 64, 723, 959, 787, 669, 927, 539, 900, 557, 518, 914, 470, 812, 172, 861, 839, 972, 685, 434, 17, 332, 2, 439, 18, 459, 845, 857, 334, 338, 111, 930, 120, 596, 865, 790, 240, 933, 30, 576, 912, 384, 936, 761, 695, 699, 895, 594, 762, 558, 606, 4, 442, 293, 99, 76, 405, 607, 742, 289, 57, 357, 426, 122, 899, 369, 755, 202, 626, 259, 438, 848, 123, 686, 127, 978, 478, 958, 651, 371, 416, 698, 882, 599, 62, 733, 95, 430, 630, 229, 252, 396, 342, 693, 650, 67, 352, 828, 969, 850, 605, 883, 970, 516, 169, 385, 798, 194, 363, 955, 533, 928, 809, 474, 943, 805, 77, 85, 503, 904, 409, 744, 162, 562, 843, 830, 501, 274, 167, 207, 128, 796, 401, 724, 564, 722, 218, 523, 312, 16, 475, 997, 637, 82, 298, 177, 147, 486, 511, 135, 934, 932, 706, 300, 625, 129, 582, 714, 749, 79, 598, 781, 328, 48, 287, 154, 508, 612, 718, 905, 795, 305, 583, 453, 387, 41, 721, 567, 372, 407, 45, 485, 234, 476, 149, 158, 370, 98, 451, 692, 211, 980, 561, 160, 498, 903, 253, 921, 690, 581, 544, 432, 628, 859, 578, 164, 720, 257, 175, 315, 411, 717, 209, 794, 986, 995, 249, 311, 330, 504, 894, 28, 575, 999, 419, 49, 58, 728, 170, 944, 233, 799, 960, 982, 974, 422, 190, 182, 155, 953, 554, 173, 827, 994, 745, 756, 765, 74, 362, 180, 66, 157, 251, 226, 876, 597, 382, 304, 701, 161, 616]
We have searched the list. The number [599] does not exist in this list.
--- 0.0010008811950683594 seconds ---

In [ ]: