使用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完成排序。

转载于:https://www.cnblogs.com/lyg-blog/p/8798320.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值