Created
October 11, 2023 18:33
-
-
Save muromtsev/1526e2afb2edd2c2d7d4f943f9b9c0e5 to your computer and use it in GitHub Desktop.
Selection sort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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