I'm trying to make a selection sort using python, I just recently learned python following the book "Python Crash Course" then now I'm reading the book "grokking algorithms"
I pretty much understood the selection sort algorithm but I just can't figure out why I'm getting a TypeError: 'NoneType' object cannot be interpreted as an integer
So the way I test it is to populate my array using random.randint(1, 101) and range(0,11), what I can't figure out is why python can't interpret it as an integer? since both function returns an integer. The error seems to happen when I do the new_array.append(arr.pop(smallest)).
see my full code below. (I'm using Python 3)
import random
def find_smallest(arr):
"""Return the index of the smallest value in an array"""
smallest = arr[0]
smallest_index = 0
for i in range(1, len(arr)):
if arr[i] < smallest:
smallest = arr[i]
smallest_index = i
return smallest_index
def selection_sort(arr):
"""sort an array by storing smallest value to new array 1 by 1"""
new_array = []
for i in range(len(arr)):
smallest = find_smallest(arr)
new_array.append(arr.pop(smallest))
return new_array
my_array = []
for i in range(0, 11):
my_array.append(random.randint(1, 101))
print(my_array[i])
sorted_array = selection_sort(my_array)
for i in range(len(sorted_array)):
print(sorted_array[i])