zoukankan      html  css  js  c++  java
  • 冒泡 [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()
    

      

  • 相关阅读:
    Map-HashMap
    Collection(List & Set)
    Redis五种数据类型详解
    Redis基本数据结构详解
    分布式Session管理
    一致性算法
    Zookeeper
    分布式锁
    线程池原理解析
    疑点难点1.1
  • 原文地址:https://www.cnblogs.com/eternal1025/p/5274845.html
Copyright © 2011-2022 走看看