Question:
Please implement Selection sort, which is a simple and basic sorting algorithm. It is not very fast. Its complexity is O(n^2).
Answer:
def selection_sort(arr):
n = len(arr)
for i in range(n):
for j in range(i+1, n):
# if not in ascending order, swap
if arr[i] > arr[j]:
arr[i], arr[j] = arr[j], arr[i]
return arr
# call the function
input = [8, 3, 2, 8, 5, 4, 6, 7, 2, 6, 4, 0, 9, 4, 6, 5, 8]
print(selection_sort(input))
👍 Have fun while coding with Python!
👌 Please subscribe to receive notifications on future challenges.
💬 If you have any questions simply write a comment down below.