python-选择排序

def selectedSort(myList):
  #获取list的长度
  length = len(myList)
  #一共进行多少轮比较
  for i in range(0,length-1):
    #默认设置最小值得index为当前值
    smallest = i
    #用当先最小index的值分别与后面的值进行比较,以便获取最小index
    for j in range(i+1,length):
      #如果找到比当前值小的index,则进行两值交换
      if myList[j]<myList[smallest]:
        tmp = myList[j]
        myList[j] = myList[smallest]
        myList[smallest]=tmp
    #打印每一轮比较好的列表
    print("Round ",i,": ",myList)
myList = [1,4,5,0,6]
print("Selected Sort: ")
selectedSort(myList)
Selected Sort: 
Round  0 :  [0, 4, 5, 1, 6]
Round  1 :  [0, 1, 5, 4, 6]
Round  2 :  [0, 1, 4, 5, 6]
Round  3 :  [0, 1, 4, 5, 6]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值