zoukankan      html  css  js  c++  java
  • 使用Python完成排序(冒泡、选择、插入法)

     1 class Sort(object):
     2 
     3     @staticmethod
     4     def bubble_sort(ls):
     5         lenth = len(ls)
     6         if lenth == 0:
     7             return []
     8         while lenth:
     9             for i in range(lenth-1):
    10                 if ls[i] > ls[i+1]:
    11                     ls[i], ls[i+1] = ls[i+1], ls[i]
    12             lenth -= 1
    13         return ls
    14 
    15     @staticmethod
    16     def select_sort(ls): 
    17         if not ls:
    18             return []
    19         lenth = len(ls)
    20         i = 0
    21         while i < lenth-1:
    22             min_v = ls[i]
    23             for n in range(i+1, lenth):
    24                 if ls[n] < min_v:
    25                     loc, min_v = n, ls[n]
    26             if ls[i] != min_v:
    27                 ls[i], ls[loc] = ls[loc], ls[i]
    28             i += 1
    29         return ls
    30 #以下有错误,当时没注意
    31 #    @staticmethod
    32 #    def insert_sort(ls): 
    33 #        if not ls:
    34 #            return []
    35 #        i = 1
    36 #        lenth = len(ls)
    37 #        while i < lenth:
    38 #            for n in range(0, i):
    39 #                if ls[n] > ls[n+1]:
    40 #                    ls[n], ls[n+1] = ls[n+1], ls[n]
    41 #            i += 1
    42 #        return ls
    43 #更正如下:
    44
     1     @staticmethod
     2     def insert_sort(ls):
     3         if not ls:
     4             return []
     5         i = 1
     6         lenth = len(ls)
     7         while i < lenth:
     8             tmp = ls[i]
     9             for n in range(i, 0, -1):
    10                 if tmp < ls[n-1]:
    11                     ls[n] = ls[n-1] #the smaller ahead
    12                 else:
    13                     ls[n] = tmp
    14                     break
    15             i += 1
    16         return ls
    45
     1     @staticmethod
     2     def shell_sort(ls):
     3         if not ls:
     4             return []
     5         lenth = len(ls)
     6         increment = lenth // 2
     7         while increment > 0: # stop the loop when increment = 0
     8             loc = increment # start from location increment
     9             while loc < lenth:
    10                 tmp = ls[loc]
    11                 for i in range(loc, increment-1, -increment):
    12                     if tmp < ls[i-increment]:   #if large than the value in loc
    13                         ls[i] = ls[i-increment] # move to the next location
    14                     else:
    15                         break
    16                 ls[i] = tmp # move the value in loc to location i
    17                 loc += 1 # loop from increment to the last of ls
    18             increment //= 2
    19         return ls # this method allow sb. to sort the ls in different increment
    46 
    47 if __name__ == '__main__':
    48     ls = [1, 9, 5, 4, 3, 7, 6]
    49     s = Sort()
    50     print(s.bubble_sort(ls[:]))
    51     print(s.select_sort(ls[:]))
    52     print(s.insert_sort(ls[:]))
    53 print(s.shell_sort(ls[:]))

    可知:

    1. 冒泡排序是将最大值(最小值)通过相邻交换到行尾(行首);
    2. 选择排序是选出最大值(最小值)放到行尾(行首);
    3. 插入排序是通过相邻交换将下一个数插入到已经排好序的序列中。
    4. 希尔排序与3相近,但通过设置间隔分别排序,直到间隔为1完成排序。
  • 相关阅读:
    变量,基本数据类型
    编程语言分类,Python介绍、IDE集成开发环境,注释
    Django之Cookie,Session
    第三章
    第二章
    第一章
    php面向对象(文件操作)
    php面向对象(目录操作)
    php有关类和对象的相关知识2
    php有关类和对象的相关知识1
  • 原文地址:https://www.cnblogs.com/lyg-blog/p/8798320.html
Copyright © 2011-2022 走看看