Write a python function that performs selection sort on the given list or tuple or string and returns the new sorted sequence


Topic: Write a python function that performs selection sort on the given list or tuple or string and returns the new sorted sequence

Solution

def selection_sort(list_to_be_sorted):
    sorted_list = list_to_be_sorted[:]
    for i in range(len(sorted_list)):
        new_min = sorted_list[i]
        new_min_old_place = i
        for j in range(i+1, len(sorted_list)):
            if new_min > sorted_list[j]:
                new_min = sorted_list[j]
                new_min_old_place = j
        old_val = sorted_list[i]
        sorted_list[i] = new_min
        sorted_list[new_min_old_place] = old_val
    return sorted_list



List all Python Programs