Skip to content

Commit 77da85b

Browse files
committed
code for chapter 2
1 parent f985d33 commit 77da85b

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Finds the smallest value in an array
2+
def findSmallest(arr):
3+
# Stores the smallest value
4+
smallest = arr[0]
5+
# Stores the index of the smallest value
6+
smallest_index = 0
7+
for i in range(1, len(arr)):
8+
if arr[i] < smallest:
9+
smallest = arr[i]
10+
smallest_index = i
11+
return smallest_index
12+
13+
# Sort array
14+
def selectionSort(arr):
15+
newArr = []
16+
for i in range(len(arr)):
17+
# Finds the smallest element in the array and adds it to the new array
18+
smallest = findSmallest(arr)
19+
newArr.append(arr.pop(smallest))
20+
return newArr
21+
22+
print selectionSort([5, 3, 6, 2, 10])

0 commit comments

Comments
 (0)