After implementing suggestions from an earlier, related question (Python: Insertion Sort), I have written this code. Could you help me to improve this code?
def get_input():
input_str = input("Enter elements to be sorted: ")
lst = list(map(int, input_str.split()))
return lst
def selection_sort(thelist):
for i in range(len(thelist)-1):
min_idx = i
for j in range(i+1, len(thelist)):
if thelist[j] < thelist[min_idx]:
min_idx = j
thelist[i], thelist[min_idx] = thelist[min_idx], thelist[i]
if __name__ == '__main__':
input_list = get_input()
selection_sort(input_list)
print(*input_list, sep = ", ")
get_input()
could handle errors in the input (e.g. if the user enters "a, b ,c"). You could add doc strings and you could add some unit tests forselection_sort()
(e.g., sort and empty list, a one element list, an already sorted list, etc.) \$\endgroup\$