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

Pseudocode for Sorting Algorithm

  • Input a list of integer values
  • Assign it to a variable, tosort_list
  • Create an empty list and assign it to the variable, sorted_list
  • Assign the first number to a varibale, greatest
  • loop: for each number in the list:
    • take number in the position 0 check number in position 1 is greater than the greatest
    • if the position 1 is the greatest, assign number in the position to greatest.
    • continue until you find the greatest value
    • Once you find the greatest value, append to the list, sorted_list
    • remove that number from the original list, tosort_list
    • Continue until the tosort_list is empty.
#Pseudocode x =[5,1,2] greatest = 5 for item in x: greatest = item = 5 append to sorted list [5] remove from x now x = [1,2] greatest = 1 for item in x: greatest < item (1 < 2) greatest = item = 2 append to sorted list [5,2] remove from x now x = [1] greatest = 1 greatest = item = 1 append to sorted list [5,2,1] remove from x now x = [] ---------------------------------------- x = [1,5,2] greatest = 1 for item in x: greatest < item (1 < 5) greatest = item = 5 append to sorted list [5] remove from x. now x = [1,2] greatest = 1 for item in x: greatest < item (1 < 2) greatest = item = 2 append to sorted list [5,2] remove from x now x = [1] now x = [1] greatest = 1 greatest = item = 1 append to sorted list [5,2,1] remove from x now x = []

In [2]:
import random  
import time

In [84]:
start_time = time.time()
numbers = random.sample(range(0, 100), 10)   
print(numbers)
def sort_a_list(your_list):
    sorted_list = []
    while numbers:
        largest = max(numbers)
        index = numbers.index(largest)
        sorted_list.append(numbers.pop(index))

    return sorted_list

print(sort_a_list(numbers))
end_time = time.time()
print("Elapsed time was %g seconds" % (end_time - start_time))


[17, 14, 78, 75, 90, 54, 9, 58, 85, 87]
[90, 87, 85, 78, 75, 58, 54, 17, 14, 9]
Elapsed time was 0.00200105 seconds
start_time = time.time() numbers = random.sample(range(0, 100), 10) print(numbers) sorted_list = [] while numbers: largest = max(numbers) index = numbers.index(largest) sorted_list.append(numbers.pop(index)) print(sorted_list) end_time = time.time() print("Elapsed time was %g seconds" % (end_time - start_time))

In [86]:
start_time = time.time()
numbers = random.sample(range(0, 1000), 100)   
print(numbers)
def sort_a_list(your_list):
    sorted_list100 = []
    while numbers:
        largest = max(numbers)
        index = numbers.index(largest)
        sorted_list100.append(numbers.pop(index))

    return sorted_list100

print(sort_a_list(numbers))
end_time = time.time()
print("Elapsed time was %g seconds" % (end_time - start_time))


[628, 453, 194, 887, 985, 629, 980, 133, 709, 651, 946, 984, 569, 895, 531, 540, 434, 3, 840, 347, 374, 224, 854, 852, 479, 528, 718, 191, 757, 146, 482, 91, 790, 938, 969, 260, 168, 777, 257, 559, 363, 409, 624, 562, 502, 291, 389, 80, 818, 416, 646, 78, 423, 671, 338, 604, 426, 216, 593, 538, 848, 475, 438, 609, 2, 927, 584, 919, 481, 414, 366, 879, 564, 23, 897, 61, 251, 303, 555, 789, 740, 150, 608, 356, 451, 931, 400, 667, 606, 245, 49, 97, 163, 136, 455, 621, 527, 866, 778, 493]
[985, 984, 980, 969, 946, 938, 931, 927, 919, 897, 895, 887, 879, 866, 854, 852, 848, 840, 818, 790, 789, 778, 777, 757, 740, 718, 709, 671, 667, 651, 646, 629, 628, 624, 621, 609, 608, 606, 604, 593, 584, 569, 564, 562, 559, 555, 540, 538, 531, 528, 527, 502, 493, 482, 481, 479, 475, 455, 453, 451, 438, 434, 426, 423, 416, 414, 409, 400, 389, 374, 366, 363, 356, 347, 338, 303, 291, 260, 257, 251, 245, 224, 216, 194, 191, 168, 163, 150, 146, 136, 133, 97, 91, 80, 78, 61, 49, 23, 3, 2]
Elapsed time was 0.00300169 seconds

In [87]:
start_time = time.time()
numbers = random.sample(range(0, 10000), 1000)   

def sort_a_list(your_list):
    sorted_list1000 = []
    while numbers:
        largest = max(numbers)
        index = numbers.index(largest)
        sorted_list1000.append(numbers.pop(index))

    return sorted_list1000

print(sort_a_list(numbers))
end_time = time.time()
print("Elapsed time was %g seconds" % (end_time - start_time))


