简单排序

Posted json28

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单排序相关的知识,希望对你有一定的参考价值。

  1 # Author:Json
  2 
  3 class Sort(object):
  4     ‘‘‘
  5     this class include bubble sort,insert sort,select sort,quick sort,merge sort and heap sort
  6     ‘‘‘
  7 
  8     def __init__(self, li):
  9         self.li = li
 10 
 11     def bubble_sort(self):
 12         ‘‘‘
 13         this function is bubble sort
 14         :return:
 15         ‘‘‘
 16         for i in range(len(self.li) - 1):
 17             exchage = False
 18             for j in range(len(self.li) - i - 1):
 19                 if self.li[j] > self.li[j + 1]:
 20                     self.li[j], self.li[j + 1] = self.li[j + 1], self.li[j]
 21                     exchage = True
 22             if not exchage:
 23                 break
 24 
 25     def insert_sort(self):
 26         ‘‘‘
 27         this function is insert sort
 28         :return:
 29         ‘‘‘
 30         for i in range(1, len(self.li)):
 31             tmp = self.li[i]
 32             j = i - 1
 33             while j >= 0 and tmp < self.li[j]:
 34                 self.li[j + 1] = self.li[j]
 35                 j -= 1
 36             self.li[j + 1] = tmp
 37 
 38     def select_sort(self):
 39         ‘‘‘
 40         this function is select sort
 41         :return:
 42         ‘‘‘
 43         for i in range(len(self.li) - 1):
 44             min_loc = i
 45             for j in range(i + 1, len(self.li)):
 46                 if self.li[min_loc] > self.li[j]:
 47                     min_loc = j
 48             if min_loc != i:
 49                 self.li[min_loc], self.li[i] = self.li[i], self.li[min_loc]
 50 
 51     def quick_sort(self):
 52         ‘‘‘
 53         this function include "quick_sort_" and "partion"
 54         "quick_sort_" is used in Recursive, "partition" used in parting
 55         :return:
 56         ‘‘‘
 57         left = 0
 58         right = len(self.li) - 1
 59         self.__quick_sort(left, right)
 60 
 61     def __quick_sort(self, left, right):
 62         if left < right:
 63             mid = self.__partition(left, right)
 64             self.__quick_sort(left, mid)
 65             self.__quick_sort(mid + 1, right)
 66 
 67     def __partition(self, left, right):
 68         tmp = self.li[left]
 69         while left < right:
 70             while left < right and self.li[right] > tmp:
 71                 right -= 1
 72             self.li[left] = self.li[right]
 73             while left < right and self.li[left] < tmp:
 74                 left += 1
 75             self.li[right] = self.li[left]
 76         self.li[left] = tmp
 77         return left
 78 
 79     def merge_sort(self):
 80         ‘‘‘
 81         this function include __merge_sort,__merge
 82         the effect of __merge_sort is breaking down the list
 83         the effect of __merge is merging the two ordered list to one ordered list
 84         :return:
 85         ‘‘‘
 86         low = 0
 87         high = len(self.li) - 1
 88         self.__merge_sort(low, high)
 89 
 90     def __merge_sort(self, low, high):
 91         if low < high:
 92             mid = (low + high) // 2
 93             self.__merge_sort(low, mid)
 94             self.__merge_sort(mid + 1, high)
 95             self.__merge(low, mid, high)
 96 
 97     def __merge(self, low, mid, high):
 98         i = low
 99         j = mid + 1
100         ltmp = []
101         while i <= mid and j <= high:
102             if self.li[i] < self.li[j]:
103                 ltmp.append(self.li[i])
104                 i += 1
105             else:
106                 ltmp.append(self.li[j])
107                 j += 1
108         while i <= mid:
109             ltmp.append(self.li[i])
110             i += 1
111         while j <= high:
112             ltmp.append(self.li[j])
113             j += 1
114         self.li[low:high + 1] = ltmp
115 
116     def heap_sort(self):
117         n = len(self.li)
118         for i in range(n//2-1,-1,-1):
119             self.__sift(i,n-1)
120         for j in range(n-1,-1,-1):
121             self.li[0],self.li[j] = self.li[j],self.li[0]
122             self.__sift(0,j-1)
123 
124     def __sift(self,low,high):
125         i = low
126         j = i * 2 + 1
127         tmp = self.li[i]
128         while j <= high:
129             if j < high and self.li[j] < self.li[j+1]:
130                 j += 1
131             if tmp < self.li[j]:
132                 self.li[i] = self.li[j]
133                 i = j
134                 j = i * 2 + 1
135             else:
136                 break
137         self.li[i] = tmp
138 
139 
140 
141 
142 def test_sort():
143     import random
144     li = []
145     for i in range(1000):
146         li.append(i)
147     random.shuffle(li)
148     a = Sort(li)
149     a.heap_sort()
150     print(li)
151 
152 
153 test_sort()

 

以上是关于简单排序的主要内容,如果未能解决你的问题,请参考以下文章

为啥这段代码会泄露? (简单的代码片段)

C# 与 F# 中的默认排序

spark 例子wordcount topk

代码片段 - Golang 实现简单的 Web 服务器

创建自己的代码片段(CodeSnippet)

以下代码片段的时间复杂度是多少?