Sorting Fuction


In [113]:
#Pseudocode for sorting

#define a function to sort:
#    set the index as 0
#    set the new list as empty
#    for number in a list:
#         take the first number and insert it into the list
#         now take the second number in a list, and compare it to numbers in the new list
#         while the number is greater than the next number in the list:
#               insert into list


#the first item in the new list is the first item in the old list
#new_list = old_list[0]
#list of indexes range(len(old_list))
#compare each item

In [114]:
#Min_sorting, this is cheeky, but works nicely. Probably not fair to use min though
def min_sorting(lst):
    new_list = []
    for number in range(len(lst)):
        lowest_number = min(lst)
        new_list.append(lowest_number)
        lst.remove(lowest_number)
    return new_list

In [134]:
#Insertion_sorting, this is what we covered in class
def insertion_sorting(lst):
    for index in range(1,len(lst)):
        currentvalue = lst[index]
        while index > 0 and lst[index-1] > currentvalue:
            lst[index]=lst[index-1]
            index = index - 1
        lst[index] = currentvalue
    return lst

In [135]:
import random

In [136]:
%time
lst10 = [random.randint(0,1000) for r in range(10)]
min_sorting(lst10)


CPU times: user 2 µs, sys: 1e+03 ns, total: 3 µs
Wall time: 5.01 µs
Out[136]:
[119, 226, 228, 426, 429, 458, 517, 573, 612, 824]

In [137]:
%time
lst10 = [random.randint(0,1000) for r in range(10)]
insertion_sorting(lst10)


CPU times: user 2 µs, sys: 0 ns, total: 2 µs
Wall time: 5.01 µs
Out[137]:
[16, 95, 289, 470, 486, 825, 863, 879, 912, 948]

In [131]:
%time
lst100 = [random.randint(0,1000) for r in range(100)]
print(min_sorting(lst100))


CPU times: user 3 µs, sys: 1 µs, total: 4 µs
Wall time: 6.91 µs
[7, 9, 17, 22, 24, 27, 43, 46, 49, 53, 67, 86, 89, 92, 93, 100, 106, 119, 137, 141, 160, 180, 180, 208, 216, 227, 227, 234, 247, 258, 270, 285, 313, 327, 331, 352, 355, 358, 364, 377, 393, 413, 417, 433, 460, 471, 473, 476, 492, 497, 506, 514, 523, 523, 524, 527, 534, 537, 543, 565, 587, 598, 602, 604, 604, 612, 629, 635, 644, 646, 685, 688, 692, 703, 726, 737, 750, 758, 766, 779, 782, 787, 795, 795, 819, 824, 850, 855, 871, 878, 879, 908, 909, 916, 928, 960, 963, 971, 980, 994]

In [132]:
%time
lst100 = [random.randint(0,1000) for r in range(100)]
print(insertion_sorting(lst100))


CPU times: user 2 µs, sys: 0 ns, total: 2 µs
Wall time: 4.77 µs
[28, 36, 59, 63, 63, 82, 83, 103, 115, 122, 124, 146, 155, 181, 192, 200, 208, 211, 216, 223, 231, 232, 238, 243, 265, 282, 287, 299, 312, 317, 340, 352, 356, 357, 369, 370, 375, 399, 425, 442, 460, 461, 464, 495, 496, 518, 519, 522, 534, 540, 552, 560, 575, 583, 600, 616, 648, 658, 667, 672, 674, 690, 702, 705, 706, 717, 725, 729, 733, 736, 762, 787, 790, 797, 804, 812, 822, 829, 831, 837, 844, 871, 884, 884, 885, 886, 890, 904, 914, 922, 930, 935, 944, 950, 958, 959, 962, 963, 979, 986]

In [130]:
%time
lst100 = [random.randint(0,1000) for r in range(1000)]
print(min_sorting(lst100))


