Reputation:
I'm trying to run selection sort in python, this is the code that I'm using
def main(list):
input_array = [12, 9, 13, 7, 3, 19, 6, 5]
output_array = selection_sort(input_array)
print(output_array)
def selection_sort(param):
for i in range(0, (len(param) - 1)):
min = i
for j in range(i + 1, len(param)):
if param[min] < param[j]:
min = j
if min != i:
temp = param[i]
param[i] = param[min]
param[min] = temp
return param
The output that I get is
Process finished with exit code 0
I'm using PyCharm as the idea, if that's of any consequence.
Upvotes: 0
Views: 126
Reputation: 180522
This will sort in ascending order:
def selection_sort(my_list):
for j in range(len(my_list)):
for i in range(j, len(my_list)):
if my_list[i] < my_list[j]:
my_list[j], my_list[i] = my_list[i], my_list[j]
return my_list
def main():
input_array = [12, 9, 13, 7, 3, 19, 6, 5]
output_array = selection_sort(input_array)
print(output_array)
[3, 5, 6, 7, 9, 12, 13, 19]
Upvotes: 0
Reputation: 78780
input_array should be a list, you are making it a set
input_array = [12, 9, 13, 7, 3, 19, 6, 5]
don't use the variable name list, it is the name for the built-in list
selection_sort
change the line
minimum, i, j, temp = 0
to
minimum, i, j, temp = 0, 0, 0, 0
Upvotes: 2