zoukankan      html  css  js  c++  java
  • 冒泡排序

    class BubbleSort(object):
        def __init__(self, data):
            self.data = data
            self.length = len(data)
    
        def base_sort(self):
            """
            拿到第一个数往后比较,碰到比它小的,与之交换位置,碰到比它大的则选择大的那个,用大的与后面的元素比较
            :return:
            """
            data_list = list(self.data)
            for i in range(self.length - 1):
                print("%s".center(50, "*") % (i + 1))
                for index in range(self.length - i - 1):
                    print('index', index)
                    if data_list[index] > data_list[index + 1]:
                        data_list[index], data_list[index + 1] = data_list[index + 1], data_list[index]
            return data_list
    
    
    boo = BubbleSort([10, 29, 14, 37, 11, 8])
    print(boo.base_sort())
    
  • 相关阅读:
    poj 2391 Ombrophobic Bovines
    混合欧拉回路poj 1637 Sightseeing tour
    POJ1149-PIGS
    C
    B
    A
    C
    B
    A
    O
  • 原文地址:https://www.cnblogs.com/qiaoqianshitou/p/9928087.html
Copyright © 2011-2022 走看看