CPU times: user 2 µs, sys: 1e+03 ns, total: 3 µs
Wall time: 5.01 µs
[0, 4, 6, 6, 7, 7, 7, 8, 9, 9, 11, 12, 12, 12, 13, 13, 14, 19, 19, 20, 20, 22, 24, 25, 28, 29, 29, 29, 29, 32, 32, 35, 36, 37, 38, 42, 43, 44, 44, 45, 46, 48, 48, 48, 50, 51, 51, 55, 55, 59, 60, 61, 62, 62, 62, 63, 63, 65, 66, 67, 69, 69, 69, 69, 70, 71, 73, 73, 74, 74, 75, 75, 76, 81, 81, 82, 82, 84, 85, 86, 86, 87, 87, 88, 88, 89, 89, 89, 89, 90, 90, 90, 91, 91, 93, 94, 96, 96, 97, 100, 100, 100, 102, 102, 102, 103, 105, 105, 105, 106, 106, 108, 110, 111, 111, 111, 111, 111, 112, 113, 114, 115, 116, 116, 117, 119, 120, 120, 120, 121, 123, 124, 125, 126, 126, 127, 127, 128, 130, 132, 135, 135, 136, 136, 138, 141, 141, 142, 142, 143, 144, 145, 146, 146, 149, 149, 150, 150, 151, 152, 154, 154, 155, 155, 156, 156, 156, 157, 158, 160, 160, 161, 164, 165, 168, 169, 169, 170, 172, 173, 174, 174, 176, 177, 177, 178, 179, 181, 182, 182, 189, 190, 193, 193, 194, 195, 195, 196, 196, 196, 197, 200, 200, 201, 203, 204, 204, 205, 206, 210, 211, 213, 216, 216, 220, 222, 222, 223, 223, 224, 225, 227, 227, 227, 228, 229, 229, 230, 231, 232, 233, 233, 234, 234, 234, 235, 235, 236, 237, 237, 237, 238, 238, 239, 239, 245, 245, 246, 246, 250, 250, 250, 253, 253, 254, 256, 256, 256, 256, 256, 257, 258, 259, 259, 261, 262, 262, 262, 263, 263, 264, 266, 269, 270, 271, 271, 271, 272, 272, 273, 274, 274, 274, 274, 275, 277, 277, 278, 281, 281, 281, 284, 286, 287, 288, 288, 289, 291, 293, 293, 293, 294, 295, 295, 297, 297, 298, 298, 299, 299, 299, 300, 300, 301, 303, 304, 304, 305, 305, 306, 310, 311, 311, 318, 318, 319, 319, 319, 319, 320, 320, 320, 321, 323, 324, 324, 326, 326, 328, 329, 330, 330, 331, 332, 336, 336, 337, 337, 337, 338, 339, 340, 340, 342, 345, 347, 348, 348, 350, 352, 352, 352, 352, 354, 354, 354, 354, 354, 356, 358, 358, 358, 361, 361, 362, 364, 365, 365, 365, 369, 370, 371, 372, 372, 372, 375, 375, 375, 379, 380, 380, 381, 382, 382, 382, 383, 387, 388, 389, 389, 390, 390, 390, 391, 393, 393, 397, 398, 399, 400, 400, 400, 401, 403, 403, 404, 404, 404, 405, 406, 406, 407, 409, 409, 409, 411, 412, 412, 412, 414, 415, 416, 421, 421, 421, 421, 423, 424, 424, 425, 425, 425, 425, 426, 426, 427, 428, 428, 429, 431, 432, 433, 434, 434, 436, 436, 439, 439, 439, 440, 441, 442, 443, 447, 451, 451, 451, 452, 454, 456, 458, 459, 460, 461, 461, 462, 463, 463, 464, 466, 467, 467, 467, 468, 468, 470, 471, 471, 476, 477, 477, 478, 478, 479, 479, 480, 481, 481, 481, 482, 485, 486, 489, 490, 491, 491, 491, 492, 495, 496, 497, 497, 498, 499, 502, 503, 503, 503, 504, 504, 505, 505, 505, 505, 506, 506, 507, 508, 512, 513, 515, 515, 515, 516, 517, 517, 518, 518, 520, 523, 524, 524, 525, 525, 526, 526, 530, 530, 531, 534, 534, 535, 536, 536, 540, 540, 542, 542, 543, 544, 544, 544, 547, 549, 549, 550, 551, 553, 554, 554, 554, 554, 554, 555, 557, 557, 558, 558, 559, 561, 561, 562, 562, 563, 566, 566, 567, 568, 568, 569, 569, 572, 572, 573, 574, 574, 575, 575, 576, 579, 581, 581, 582, 587, 590, 591, 591, 593, 595, 596, 596, 598, 599, 600, 600, 604, 606, 607, 607, 608, 609, 609, 609, 610, 611, 611, 611, 612, 612, 613, 613, 614, 615, 615, 616, 616, 617, 618, 619, 619, 620, 621, 623, 625, 625, 630, 630, 634, 635, 637, 639, 639, 641, 644, 645, 647, 648, 649, 651, 656, 657, 659, 659, 660, 663, 665, 665, 665, 669, 670, 670, 672, 673, 674, 675, 676, 676, 676, 678, 678, 678, 679, 683, 684, 684, 684, 685, 686, 686, 688, 688, 689, 690, 690, 692, 692, 694, 694, 695, 695, 695, 696, 697, 698, 699, 699, 700, 701, 703, 705, 705, 706, 707, 708, 709, 710, 710, 710, 711, 711, 711, 713, 715, 715, 716, 717, 718, 720, 721, 721, 721, 723, 725, 726, 726, 726, 726, 727, 728, 728, 728, 728, 730, 730, 730, 732, 732, 732, 732, 733, 736, 743, 744, 744, 745, 747, 747, 750, 752, 752, 753, 753, 756, 758, 760, 761, 761, 762, 762, 764, 765, 767, 768, 769, 772, 773, 773, 774, 774, 774, 775, 777, 777, 778, 778, 779, 781, 781, 782, 782, 782, 782, 782, 787, 788, 789, 793, 795, 795, 796, 798, 800, 801, 806, 806, 807, 807, 807, 813, 814, 814, 816, 818, 820, 822, 823, 826, 827, 828, 828, 828, 829, 830, 830, 831, 833, 836, 836, 837, 837, 837, 837, 838, 839, 839, 839, 839, 840, 841, 842, 842, 842, 843, 845, 846, 846, 852, 852, 853, 854, 855, 855, 857, 857, 859, 860, 860, 860, 861, 861, 861, 861, 861, 862, 865, 865, 867, 870, 871, 871, 871, 873, 874, 874, 875, 875, 875, 876, 877, 878, 878, 878, 879, 882, 882, 883, 884, 887, 887, 889, 889, 889, 890, 891, 891, 893, 894, 897, 897, 903, 904, 905, 905, 905, 906, 908, 908, 914, 915, 917, 917, 918, 918, 918, 919, 920, 920, 923, 927, 927, 927, 928, 928, 928, 932, 932, 934, 935, 938, 940, 940, 940, 941, 942, 942, 942, 942, 945, 945, 946, 948, 948, 949, 949, 949, 952, 953, 953, 954, 956, 956, 957, 958, 962, 962, 963, 963, 963, 964, 964, 965, 965, 966, 966, 968, 968, 970, 970, 972, 972, 972, 974, 974, 975, 975, 975, 977, 977, 978, 978, 979, 979, 980, 980, 982, 983, 984, 984, 986, 987, 988, 989, 990, 991, 993, 994, 994, 995, 995, 999]

