Skip to main content

Sort List

Question

Given a linked list, sort it in ascending order using a standard sorting algorithm, such as selection sort, insertion sort, merge sort, or quick sort.

Example 1
Input: [3, 2, 4, 1]

Output: [1, 2, 3, 4]

Solution

all//Sort List.py


def sortList(list):
for i in range(0, len(list)):
min_idx = i
for j in range(i+1, len(list)):
if list[min_idx] > list[j]:
min_idx = j
list[i], list[min_idx] = list[min_idx], list[i]
return list

# Test
list = [64, 34, 25, 12, 22, 11, 90]
print(sortList(list))