Python 实现的“冒泡排序”

本网站用的阿里云ECS,推荐大家用。自己搞个学习研究也不错
Python 实现的”冒泡排序”

”’
Bubble Sort

@author: aihua.sun
”’
import string
import random
number = [];
count = 0;

def bubbleSort():
 
    #initializeNumberArray();
    generateRandomNumberArray(10, 1, 300);
    showArray();
    sort();
    showArray();

def showArray():
    print(number);
#Manual input number
def initializeNumberArray():
    print(“Please input array of integer, and input Q will get sort of result!”);
    while True:
        inputCommand = input(“input a number:”);
        if inputCommand != “Q”:
            #must indicator count is global variable
            global count;
            count += 1;
            number.append(inputCommand);
        else:
            break;
#automatic generate random number array
def generateRandomNumberArray(lenght, minNumber, maxNumber):
    #must indicator count is global variable
    global count;
    for index in range(lenght):
        number.append(random.randrange(minNumber, maxNumber));
        count = index;

def sort():
    for reverseIndex in range(count – 2, 0, -1):
        for index in range(reverseIndex):
            behindIndex = index + 1;
            if needSwap(number[index], number[behindIndex]):
                swapByIndex(index, behindIndex);
               

def needSwap(previousNumber, behindNumber):
    return int(previousNumber) > int(behindNumber);

def swapByIndex(previousNumber, behindNumber):
 

未经允许不得转载:演道网 » Python 实现的“冒泡排序”

赞 (0)
分享到:更多 ()

评论 0

评论前必须登录!

登陆 注册