2019-07-01 08:10:18 +00:00
|
|
|
"""Gnome Sort Algorithm."""
|
|
|
|
|
|
|
|
|
2016-12-04 15:00:20 +00:00
|
|
|
def gnome_sort(unsorted):
|
2019-07-01 08:10:18 +00:00
|
|
|
"""Pure implementation of the gnome sort algorithm in Python."""
|
2016-12-04 15:00:20 +00:00
|
|
|
if len(unsorted) <= 1:
|
|
|
|
return unsorted
|
2019-07-01 08:10:18 +00:00
|
|
|
|
2016-12-04 15:00:20 +00:00
|
|
|
i = 1
|
2019-07-01 08:10:18 +00:00
|
|
|
|
2016-12-04 15:00:20 +00:00
|
|
|
while i < len(unsorted):
|
2019-07-01 08:10:18 +00:00
|
|
|
if unsorted[i - 1] <= unsorted[i]:
|
2016-12-04 15:00:20 +00:00
|
|
|
i += 1
|
|
|
|
else:
|
2019-07-01 08:10:18 +00:00
|
|
|
unsorted[i - 1], unsorted[i] = unsorted[i], unsorted[i - 1]
|
2016-12-04 15:00:20 +00:00
|
|
|
i -= 1
|
2019-10-05 05:14:13 +00:00
|
|
|
if i == 0:
|
2016-12-04 15:00:20 +00:00
|
|
|
i = 1
|
2019-07-01 08:10:18 +00:00
|
|
|
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
user_input = input("Enter numbers separated by a comma:\n").strip()
|
|
|
|
unsorted = [int(item) for item in user_input.split(",")]
|
2016-12-04 15:00:20 +00:00
|
|
|
gnome_sort(unsorted)
|
2017-11-25 09:23:50 +00:00
|
|
|
print(unsorted)
|