python-选择排序

选择排序

选择排序每次只记录最大数的索引值. 类似于冒泡排序, 也是要比较n-1次, 区别是冒泡排序每次都交换, 选择排序只在最后比较完后才进行交换

#!/usr/bin/env python
# coding:utf-8


def selectSort(nums):
    for i in range(len(nums)):
        max_index = 0
        for j in range(len(nums)-i):
            if nums[max_index] < nums[j]:
                max_index = j
        nums[max_index], nums[len(nums)-i-1] = nums[len(nums)-i-1], nums[max_index]
    return nums


nums = [6,2,54435,3141]
print selectSort(nums)


posted @ 2016-11-01 00:23  qlshine  阅读(1047)  评论(0编辑  收藏  举报