Search code examples
pythonalgorithmpycharmselection-sort

Selection sort in Python yields no output


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.


Solution

    • 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

    • don't use min as a variable name, it is the name for the built-in function min
    • you don't need an argument for your main method here
    • you are not calling your main method, call it after the definition of selection_sort
    • change the line

      minimum, i, j, temp = 0
      

      to

      minimum, i, j, temp = 0, 0, 0, 0