Last active
June 12, 2017 15:42
-
-
Save ShahriyarR/79ee3f175c51def78818cdf9f7031f9f to your computer and use it in GitHub Desktop.
Sort Algorithms implementation in Python3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This gist is for showing sorting algorithms implementation in Python | |
unsorted = [91, 6279, 157, 9823, 8585, 2760, 8432, 8427, 7461, 8186, 9528, 1651, 6768, 5913, 8443, 8603, 6856, 6405, 269, 9630, 7744, 3810, 4474, 8856, 1583, 1222, 7331, 1048, 3121, | |
8411, 5355, 6412, 4039, 4522, 3741, 9726, 9853, 1510, 7038, 330, 5614, 4706, 9815, 9700, 3208, 586, 6563, 2875, 2685, 6364, 5359, 8073, 9329, 8063, 2512, 3775, 9026, 9871, | |
9862, 1028, 6389, 5597, 25, 9091, 3260, 9377, 7274, 641, 8088, 9837, 2606, 3255, 1896, 1520, 666, 8842, 1939, 4606, 8992, 8292, 9155, 188, 6498, 5047, 2300, 5783, 9103, 3260, 9835, 2670, 1194, 1952, 1273, 7411, 6943, 3632, 5149, 2139, 5521, 6360, 1864, 1509, 9719, 4914, 3471, 9152, 4543, 3157, 9346, 1354, 1015, 6918, 2608, 7906, 9803, 450, | |
550, 1040, 3815, 1141, 9998, 7920, 3208, 1838, 5582, 8378, 7785, 176, 1433, 8350, 9611, 3919, 3849, 8751, 9484, 3218, 4387, 5544, 2121, 3165, 9618, 483, 4970, 1932, 3289, | |
8025, 7385, 6975, 637, 7245, 9279, 9232, 3419, 5388, 9097, 5893, 2747, 4329, 1932, 2637, 5871, 7359, 5672, 7350, 9490, 5932, 6957, 6209, 2175, 9487, 9299, 9406, 1087, 6843, 8839, 7159, 2744, 2697, 4886, 9674, 8760, 9810, 9357, 456, 5593, 5479, 6028, 9027, 6569, 817, 8912, 2934, 5593, 7489, 4410, 5070, 6231, 4789, 3088, 8214, 1102, 3712, 4249, 6328, 1005, 6990, 7439, 5451, 2791, 4901, 8753, 4987, 4121, 5769, 8367, 6130, 7288, 7451, 4308, 5624, 2449, 3000, 8051, 1832, 2266, 980, 1726, 417, 6291, 3179, 7672, 2109, 5984, 6446, 1240, 8004, 6270, 2278, 5899, 7279, 7135, 1428, 4721, 8976, 5092, 4127, 539, 2074, 1477, 2567, 2758, 6129, 9482, 3646, 6979, 7699, 5214, 6426, 8039, 9155, | |
9361, 4601, 8674, 6335, 2000, 3893, 7481, 6911, 6830, 9338, 2287, 5940, 8711, 1506, 7325, 5157, 1646, 2082, 8458, 3769, 4177, 3892, 3819, 2556, 7287, 5373, 242, 2716, 3524, 8276, 4057, 4006, 6848, 9449, 4402, 1940, 3121, 8368, 87, 6703, 9048, 8301, 1266, 612, 2941, 3285, 2099, 3869, 4060, 618, 554, 4740, 54, 2091, 5995, 6004, 1663, 3038, 4198, 8735, 8018, 9541, 2679, 7861, 4001, 2374, 9241, 5951, 5817, 6434, 9185, 9815, 2766, 2032, 1463, 8505, 311, 8184, 1559, 6976, 4726, 8753, 2640, 4316, 2695, 1200, 635, | |
2750, 731, 740, 9082, 6521, 4276, 8245, 4751, 2657, 3188, 8032, 2458, 5431, 473, 3560, 7825, 1875, 5639, 4803, 955, 9936, 3761, 7838, 4336, 7044, 4614, 2846, 1573, 4539, 8898, 4554, 1320, 1150, 2357, 4473, 5819, 9891, 7644, 6730, 7511, 3632, 6853, 7547, 6223, 4293, 5750, 8758, 7221, 386, 2250, 8354, 9934, 3700, 1809, 591, 9786, 3732, 3841, | |
1169, 4969, 151, 2893, 1604, 4755, 797, 1560, 2932, 1047, 8744, 2684, 1566, 4401, 7292, 5593, 6845, 9615, 1658, 1233, 858, 8223, 4266, 4914, 7374, 2218, 7172, 4084, 307, 5099, 498, 5401, 9717, 3254, 700, 5330, 6072, 4458, 6003, 4063, 9479, 9094, 1949, 871, 8230, 8665, 2198, 6585, 4669, 8096, 7650, 7171, 4411, 5004, 2352, 4964, 6325, 983, 6418, 6938, 5375, 8120, 9768, 290, 8840, 6214, 5437, 8183, 4747, 4722, 3381, 5219, 8394, 309, 5500, 6720, 1292, 3994, 6138, 1518, 5597, 3805, 8451, 8126, 5347, 2953, 9419, 3282, 7947, 5678, 8006, 8983, 1251, 9678, 5477, 2673, 6540, 511, 1494, 181, 6688, 8438, 8366, 105, 5891, 7853, 2265, 2442, 6932, 3597, 5269, 2906, 7108, 901, 6752, 6762, 6370, 1965, 4785, 3791, 2493, 7593, 3835, 6814, 2792, 4905, 2293, 4394, 4758, 1447, 516, 7108, 3913, 3329, 9244, 5981, 6245, 1835, 2618, 6503, 8069, 5932, 9227, 1251, 1500, | |
9834, 3850, 6997, 5435, 6936, 380, 684, 5100, 1490, 9444, 5982, 4289, 7169, 5754, 4802, 6933, 4713, 3379, 4495, 559, 6381, 2363, 7424, 5111, 4747, 4447, 5741, 4482, 4314, | |
8629, 7487, 7300, 1732, 1050, 1442, 1264, 5129, 2956, 96, 7331, 731, 357, 4338, 2606, 3216, 1622, 9302, 9772, 6415, 5011, 8312, 9483, 7798, 1844, 4778, 3422, 2719, 1418, 8632, 9981, 2694, 1816, 299, 7789, 8235, 5610, 1949, 3422, 2335, 4967, 3812, 2593, 5323, 6224, 6915, 4031, 6322, 2119, 5536, 1027, 6742, 2914, 9707, 2524, 8582, 4538, 4894, | |
7396, 6756, 7695, 1722, 473, 1749, 9099, 6015, 3534, 8966, 7581, 1013, 4691, 4149, 7323, 5290, 6151, 8919, 4812, 4066, 4057, 3434, 4410, 1560, 5550, 9845, 5540, 4719, 8663, 9677, 671, 7361, 1948, 7151, 8979, 5765, 8675, 3460, 7373, 1109, 5523, 5419, 8662, 4047, 6940, 8373, 9968, 504, 2062, 6246, 1524, 6620, 4833, 1591, 7763, 4027, 921, 9114, 1899, 8729, 9561, 6078, 7718, 3511, 2914, 4018, 2089, 3544, 7340, 5480, 3368, 7629, 2500, 9065, 5740, 706, 220, 9043, 3520, 2111, 1828, 9292, 9209, 267, 4130, 21, 440, | |
4483, 7783, 1163, 9246, 1290, 5216, 5537, 9172, 3372, 5525, 4572, 6044, 8487, 232, 3818, 8553, 9761, 8342, 7274, 358, 768, 2185, 9025, 9189, 81, 7868, 5313, 213, 5589, 9088, 5311, 1904, 9723, 6885, 6589, 5485, 5359, 1481, 111, 1917, 8650, 97, 3799, 6524, 4546, 9788, 474, 5450, 9350, 674, 8858, 4596, 537, 944, 3998, 746, 568, 2808, 6369, 8959, 9828, 4963, 7929, 4541, 2725, 9240, 2200, 6109, 9005, 8078, 4995, 3788, 5357, 7327, 5809, 560, 3067, 9303, 2306, 6475, 6388, 1170, 1309, 4327, 8381, 2518, 5484, 4408, 7880, 9530, 5183, 9251, 4827, 2866, 5568, 9272, 1523, 3799, 4494, 8047, 1752, 5837, 9770, 8281, 7284, 9444, 6398, 9902, 5808, 2632, 3249, 2600, 9017, 36, 660, 5486, 5911, 8147, 1299, 7957, 2578, 9131, 5443, 9983, 2498, 7450, 4740, 1719, 3897, 9658, 2261, 5747, 8225, 1333, 1481, 3733, 3657, 1373, 5670, 795, 8798, 348, 5570, 5690, 4015, 1273, | |
5565, 2963, 2199, 865, 4923, 7392, 8091, 2584, 8391, 4750, 424, 6632, 6375, 8119, 2114, 8750, 7976, 5835, 8968, 6352, 7842, 9402, 7317, 125, 511, 8112, 1621, 5323, 6063, 5255, 7230, 5052, 7345, 7905, 814, 9169, 4290, 5085, 3734, 7234, 2146, 463, 8142, 9126, 431, 2385, 2903, 7811, 8342, 647, 5, 1398, 1871, 1774, 4606, 4417, 1412, 5112, 8227, | |
5091, 6209, 2980, 7150, 1328, 3676, 4541, 6399, 3335, 9324, 6672, 1752, 7937, 1455, 5925, 5245, 6018, 9281, 5296, 4146, 4286, 4210, 8735, 1085, 2892, 4906, 8529, 1589, 5276, 5189, 5672, 649, 6421, 4982, 2222, 2535, 2149, 6912, 7776, 7743, 2830, 9184, 7358, 6957, 7710, 1567, 5012, 9381, 5936, 78, 2683, 1638, 1219, 8091, 2331, 1542, 5296, 6091, 702, 8875, 4281, 6487, 102, 3059, 6647, 7771, 7350, 5714, 3883, 2537, 7238] | |
####################################################################### | |
# SELECTION Sort -> https://en.wikipedia.org/wiki/Selection_sort | |
def selection_sort(unsorted_list): | |
# Make copy of unsorted list here: | |
will_be_sorted = unsorted_list | |
for i in range(len(will_be_sorted)): | |
min_index = i | |
for j in range(min_index + 1, len(will_be_sorted)): | |
if will_be_sorted[j] < will_be_sorted[min_index]: | |
# catch the smaller value | |
min_index = j | |
if min_index != i: | |
# swapping | |
will_be_sorted[i], will_be_sorted[min_index] = will_be_sorted[min_index], will_be_sorted[i] | |
return will_be_sorted | |
# Calling result | |
#print(selection_sort(unsorted)) | |
#$ python3 -m cProfile /home/shako/sort_algo.py | |
# 1006 function calls in 0.034 seconds | |
# Ordered by: standard name | |
# ncalls tottime percall cumtime percall filename:lineno(function) | |
# 1 0.034 0.034 0.034 0.034 sort_algo.py:21(selection_sort) | |
# 1 0.000 0.000 0.034 0.034 sort_algo.py:3(<module>) | |
# 1 0.000 0.000 0.034 0.034 {built-in method builtins.exec} | |
# 1001 0.000 0.000 0.000 0.000 {built-in method builtins.len} | |
# 1 0.000 0.000 0.000 0.000 {built-in method builtins.print} | |
# 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} | |
# SELECTION Sort -> Pythonic way | |
def selection_sort_py_way(unsorted_list): | |
# Make copy of unsorted list here: | |
will_be_sorted = unsorted_list | |
for i in range(len(will_be_sorted)): | |
# Get the min value | |
min_val = min(will_be_sorted[i:]) | |
# Get the index of min value | |
min_index = will_be_sorted[i:].index(min_val) | |
# Swapping | |
will_be_sorted[i + min_index] = will_be_sorted[i] | |
will_be_sorted[i] = min_val | |
return will_be_sorted | |
# Calling result | |
#print(selection_sort_py_way(unsorted)) | |
#$ python3 -m cProfile /home/shako/sort_algo.py | |
# 2006 function calls in 0.015 seconds | |
# Ordered by: standard name | |
# ncalls tottime percall cumtime percall filename:lineno(function) | |
# 1 0.000 0.000 0.015 0.015 sort_algo.py:3(<module>) | |
# 1 0.003 0.003 0.015 0.015 sort_algo.py:57(selection_sort_py_way) | |
# 1 0.000 0.000 0.015 0.015 {built-in method builtins.exec} | |
# 1 0.000 0.000 0.000 0.000 {built-in method builtins.len} | |
# 1000 0.009 0.000 0.009 0.000 {built-in method builtins.min} | |
# 1 0.000 0.000 0.000 0.000 {built-in method builtins.print} | |
# 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} | |
# 1000 0.003 0.000 0.003 0.000 {method 'index' of 'list' objects} | |
############################################################################## | |
# INSERTION sort -> https://en.wikipedia.org/wiki/Insertion_sort | |
def insertion_sort(unsorted_list): | |
# Make copy of unsorted list here: | |
will_be_sorted = unsorted_list | |
# Begin for loop from index 1 | |
for i in range(1, len(will_be_sorted)): | |
curr_val = will_be_sorted[i] # Assign the value of index to current value | |
pos = i # Assing index(i) to position(pos) | |
while pos > 0 and (will_be_sorted[pos-1] > curr_val): # Check if previous value is greater than current | |
will_be_sorted[pos] = will_be_sorted[pos-1] # If while loop condition is true then swap the current and previous values here | |
pos = pos - 1 # Decrement the position here to go lefter and lefter | |
will_be_sorted[pos] = curr_val | |
return will_be_sorted | |
# Calling result | |
#print(insertion_sort(unsorted)) | |
# 6 function calls in 0.044 seconds | |
# Ordered by: standard name | |
# ncalls tottime percall cumtime percall filename:lineno(function) | |
# 1 0.000 0.000 0.044 0.044 sort_algo.py:3(<module>) | |
# 1 0.044 0.044 0.044 0.044 sort_algo.py:92(insertion_sort) | |
# 1 0.000 0.000 0.044 0.044 {built-in method builtins.exec} | |
# 1 0.000 0.000 0.000 0.000 {built-in method builtins.len} | |
# 1 0.000 0.000 0.000 0.000 {built-in method builtins.print} | |
# 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment