Python冒泡排序

来源:互联网 发布:淘宝微淘设置匿名 编辑:程序博客网 时间:2024/06/03 17:28




# -*- coding: utf-8 -*-"""Bubble SortAuthor : <jianzhang.zhang@foxmail.com>Date   : 2016-07-04Version: 1.0"""def bubbleSort(olist):    length = len(olist)    for i in range(length-1):        for j in range(length-1):            if olist[j] > olist[j+1]:                # swap the position                olist[j],olist[j+1] = olist[j+1],olist[j]    return olistif __name__ == "__main__":    testList = [9,4,7,3,8,2,5,6]    print bubbleSort(testList)>>> ================================ RESTART ================================>>> [2, 3, 4, 5, 6, 7, 8, 9]>>> 


1 0
原创粉丝点击