In [133]:
# Pseudocode:
# Creat a empty sort_list,
# Assume the first number of the input list is the minimum,
# Compare the number with the "minimum",
# If number less than the minimum, it becomes the new minimum
# Append the "new minimum" in the sort_list and remove it from input_list at the same time
# Repeat it till there is none in the original list.
# Which means all the numbers have been added into sort_list one by one as "new minimum".
# The while loop stops, return the new sorted list.
def to_sort(input_list):
sort_list = []
while input_list:
minimum = input_list[0]
for number in input_list:
if number < minimum:
minimum = number
sort_list.append(minimum)
input_list.remove(minimum)
return sort_list
In [177]:
# Test 10
a = [5,222,4,31,3123,-109,22,15,-10,0]
%time to_sort(a)
Out[177]:
In [168]:
import random
In [179]:
# Test 100
test_100 = random.sample(range(1,10000), 100)
#%time to_sort(test_100)
#CPU times: user 477 µs, sys: 0 ns, total: 477 µs
#Wall time: 482 µs
In [182]:
# Test 1000
test_1000 = random.sample(range(1,100000), 1000)
#%time to_sort(test_1000)
#CPU times: user 38.2 ms, sys: 1.06 ms, total: 39.3 ms
#Wall time: 39 ms
In [189]:
# Pseudocode:
# Old_list is the unsorted list
# For index in the range(len(old_list))
# Start from the first number, compare it with the next one.
# If the left one is greater than the right one,exchange them.
# Else, nothing changes, go back and continue
# Repeat it till the minimum becomes the first number.
def waytwo(old_list):
for i in range(len(old_list)):
for j in range(len(old_list)-1):
if old_list[j] > old_list[j+1]:
old_list[j+1], old_list[j] = old_list[j], old_list[j+1]
return old_list
In [190]:
# Test 10
a = [5,222,4,31,3123,-109,22,15,-10,0]
%time waytwo(a)
Out[190]:
In [185]:
# Test 100
test2_100 = random.sample(range(1,10000), 100)
#%time to_sort(test2_100)
#CPU times: user 391 µs, sys: 0 ns, total: 391 µs
#Wall time: 395 µs
In [188]:
# Test 1000
test2_1000 = random.sample(range(1,100000), 1000)
#%time to_sort(test2_1000)
#CPU times: user 43.1 ms, sys: 832 µs, total: 43.9 ms
#Wall time: 43.5 ms
In [ ]: