Selection sort program python

前端 未结 2 1889
有刺的猬
有刺的猬 2021-01-28 17:11

So this is supposed to be a sorting program, but for some reason, it is not sorting the file I\'m giving, but just giving me straight numbers as it is. Any help would be appreci

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-28 17:24

    You have simply defined a method. You need to call it from somewhere. I added to your code and tested it. It works. Here you go: The last two lines actually make a call to the code you created: (be careful about indentation)

    alist = [42,54,2,45,6,125,32]
    print(alist)
    
    def selectionSort(alist):
        for index in range(0, len(alist)):
            ismall = index
            for i in range(index,len(alist)):
                if alist[ismall] > alist[i]:
                    ismall = i
            alist[index], alist[ismall] = alist[ismall], alist[index]
        return alist
    
    newList = selectionSort(alist)
    print newList
    

提交回复
热议问题