python实现冒泡排序法

5-11 3,382 views

方法一 : while 循环
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def bubble(bubbleList):
    listLength = len(bubbleList)
    while listLength > 0:
        for i in range(listLength - 1):
            if bubbleList[i] > bubbleList[i+1]:
                sum1 = bubbleList[i]
                bubbleList[i] = bubbleList[i+1]
                bubbleList[i+1] = sum1
        listLength -= 1
    print bubbleList

if __name__ == '__main__':
    bubbleList = [3, 4, 1, 2, 5, 8, 0,0,5,2,4,23]
    bubble(bubbleList)
方法二 : 2层for循环
arr = [1,2,65,4,2,23,23,231,65]

def bubblesort(numbers):
    for j in range(len(numbers)-1,-1,-1):
        for i in range(j):
            if numbers[i]>numbers[i+1]:
                numbers[i],numbers[i+1] = numbers[i+1],numbers[i]
    return numbers
print bubblesort(arr);

centos7安装python3.8

1.安装python 依赖 sudo yum -y install epel-release sudo yum -y update 安装完后可以 reboot 重其服务器,非必须。 2.安装gcc编译环境等等 sudo yum -y gro...

阅读全文

python清空文件夹下log文件

#!/usr/bin/python import os import sys files_list = [] def print_files(path): lsdir = os.listdir(path) dirs = [i for i in lsdir if os.path...

阅读全文

python paramiko(SFTP) 相关文档

python paramiko SFTP文档地址: http://docs.paramiko.org/en/2.4/api/sftp.html import paramiko host = "THEHOST.com" #...

阅读全文

欢迎留言