2016-07-29 19:48:47 +00:00
|
|
|
import sys
|
|
|
|
|
2016-07-19 07:04:43 +00:00
|
|
|
|
2016-07-29 18:47:32 +00:00
|
|
|
def simple_insertion_sort(int_list):
|
2016-07-29 19:31:20 +00:00
|
|
|
count = len(int_list)
|
|
|
|
for i in range(1, count):
|
|
|
|
temp = int_list[i]
|
|
|
|
j = i - 1
|
|
|
|
while(j >= 0 and temp < int_list[j]):
|
|
|
|
int_list[j + 1] = int_list[j]
|
|
|
|
j -= 1
|
|
|
|
int_list[j + 1] = temp
|
2016-07-19 07:04:43 +00:00
|
|
|
|
2016-07-29 19:31:20 +00:00
|
|
|
return int_list
|
2016-07-19 07:04:43 +00:00
|
|
|
|
|
|
|
|
2016-07-29 19:14:30 +00:00
|
|
|
def main():
|
2016-07-29 19:48:47 +00:00
|
|
|
# Python 2's `raw_input` has been renamed to `input` in Python 3
|
|
|
|
if sys.version_info.major < 3:
|
|
|
|
input_function = raw_input
|
|
|
|
else:
|
|
|
|
input_function = input
|
|
|
|
|
2016-07-29 19:31:20 +00:00
|
|
|
try:
|
|
|
|
print("Enter numbers separated by spaces:")
|
2016-07-29 19:48:47 +00:00
|
|
|
s = input_function()
|
2016-07-29 19:31:20 +00:00
|
|
|
inputs = list(map(int, s.split(' ')))
|
|
|
|
if len(inputs) < 2:
|
|
|
|
print('No Enough values to sort!')
|
|
|
|
raise Exception
|
2016-07-29 19:14:30 +00:00
|
|
|
|
2016-07-29 19:31:20 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
else:
|
|
|
|
sorted_input = simple_insertion_sort(inputs)
|
|
|
|
print('\nSorted list (min to max): {}'.format(sorted_input))
|
2016-07-19 07:04:43 +00:00
|
|
|
|
2016-07-29 18:47:32 +00:00
|
|
|
if __name__ == '__main__':
|
2016-07-29 19:31:20 +00:00
|
|
|
print('==== Insertion Sort ====\n')
|
|
|
|
main()
|