[9997, 9973, 9969, 9963, 9956, 9940, 9932, 9922, 9919, 9890, 9889, 9813, 9780, 9759, 9756, 9750, 9744, 9739, 9729, 9727, 9726, 9691, 9678, 9676, 9662, 9651, 9634, 9612, 9603, 9594, 9570, 9545, 9542, 9540, 9533, 9492, 9490, 9475, 9469, 9467, 9447, 9446, 9441, 9423, 9406, 9396, 9392, 9385, 9377, 9363, 9360, 9354, 9351, 9350, 9347, 9331, 9300, 9295, 9270, 9267, 9266, 9247, 9241, 9234, 9223, 9218, 9209, 9198, 9190, 9174, 9170, 9153, 9149, 9148, 9138, 9137, 9117, 9103, 9095, 9090, 9086, 9071, 9065, 9059, 9035, 9033, 9020, 9019, 9014, 9012, 9009, 8989, 8987, 8936, 8932, 8929, 8919, 8916, 8912, 8911, 8901, 8899, 8897, 8877, 8857, 8855, 8818, 8816, 8799, 8788, 8772, 8771, 8767, 8765, 8761, 8751, 8732, 8705, 8704, 8681, 8674, 8670, 8659, 8655, 8641, 8621, 8618, 8586, 8583, 8576, 8575, 8556, 8517, 8500, 8497, 8483, 8469, 8462, 8458, 8448, 8434, 8409, 8405, 8403, 8383, 8376, 8364, 8356, 8352, 8349, 8342, 8329, 8288, 8284, 8281, 8258, 8250, 8241, 8183, 8142, 8113, 8108, 8104, 8062, 8049, 8047, 8038, 8036, 8030, 8006, 7993, 7984, 7979, 7977, 7963, 7938, 7934, 7909, 7901, 7882, 7870, 7861, 7857, 7855, 7854, 7841, 7819, 7815, 7803, 7790, 7788, 7779, 7775, 7760, 7755, 7749, 7747, 7745, 7732, 7728, 7724, 7722, 7697, 7695, 7692, 7682, 7671, 7668, 7663, 7640, 7638, 7634, 7612, 7599, 7581, 7573, 7543, 7538, 7527, 7508, 7502, 7492, 7487, 7483, 7482, 7473, 7469, 7459, 7456, 7439, 7418, 7411, 7409, 7408, 7399, 7375, 7363, 7351, 7348, 7347, 7343, 7339, 7334, 7320, 7305, 7276, 7269, 7255, 7253, 7244, 7239, 7224, 7217, 7205, 7199, 7198, 7191, 7188, 7186, 7164, 7154, 7147, 7120, 7115, 7114, 7108, 7088, 7085, 7082, 7078, 7077, 7065, 7063, 7061, 7053, 7048, 7042, 7024, 7020, 7014, 7012, 7011, 7003, 6998, 6985, 6977, 6932, 6922, 6905, 6877, 6875, 6864, 6862, 6843, 6842, 6840, 6837, 6830, 6809, 6799, 6785, 6761, 6753, 6745, 6741, 6733, 6728, 6720, 6711, 6698, 6692, 6685, 6682, 6677, 6670, 6663, 6654, 6653, 6647, 6637, 6627, 6625, 6610, 6608, 6582, 6568, 6523, 6509, 6491, 6476, 6447, 6432, 6424, 6422, 6421, 6415, 6408, 6394, 6393, 6392, 6382, 6380, 6374, 6353, 6349, 6343, 6310, 6287, 6282, 6280, 6209, 6207, 6180, 6125, 6121, 6116, 6110, 6098, 6085, 6083, 6082, 6081, 6079, 6070, 6065, 6048, 6046, 6027, 6019, 6008, 5979, 5946, 5942, 5916, 5905, 5900, 5898, 5837, 5833, 5805, 5803, 5792, 5777, 5771, 5752, 5745, 5734, 5726, 5720, 5719, 5710, 5707, 5700, 5692, 5678, 5674, 5645, 5633, 5632, 5594, 5587, 5586, 5584, 5576, 5548, 5532, 5528, 5512, 5510, 5497, 5496, 5489, 5470, 5458, 5432, 5425, 5421, 5418, 5399, 5385, 5381, 5376, 5373, 5369, 5360, 5354, 5353, 5335, 5332, 5324, 5322, 5319, 5303, 5300, 5297, 5262, 5246, 5243, 5241, 5203, 5192, 5191, 5171, 5170, 5165, 5163, 5139, 5137, 5129, 5126, 5100, 5080, 5053, 5047, 5039, 5026, 5009, 5008, 5007, 4994, 4979, 4976, 4969, 4959, 4938, 4936, 4928, 4923, 4922, 4919, 4903, 4897, 4887, 4863, 4820, 4788, 4777, 4769, 4765, 4751, 4748, 4746, 4726, 4725, 4714, 4711, 4706, 4704, 4702, 4677, 4662, 4608, 4559, 4540, 4530, 4525, 4518, 4515, 4514, 4513, 4512, 4502, 4500, 4498, 4486, 4485, 4478, 4476, 4471, 4470, 4463, 4454, 4445, 4425, 4411, 4406, 4390, 4388, 4382, 4374, 4370, 4368, 4350, 4323, 4321, 4304, 4298, 4278, 4265, 4264, 4249, 4230, 4187, 4167, 4152, 4149, 4148, 4122, 4119, 4105, 4100, 4079, 4070, 4063, 4055, 4034, 3990, 3985, 3964, 3940, 3931, 3924, 3920, 3917, 3911, 3907, 3902, 3897, 3895, 3883, 3878, 3877, 3863, 3853, 3849, 3843, 3842, 3817, 3786, 3783, 3780, 3774, 3763, 3759, 3746, 3744, 3739, 3713, 3695, 3688, 3684, 3681, 3680, 3679, 3673, 3631, 3630, 3628, 3618, 3617, 3607, 3603, 3596, 3583, 3576, 3564, 3558, 3548, 3537, 3535, 3533, 3529, 3528, 3507, 3504, 3502, 3501, 3496, 3489, 3486, 3485, 3477, 3474, 3468, 3453, 3451, 3428, 3422, 3414, 3404, 3401, 3396, 3390, 3371, 3370, 3369, 3366, 3365, 3347, 3323, 3318, 3293, 3287, 3284, 3282, 3278, 3277, 3262, 3260, 3248, 3239, 3228, 3219, 3208, 3206, 3198, 3192, 3181, 3126, 3121, 3119, 3090, 3083, 3080, 3075, 3061, 3042, 3029, 3010, 3005, 3002, 3001, 2997, 2988, 2984, 2978, 2975, 2955, 2945, 2938, 2893, 2891, 2886, 2873, 2852, 2851, 2815, 2794, 2765, 2762, 2760, 2754, 2734, 2729, 2726, 2725, 2721, 2715, 2709, 2685, 2679, 2669, 2665, 2654, 2653, 2633, 2630, 2625, 2620, 2610, 2609, 2594, 2593, 2569, 2564, 2561, 2560, 2545, 2527, 2522, 2512, 2503, 2498, 2491, 2476, 2455, 2437, 2433, 2429, 2416, 2407, 2382, 2378, 2364, 2349, 2337, 2319, 2317, 2301, 2281, 2278, 2272, 2265, 2251, 2243, 2229, 2216, 2201, 2197, 2166, 2158, 2153, 2150, 2139, 2135, 2127, 2107, 2100, 2098, 2083, 2067, 2059, 2054, 2046, 2036, 2034, 2023, 2016, 2004, 1993, 1988, 1975, 1974, 1969, 1968, 1964, 1960, 1957, 1951, 1936, 1927, 1926, 1917, 1916, 1908, 1907, 1899, 1893, 1891, 1883, 1881, 1877, 1871, 1866, 1836, 1827, 1826, 1817, 1815, 1801, 1794, 1769, 1764, 1757, 1751, 1746, 1743, 1733, 1713, 1711, 1710, 1708, 1704, 1688, 1678, 1676, 1667, 1658, 1656, 1644, 1641, 1639, 1636, 1619, 1617, 1610, 1608, 1580, 1564, 1563, 1542, 1521, 1503, 1486, 1485, 1484, 1475, 1471, 1465, 1453, 1447, 1439, 1435, 1434, 1426, 1420, 1416, 1412, 1411, 1409, 1393, 1383, 1376, 1373, 1370, 1346, 1334, 1333, 1326, 1321, 1318, 1290, 1288, 1281, 1272, 1241, 1235, 1226, 1222, 1206, 1202, 1186, 1166, 1156, 1149, 1143, 1103, 1101, 1090, 1072, 1069, 1060, 1057, 1047, 1037, 1034, 1017, 1014, 933, 924, 911, 909, 892, 888, 866, 857, 856, 837, 836, 835, 829, 828, 826, 812, 808, 807, 802, 801, 796, 795, 792, 775, 742, 739, 723, 722, 720, 715, 710, 705, 703, 696, 684, 681, 676, 673, 661, 659, 636, 632, 614, 609, 606, 601, 596, 591, 579, 578, 568, 566, 563, 547, 541, 530, 523, 512, 509, 503, 491, 484, 479, 477, 459, 443, 442, 434, 429, 412, 404, 403, 382, 380, 373, 368, 367, 366, 356, 352, 349, 340, 334, 333, 325, 323, 315, 307, 305, 280, 277, 255, 244, 243, 242, 229, 227, 187, 183, 165, 151, 136, 128, 127, 119, 116, 114, 112, 106, 104, 103, 101, 86, 58, 51, 47, 36, 28, 19, 17, 3, 2]
Elapsed time was 0.12809 seconds

Observations:

  • Printing is time consuming
  • Looping is also time consuming
  • As the numbers to be looped through is proportional to the time required to execute and output the result