code for chapter 2
This commit is contained in:
22
02_selection_sort/python/01_selection_sort.py
Normal file
22
02_selection_sort/python/01_selection_sort.py
Normal file
@@ -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])
|
||||
Reference in New Issue
Block a user