常见排序算法

选择排序

# 选择排序
class SelectSort():
    def __init__(self,unsorted_list):
        self.unsorted_list = unsorted_list
    #
    def sort(self):
        length = len(self.unsorted_list)
        for i in range(length-1):
            min = self.unsorted_list[i]
            min_index = i
            for j in range(i+1,length):
                min,min_index = self.less(min,min_index,self.unsorted_list[j],j)
            self.unsorted_list[i],self.unsorted_list[min_index] = min,self.unsorted_list[i]
        #
        return self.unsorted_list
    #
    def less(self,first,first_index,second,second_index):
        if first < second:return first, first_index
        return second,second_index
#
l = [3,4,1,2,3,4,5,7,9]
#
so = SelectSort(l)
print(so.sort())
#
l = [3,4,1,2,3,4,5,7,9]
l.sort()
print(l)
View Code

冒泡排序(支持的数据类型: list、dict、tuple)

import copy
#
class BubbleSort(object):
    def __init__(self,unsorted_list):
        self.unsorted_list = copy.copy(unsorted_list)
        self.transform_data()
#
    def transform_data(self):
        self.data_type = type(self.unsorted_list)
#
        if self.data_type == list:pass
        elif self.data_type == tuple: self.unsorted_list = list(self.unsorted_list)
        elif self.data_type == dict:
            self.unsorted_index,self.unsorted_list = list(self.unsorted_list.keys()),list(self.unsorted_list.values())
        else:
            raise Exception("不支持%s类型数据的排序!" % (self.data_type,) )
#
    def sort(self):
        length = len(self.unsorted_list)
#
        for i in range(length-1):
            is_sorted = True
            for j in range(length-1-i):
                if self.unsorted_list[j] > self.unsorted_list[j+1]:
                    is_sorted = False
                    self.unsorted_list[j+1],self.unsorted_list[j] = self.unsorted_list[j],self.unsorted_list[j+1]
                    if self.data_type == dict:
                        self.unsorted_index[j + 1], self.unsorted_index[j] = self.unsorted_index[j],self.unsorted_index[j + 1]
            if is_sorted:break
#
        return self.restore_data()
#
    def restore_data(self):
        if self.data_type == list:return self.unsorted_list
        elif self.data_type == tuple:return tuple(self.unsorted_list)
        elif self.data_type == dict:
            return dict(zip(self.unsorted_index,self.unsorted_list))
#
l = [2,3,5,7,2,1]
d = {'1':21,'2':22,'3':1,'4':3,'5':9,'6':6,'7':3}
bs = BubbleSort(d)
print(bs.sort())
View Code

插入排序

class InsertSort(object):
    def __init__(self, unsorted_list):
        self.unsorted_list = unsorted_list
#
    def sort(self):
        length = len(self.unsorted_list)
#
        for i in range(1, length):
            present_index = i - 1
            current = self.unsorted_list[i]
#
            while present_index >= 0 and self.unsorted_list[present_index] > current:
                self.unsorted_list[present_index + 1], self.unsorted_list[present_index] = 
                    self.unsorted_list[present_index], self.unsorted_list[present_index + 1]
                present_index -= 1
#
        return self.unsorted_list
#
l = [3,5,6,7,8,1]
iss = InsertSort(l)
print(iss.sort())
View Code

希尔排序

from math import floor
#
class ShellSort(object):
    def __init__(self, unsorted_list):
        self.unsorted_list = unsorted_list
#
    def sort(self):
        length = len(self.unsorted_list)
        gap = 1
#
        while gap < (length / 3):
            gap = gap * 3 + 1
#
        while gap >= 1:
#
            for i in range(gap, length):
                temp = self.unsorted_list[i]
                j = i
                while j >= gap and self.unsorted_list[j-gap] > temp:
                    self.unsorted_list[j],self.unsorted_list[j-gap] = self.unsorted_list[j-gap],self.unsorted_list[j]
                    j -= gap
            gap = floor(gap/3)
#
        return self.unsorted_list
#
l = [84,83,88,87,61,50,70,60,80,99]
ss = ShellSort(l)
print(ss.sort())
View Code

归并排序

from math import floor
class MergeSort(object):

    def sort(self, unsorted_list):
        length = len(unsorted_list)
        if length < 2:
            return unsorted_list

        middle = floor(length / 2)

        left = unsorted_list[0:middle]
        right = unsorted_list[middle:]

        return self.merage(self.sort(left), self.sort(right))

    def merage(self, left, right):
        result = []

        while len(left) > 0 and len(right) > 0:
            if left[0] > right[0]:
                result.append(left.pop(0))
            else:
                result.append(right.pop(0))

        if len(left): result.extend(left)
        if len(right): result.extend(right)

        return result

l = [3,4,5,1,2,8,9,43,2,11,12,33,66]
ms = MergeSort()
print(ms.sort(l))
View Code
原文地址:https://www.cnblogs.com/nzd123456/p/9822233.html