diff --git a/02_selection_sort/python/01_selection_sort.py b/02_selection_sort/python/01_selection_sort.py new file mode 100644 index 0000000..4173eb5 --- /dev/null +++ b/02_selection_sort/python/01_selection_sort.py @@ -0,0 +1,22 @@ +# Finds the smallest value in an array +def findSmallest(arr): + # Stores the smallest value + smallest = arr[0] + # Stores the index of the smallest value + smallest_index = 0 + for i in range(1, len(arr)): + if arr[i] < smallest: + smallest = arr[i] + smallest_index = i + return smallest_index + +# Sort array +def selectionSort(arr): + newArr = [] + for i in range(len(arr)): + # Finds the smallest element in the array and adds it to the new array + smallest = findSmallest(arr) + newArr.append(arr.pop(smallest)) + return newArr + +print selectionSort([5, 3, 6, 2, 10])