Skip to content

Instantly share code, notes, and snippets.

@muromtsev
Created October 11, 2023 18:33
Show Gist options
  • Save muromtsev/1526e2afb2edd2c2d7d4f943f9b9c0e5 to your computer and use it in GitHub Desktop.
Save muromtsev/1526e2afb2edd2c2d7d4f943f9b9c0e5 to your computer and use it in GitHub Desktop.
Selection sort
def findSmallest(arr):
smallest = arr[0]
smallest_idx = 0
for i in range(1, len(arr)):
if arr[i] < smallest:
smallest = arr[i]
smallest_idx = i
return smallest_idx
def selectionSort(arr):
newArr = []
for i in range(len(arr)):
smallest = findSmallest(arr)
newArr.append(arr.pop(smallest))
return newArr
selection = selectionSort([5, 3, 6, 2, 10]) # [2, 3, 5, 6, 10]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment