This algorithm series is mainly self-learning algorithm in the process of hands-on practice, write this article as notes and share personal experience, if there are errors please put forward.
NOTE: Reprint please indicate the source
questions raised:
Sort the following data in ascending order: 5, 2, 8, 6, 4, 9, 7, 3, 1
The principle of quick sorting:
The core idea of fast sequencing is (e.g.)
1. First determine a base number, so that after the comparison rules, such as this example is in ascending order, will be larger than the base on the right, smaller than the base on the left.
2. Repeat step 1 for each side until all the sorting is complete.
The idea of programming is (take the above question as an example)
Traverse comparison between left and right respectively
1 # 2 3 a = [ 5,2,8,6,4,9,7,3,1] # data 4 temp # for saving cardinality 5 left # start 6 right # start 7 i # Zoo 8 J # right cursor
1. Set the first number as the cardinality, Temp=a[left], initialize two cursor i=left;j=right (when left is greater than right, the category ends).
2. Start by the right J first traversal (must first start from the right to traverse, think about why, the latter will answer), when encountered than the base of a small stop;
Then left I begin to traverse, when encountering a number larger than the base stop, when i<j, Exchange I and J where the number A[i], a[j] = A[j],a[i],
After the exchange is complete, continue traversing until I and J meet.
3. When I and J meet, explain the completion of the classification, note that at this time the cardinality and I,j meet the number of the exchange. A[left], A[i]=a[i], A[left]
4. First handle the number to the left of the cardinality, set the left start to Left=left, the right start to right=i-1, and repeat 1 to 5 steps.
5. When the left number processing is done, the right number is processed, the left starting point is set to Left=i+1, the right starting point is right=right, and 1 to 5 steps are repeated.
Next is the Python program source code:(can download https://github.com/DIGCreat/pythonAndAlgorithms.git on GitHub)
1 #!/usr/bin/env python2 #-*-Coding:utf8-*-3 " "4 Introduction: This program is mainly used in Python to achieve a quick sort, the function of the program is to achieve5 In ascending order. 6 This procedure should pay attention to the problem of recursion depth in the case of large amount of data. 7 8 Author: King Date: 2016/08/01 version 19 " "Ten One classQuickSort (object): A " " - Self.datas: List of data to sort - _sort (): Sort function the Show (): Output result function - - Usage: - QuickSort (datas) instantiates a Sort object + - QuickSort (datas). _sort (left, right) + start sorting, due to sort Direct action A Self.datas, so the sorting results are also at save in Self.datas, left for - the starting position of the sort, right is the row - the end position of the order. So you can achieve - Local Sort - - QuickSort (datas). Show () output results in " " - def __init__(self, datas): toSelf.datas =datas + - def_sort (self, left, right): the #sort function, traversed by two cursors starting from both ends * #the left side of the data is smaller than the cardinality, so the judging condition is to encounter $ #the larger the base, the more it will stop. Panax Notoginseng #the right side of the case is opposite to the left. - # the #Note: The program must first be traversed from the right end, because + #the two ends of the loop, the final stopping condition must be met. A #, if the left end moves first, the last stop the #The value of the next is certainly larger than the base, if the + #number to base exchange, then the number to the left of the cardinality - #not all is smaller than the base, the program runs on $ #it's not correct. $ if(Left >Right ): - return -temp =Self.datas[left] thei = Left -j = RightWuyi whileI! =J: the while(Self.datas[j] >= Temp andI <j): -J-= 1 Wu - while(Self.datas[i] <= Temp andI <j): Abouti + = 1 $ - ifI <J: -Self.datas[i], self.datas[j] = - Self.datas[j], Self.datas[i] A +Self.datas[left], self.datas[i] =Self.datas[i], temp the -Self._sort (left, i-1) $Self._sort (i+1, right) the the defShow (self): the Print 'Result is:', the forIinchSelf.datas: - PrintI, in the Print "' the About if __name__=='__main__': the Try: theDatas = Raw_input ('Please input some number:') theDatas =Datas.split () +datas = [Int (datas[i]) forIinchRange (len (datas))] - exceptException: the PassBayi theQS =QuickSort (datas) theQs._sort (0, Len (datas)-1) -Qs.show ()
Finally interested students can pay attention to my public number, can always be timely and convenient to read my article. *^_^*
Scan code follow or search number: King_diary
Python and Quick sort