In [133]:
%time
lst100 = [random.randint(0,1000) for r in range(1000)]
print(insertion_sorting(lst100))


CPU times: user 2 µs, sys: 0 ns, total: 2 µs
Wall time: 5.01 µs
[0, 0, 0, 1, 3, 3, 4, 6, 6, 7, 7, 7, 9, 9, 9, 9, 10, 10, 12, 13, 14, 14, 22, 23, 23, 23, 24, 24, 25, 27, 28, 28, 31, 32, 32, 32, 33, 33, 34, 36, 36, 37, 37, 37, 37, 38, 38, 39, 39, 42, 44, 45, 47, 48, 49, 49, 52, 55, 56, 57, 57, 57, 59, 59, 62, 63, 63, 64, 66, 66, 68, 69, 69, 70, 70, 71, 71, 73, 73, 73, 73, 73, 74, 74, 79, 79, 79, 80, 81, 81, 81, 82, 83, 84, 85, 85, 85, 88, 92, 92, 94, 94, 95, 95, 95, 96, 97, 97, 98, 98, 99, 100, 102, 103, 106, 107, 108, 110, 111, 111, 113, 115, 115, 116, 117, 117, 118, 118, 119, 119, 120, 120, 121, 122, 123, 124, 125, 125, 126, 128, 129, 130, 130, 130, 132, 132, 134, 134, 135, 135, 136, 139, 139, 139, 140, 141, 142, 144, 144, 145, 145, 147, 148, 148, 148, 148, 148, 153, 155, 157, 157, 159, 162, 163, 165, 165, 169, 171, 172, 173, 174, 175, 175, 176, 179, 179, 181, 181, 181, 182, 182, 182, 182, 183, 185, 185, 189, 191, 191, 192, 193, 193, 194, 195, 195, 196, 198, 199, 201, 202, 202, 202, 205, 205, 205, 205, 205, 206, 207, 208, 209, 210, 210, 211, 211, 211, 212, 213, 214, 215, 221, 224, 225, 225, 225, 226, 227, 227, 228, 229, 229, 230, 231, 231, 233, 234, 235, 235, 235, 236, 238, 238, 239, 241, 243, 245, 246, 247, 247, 249, 251, 254, 255, 255, 256, 256, 257, 257, 257, 259, 260, 260, 267, 270, 271, 271, 272, 273, 274, 274, 275, 275, 275, 278, 279, 281, 281, 281, 282, 282, 283, 283, 283, 283, 284, 286, 288, 293, 293, 295, 297, 299, 299, 299, 301, 304, 305, 306, 307, 307, 308, 308, 309, 310, 310, 310, 310, 310, 311, 311, 315, 315, 315, 316, 318, 319, 320, 322, 322, 323, 323, 324, 325, 325, 326, 327, 330, 330, 331, 331, 332, 333, 333, 334, 334, 339, 341, 342, 344, 344, 345, 345, 346, 346, 347, 347, 347, 348, 348, 349, 350, 351, 351, 357, 358, 358, 358, 361, 363, 366, 366, 368, 369, 370, 374, 375, 375, 377, 377, 378, 379, 380, 381, 381, 383, 384, 384, 386, 386, 387, 387, 387, 387, 389, 389, 390, 391, 395, 395, 397, 397, 397, 398, 401, 401, 402, 403, 403, 405, 408, 408, 410, 413, 415, 415, 415, 418, 419, 420, 420, 422, 422, 425, 425, 426, 426, 427, 429, 430, 431, 432, 434, 434, 435, 435, 435, 436, 436, 437, 438, 439, 445, 447, 447, 447, 447, 448, 448, 449, 449, 450, 450, 451, 451, 452, 452, 453, 453, 454, 455, 456, 456, 458, 458, 458, 459, 460, 460, 461, 463, 463, 464, 465, 465, 466, 468, 469, 470, 471, 471, 471, 473, 473, 473, 474, 475, 477, 479, 479, 479, 481, 482, 482, 484, 486, 487, 490, 490, 490, 490, 491, 494, 495, 495, 496, 497, 498, 498, 498, 498, 499, 499, 503, 503, 503, 505, 506, 507, 507, 507, 508, 508, 510, 510, 511, 512, 513, 514, 514, 516, 517, 518, 519, 519, 519, 520, 521, 522, 523, 524, 525, 528, 528, 528, 528, 530, 530, 531, 531, 531, 532, 532, 535, 535, 536, 536, 540, 540, 541, 541, 541, 542, 543, 543, 543, 543, 544, 545, 545, 546, 546, 546, 546, 546, 549, 549, 551, 554, 554, 554, 555, 556, 558, 558, 559, 560, 562, 562, 564, 566, 566, 569, 572, 572, 572, 574, 576, 577, 578, 578, 579, 581, 582, 583, 583, 585, 586, 587, 587, 589, 589, 590, 594, 596, 597, 599, 599, 600, 600, 602, 602, 602, 602, 603, 604, 605, 606, 606, 607, 609, 610, 610, 612, 613, 614, 615, 615, 617, 617, 617, 617, 619, 622, 622, 624, 625, 628, 629, 629, 629, 632, 635, 636, 637, 637, 638, 638, 638, 639, 639, 639, 641, 641, 645, 647, 648, 650, 653, 654, 654, 656, 657, 658, 659, 659, 659, 662, 662, 663, 663, 664, 666, 668, 668, 668, 670, 676, 676, 676, 679, 679, 679, 679, 680, 682, 684, 687, 687, 688, 690, 691, 691, 691, 693, 695, 695, 697, 697, 699, 699, 701, 703, 703, 704, 707, 708, 709, 712, 713, 716, 716, 717, 717, 717, 718, 720, 721, 723, 724, 725, 728, 728, 730, 732, 732, 734, 734, 735, 737, 737, 739, 739, 741, 742, 744, 744, 744, 747, 747, 747, 748, 748, 749, 753, 753, 754, 756, 756, 759, 760, 762, 762, 763, 764, 765, 765, 766, 767, 767, 772, 773, 774, 774, 776, 776, 777, 778, 779, 779, 780, 780, 782, 782, 783, 783, 785, 785, 786, 793, 796, 796, 797, 797, 798, 799, 799, 800, 801, 801, 802, 803, 804, 804, 804, 806, 806, 809, 812, 813, 816, 817, 817, 819, 819, 820, 820, 822, 822, 822, 823, 824, 826, 827, 829, 829, 829, 830, 830, 831, 831, 834, 836, 836, 837, 837, 839, 839, 839, 840, 840, 840, 840, 841, 841, 843, 846, 846, 846, 847, 847, 848, 848, 848, 850, 850, 851, 852, 854, 854, 856, 857, 857, 858, 858, 858, 859, 861, 863, 864, 865, 865, 865, 866, 867, 870, 871, 874, 876, 876, 876, 876, 876, 877, 878, 878, 882, 883, 884, 884, 886, 886, 887, 889, 891, 891, 891, 892, 893, 894, 895, 897, 897, 898, 899, 899, 899, 899, 900, 901, 902, 903, 903, 904, 905, 906, 908, 909, 910, 911, 912, 912, 913, 913, 913, 913, 914, 915, 915, 915, 917, 918, 918, 920, 920, 920, 920, 920, 921, 922, 923, 924, 924, 929, 929, 929, 929, 930, 930, 930, 930, 931, 932, 932, 933, 933, 937, 937, 938, 939, 939, 942, 945, 945, 950, 950, 952, 953, 953, 953, 953, 954, 954, 961, 964, 967, 969, 970, 971, 972, 973, 975, 976, 979, 981, 983, 984, 985, 987, 988, 988, 989, 989, 990, 990, 990, 990, 994, 995, 997, 997]