插入排序和选择排序

def insertsort(arr):
    if arr is None:
        return
    for i in range(1, len(arr)):
        key = arr[i]
        for j in range(i-1, -1, -1):
            if arr[j] > key:
                arr[j+1] = arr[j]
            else:
                # for 循环如果不停下的话 就是在一直在执行某个条件
                arr[j+1] = key
                break
        # j = i-1
        # while j >= 0 and arr[j] > key:
        #     arr[j+1] = arr[j]
        #     j = j -1
        #
        # arr[j+1] = key

    print(arr)


def findmax(nums):
    maxNumber = nums[0]
    for i,item in enumerate(nums):
       maxNumber = max(maxNumber, nums[i])
    res = maxNumber
    nums.remove(maxNumber)
    return res


def select(nums):

    ans = []
    for i in range(len(nums)):
        ans.append(findmax(nums))
    print(ans)
    return ans



if __name__ == '__main__':
    arr = [1,5,4,2,7,11,19,53,21]
    # insertsort(arr)
    ans = select(arr)[::-1]
    print(ans)

阅读更多
个人分类: python编程
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