1 ImportCProfile2 ImportRandom3 classSortalgorithm:4 def __init__(self,unsortedlist=[]):5Self.unsortedlist = Unsortedlistor[I forIinchRandom.sample (Range (10000), 100)]6Self.length =Len (self.unsortedlist)7 8 defBubble_sort (self):9 " "Ten Bubble Sort: Starting with the first element, each of the two adjacent elements is compared, and if the former is larger than the latter, the position is exchanged. After the last two adjacent elements are completed, the largest elements are formed, One then compare from the beginning again, if the number of elements is n, then a total of n-1 wheel comparison will be done to complete the sorting. In general, the complexity of the algorithm is the square level. A " " - forIinchRange (self.length-1): - forJinchRange (self.length-i-1): the ifSelf.unsortedlist[j]>self.unsortedlist[j+1]: -SELF.UNSORTEDLIST[J],SELF.UNSORTEDLIST[J+1] = self.unsortedlist[j+1],self.unsortedlist[j] - - returnself.unsortedlist + - defSelection_sort (self): + " " A Select sort: The smallest element is found in an unordered sequence, placed first, the smallest element is found in the remaining unsorted sequence, placed in the second position, and so on, until all the elements have been sorted. at assuming that the sequence element is a total of n, we need to find the n-1 wheel, and we can order the sequence. Time complexity is the square level. - " " - forIinchRange (self.length-1): - forJinchRange (i+1, self.length): - ifSelf.unsortedlist[i]>Self.unsortedlist[j]: -SELF.UNSORTEDLIST[I],SELF.UNSORTEDLIST[J] =Self.unsortedlist[j],self.unsortedlist[i] in returnself.unsortedlist - to defInsert_sort (self): + " " - Insert Sort: treats the first element of the sequence as an element in the sorted list, then starts with the second element, compares it to the elements in the sorted list, and places it in the appropriate position. Assuming that there are n elements that need to be sorted, you need a n-1 wheel insert to arrange the order. the The time complexity is the square level. * " " $ forIinchRange (1, self.length):Panax Notoginseng forJinchRange (i): - ifself.unsortedlist[i]<Self.unsortedlist[j]: the Self.unsortedlist.insert (J,self.unsortedlist.pop (i)) + Break A returnself.unsortedlist the + defMerge_sort (self,lists=none,divid=None): - " " $ Merge Sort: Merge sort is John von Neumann invention, is a typical division method (Divide and conquer), divides an unordered list into one, divides each sub-sequence into another, continues until it can no longer be divided. $ then, we begin the process of merging, comparing the elements of each sub-sequence and another subsequence, then merging the small elements into the result sequence, and finally completing the merge sort. Time complexity is linear to a number of levels. - " " -Lists = Listsorself.unsortedlist the ifLen (lists) <=1: - returnlistsWuyidivID = divIDorLen (lists)//2 theLeft_list = Self.merge_sort (lists=lists[:d Ivid]) -Right_list = Self.merge_sort (lists=Lists[divid:]) Wu -result = [] AboutL,r=0,0 $ whileLen (left_list) >l andLen (right_list) >r: - ifleft_list[l]<Right_list[r]: - result.append (Left_list[l]) -L+=1 A Else: + result.append (Right_list[r]) theR+=1 -result+=Left_list[l:] $result+=Right_list[r:] the returnresult the the defQuick_sort (self,lists=None): the " " - Quick Sort: Quick sort is also a kind of divide-and-conquer thought, the basic idea is to find an element in the unordered list, with this element as the benchmark, all the other elements are compared with the element, which is smaller than the element to become a sub-sequence, which is larger than the element becomes another subsequence, in so that part of the data is smaller than the other part, and then follow this method to quickly sort, and finally achieve the sorting effect. The time complexity is generally linear to a number of levels. the " " theLists = Listsorself.unsortedlist About ifLen (lists) <=1: the returnlists thesmall = [] theLarge = [] +divID =lists.pop (0) - foreachinchlists: the ifeach<divID:Bayi small.append (each) the Else: the large.append (each) - Self.quick_sort (small) - Self.quick_sort (Large) the returnSelf.quick_sort (small) +[divid]+Self.quick_sort (Large) the the the if __name__=='__main__': -Sort =Sortalgorithm () the #result = Sort.bubble_sort () the #result = Sort.selection_sort () the #result = Sort.insert_sort ()94 #result = Sort.merge_sort () theresult =Sort.quick_sort () the Print(Result)
Implementation of Python's basic sorting algorithm