Bubble sort (Bubble sort) is a simpler sort algorithm in the field of computer science.
It repeatedly visited the sequence to sort, comparing two elements at a time, and swapping them out if they were wrong in the order. The work of the sequence of visits is repeated until no more need to be exchanged, that is, the sequence is sorted.
The name of the algorithm is because the larger the element will slowly "float" to the top of the sequence, hence the name.
Quick Sort (Quicksort) is an improvement to the bubbling sort.
Quick Sort by C. A. R. Hoare was introduced in 1962. Its basic idea is: by a trip to sort the data to be sorted into two separate parts, one part of all the data is smaller than the other part of all the data, and then the two parts of the data are quickly sorted by this method, the entire sorting process can be recursive, so as to achieve the entire data into an ordered sequence.
(above concept excerpt from Baidu Encyclopedia)
Now sort.py file, there is a sort class sorted, containing two methods Bubble_sort and Quick_sort, the code is as follows:
Class sort (): @staticmethod def bubble_sort (arr): arr=list (arr) If len (arr) <=1: return arr for i in range (1,len (arr)): for j in range (Len (arr)-i): if arr[j] > arr[j+1]: arr[j],arr[j+1]=arr[j+1],arr[j] return arr @staticmethod def quick_sort (arr): if len (arr) <=1: return arr arr_l = [] arr_r = [] arr_m = [] key = arr[0] for i in arr: if i<key: arr_l.append (i) elif i>key: arr_r.append (i) else:&nBsp; arr_m.append (i) arr_l = sort.quick_sort (arr_l) arr_r = sort.quick_sort (Arr_r) return arr_l + arr_m + arr_r
Unit test for sort.py, the test_sort.py file code is as follows:
Import unittestfrom sort import sortclass testsort (unittest. TestCase): def test_bubble_sort_1 (self): arr=[] self.assertequals ([],Sort.bubble_sort (arr)) def test_bubble_sort_2 (self): arr=[7] Self.assertequals ([7],sort.bubble_sort (arr)) def test_bubble_sort_3 (self): arr=[ 15,12,36,22,1,7,18] self.assertequals (sorted (arr), Sort.bubble_ Sort (arr)) def test_bubble_sort_4 ( Self): arr= (15,12,36,22,1,7,18) self.assertequals (sorted (arr), Sort.bubble_sort (arr)) def test_quick_sort_1 (self): arr=[] Self.assertequals ([],sort.quick_sort (arr)) def test_quick_sort_2 (self): arr=[7] self.assertequals ([7],sort.quick_sort (arr)) def test_quick_sort_3 (self): arr=[15,12,36,22,1,7,18] self.assertequals ( Sorted (arr), Sort.quick_sort (arr)) def test_quick_sort_4 (self): arr= (15,12,36,22,1,7,18) Self.assertequals (sorted (arr), Sort.quick_sort (arr))
Choose the compiler you like, such as: Pycharm, right-click to select the appropriate case to run, you can also right-click in the blank, that is, run all case.
If you want to customize which case to run or execute the case in multiple files at the same time, you can write a run.py file, introduce the test class to be executed, and introduce multiple, code as follows:
Import unittestfrom test_sort import testsortif __name__== ' __main__ ': suite=unittest. TestSuite () #suite. Addtest (Testsort (' test_quick_sort_1 ')) #此时只运行est_quick_sort_1这条case #suite. Addtest (unittest. Testloader (). Loadtestsfromtestcase (Testsort)) #这句等价于下面那句 suite.addtest (Unittest.makesuite (Testsort)) # Executes all case Runner=unittest in the Testsort class. Texttestrunner (verbosity=2) #等于2打印详细的执行信息 Runner.run (Suite)
This article from "Today's efforts, tomorrow's success!" "Blog, be sure to keep this provenance http://zhzhgo.blog.51cto.com/10497096/1688641
Python Unit Test Basics Example