Assignment 1

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

In [24]:
#Sorting Algorithm

def sortingalgo(numberslist):
    print("The unsorted list is",numberslist)
    import time
    start_time = time.time()
    sorted_list = []
    while numberslist:
        minimum = numberslist[0] 
        for x in numberslist: 
            if x < minimum:
                minimum = x
        sorted_list.append(minimum)
        numberslist.remove(minimum)    
    print("")
    print("The sorted list is",sorted_list)
    print("")
    print("--- %s seconds ---" % (time.time() - start_time))

In [25]:
sortingalgo([3,10,5,2])


The unsorted list is [3, 10, 5, 2]

The sorted list is [2, 3, 5, 10]

--- 0.0 seconds ---

In [26]:
import random
random.sample(range(1000), 10) # We will now use this code to be fed into our algorithm. As we can see here, it generates random numbers.


Out[26]:
[88, 47, 475, 69, 981, 174, 190, 985, 496, 638]

In [27]:
sortingalgo(random.sample(range(1000), 10))


The unsorted list is [653, 601, 183, 89, 598, 520, 380, 420, 143, 0]

The sorted list is [0, 89, 143, 183, 380, 420, 520, 598, 601, 653]

--- 0.0 seconds ---

In [28]:
sortingalgo(random.sample(range(1000), 100))


The unsorted list is [412, 601, 96, 584, 615, 337, 612, 977, 976, 701, 199, 333, 432, 815, 839, 537, 74, 65, 404, 698, 776, 132, 945, 164, 201, 200, 702, 870, 658, 203, 424, 377, 979, 770, 76, 457, 420, 88, 379, 324, 918, 607, 980, 990, 593, 292, 801, 441, 690, 438, 538, 284, 162, 632, 155, 304, 45, 68, 957, 103, 71, 388, 779, 150, 533, 375, 386, 735, 427, 879, 161, 194, 211, 832, 446, 415, 520, 844, 182, 523, 189, 418, 429, 997, 273, 973, 485, 563, 46, 950, 867, 518, 895, 693, 210, 672, 561, 627, 824, 270]

The sorted list is [45, 46, 65, 68, 71, 74, 76, 88, 96, 103, 132, 150, 155, 161, 162, 164, 182, 189, 194, 199, 200, 201, 203, 210, 211, 270, 273, 284, 292, 304, 324, 333, 337, 375, 377, 379, 386, 388, 404, 412, 415, 418, 420, 424, 427, 429, 432, 438, 441, 446, 457, 485, 518, 520, 523, 533, 537, 538, 561, 563, 584, 593, 601, 607, 612, 615, 627, 632, 658, 672, 690, 693, 698, 701, 702, 735, 770, 776, 779, 801, 815, 824, 832, 839, 844, 867, 870, 879, 895, 918, 945, 950, 957, 973, 976, 977, 979, 980, 990, 997]

--- 0.0010001659393310547 seconds ---

In [29]:
sortingalgo(random.sample(range(1000), 1000))


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

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

--- 0.03702545166015625 seconds ---