码迷,mamicode.com
首页 > 编程语言 > 详细

冒泡排序和鸡尾酒排序(code)

时间:2018-08-01 11:41:04      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:昨天   for   对象   美丽   als   style   简单   obj   range   

昨天回顾了下冒泡排序和鸡尾酒排序,用面向对象的方式写了一下,并且优化了代码,记录一下~


 

一、冒泡排序

# 冒泡排序
class BubbleSort(object):
    def __init__(self, data_list):
        self.data_list = data_list
        self.length = len(data_list)
    # 简单粗暴的排序方式
    def b_sort(self):
        data_list = list(self.data_list)
        for i in range(self.length):
            print(**************第%s轮循环************** % (i + 1))
            index_len = self.length
            for index in range(index_len - 1):
                print(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

    # 优化后美丽动人又可爱的冒泡排序
    def new_b_sort(self):
        data_list = list(self.data_list)
        for i in range(self.length):
            print(**************第%s轮循环************** % (i + 1))
            index_len = self.length
            mark = False
            for index in range(index_len - 1 - i):
                print(index)
                mark = True
                if data_list[index] > data_list[index + 1]:
                    data_list[index], data_list[index + 1] = data_list[index + 1], data_list[index]
                    mark = False
            if mark:
                break
        return data_list


data_list = [2, 3, 1, 7, 4, 3]
bubblesort = BubbleSort(data_list)
res1 = bubblesort.b_sort()
print(res1)
res2 = bubblesort.new_b_sort()
print(res2)

 

二、鸡尾酒排序

# 鸡尾酒排序
class CocktailSort(object):
    def __init__(self, data_list):
        self.data_list = data_list
        self.length = len(data_list)
    # 简单粗暴的排序方式
    def c_sort(self):
        data_list = list(self.data_list)
        for i in range(self.length):
            print(**************第%s轮循环************** % (i + 1))
            index_len = self.length
            print(左--->右)
            for index in range(index_len - 1):
                print(index)
                if data_list[index] < data_list[index + 1]:
                    data_list[index], data_list[index + 1] = data_list[index + 1], data_list[index]
            print(右--->左)
            index_len = self.length
            for index in range(index_len - 1):
                print(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
    # 优化后俏皮美丽又懂事的鸡尾酒排序
    def new_c_sort(self):
        data_list = list(self.data_list)
        for i in range(self.length):
            print(**************第%s轮循环************** % (i + 1))
            index_len = self.length
            mark = False
            print(左--->右)
            for index in range(index_len - 1- i):
                mark = True
                print(index)
                if data_list[index] < data_list[index + 1]:
                    data_list[index], data_list[index + 1] = data_list[index + 1], data_list[index]
                    mark = False
            print(右--->左)
            index_len = self.length
            for index in range(i, index_len - 1):
                mark = True
                print(index)
                if data_list[index] > data_list[index + 1]:
                    data_list[index], data_list[index + 1] = data_list[index + 1], data_list[index]
                    mark = False
            if mark:
                break
        return data_list


data_list = [2, 3, 1, 7, 4, 3]
cocktailsort = CocktailSort(data_list)
res1 = cocktailsort.c_sort()
res2 = cocktailsort.new_c_sort()
print(res1)
print(res2)

 

冒泡排序和鸡尾酒排序(code)

标签:昨天   for   对象   美丽   als   style   简单   obj   range   

原文地址:https://www.cnblogs.com/95lyj/p/9399267.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!