冒泡 [Python]

冒泡Python

class BubbleSort:
    def __init__(self):
        self.initArr()
    def initArr(self):
        self.arrInfo = [60, 61, 27, 91, 92, 44, 13, 20, 24, 13]
    def bubbleSortFromStartToEnd(self):
        length = len(self.arrInfo)
        for i in range(length):
            for j in range(length-i-1):
                if self.arrInfo[j] > self.arrInfo[j+1]:
                    temp = self.arrInfo[j]
                    self.arrInfo[j] = self.arrInfo[j+1]
                    self.arrInfo[j+1] = temp
            
    def bubbleSortFromEndToStart(self):
        length = len(self.arrInfo)
        for i in range(0, length):
            for j in range(length-1, i, -1):
                if self.arrInfo[j] < self.arrInfo[j-1]:
                    temp = self.arrInfo[j]
                    self.arrInfo[j] = self.arrInfo[j-1]
                    self.arrInfo[j-1] = temp
        
    def printResult(self):
        print(self.arrInfo)
        #self.bubbleSortFromStartToEnd()
        self.bubbleSortFromEndToStart()
        print(self.arrInfo)

if __name__ == "__main__":
    BubbleSort().printResult()

  

你可能感兴趣的