1, what is the algorithm?
Algorithm (algorithm): A computational process, a method of problem solving
2. Review: Recursion
Two characteristics of recursion: (1) call itself (2) End condition
deffunc1 (x):Print(x) func1 (x-1)defFunc2 (x):ifX>0:Print(x) func2 (x+1)deffunc3 (x):ifX>0:Print(x) func3 (x-1)defFunc4 (x):ifX>0:func4 (x-1) Print(x)
View Code
Func1 and Func2 are not recursive
Func3 and Func4 are recursive, but the results are different, func3 (5) Prints 5,4,3,2,1 and FUNC4 (5) results are 1,2,3,4,5
3. Complexity of Time
Time complexity: A thing to evaluate the efficiency of an algorithm's operation
Summary:
Time complexity is an equation (unit) that is used to estimate the run time of an algorithm.
In general, algorithms with high time complexity are faster than algorithms with low complexity.
Common time complexity (sorted by efficiency)
O (1) <o (LOGN) <o (n) <o (Nlogn) <o (n2) <o (N2logn) <o (n3)
Uncommon time complexity (look just fine)
O (n!) O (2n) o (NN) ...
How to judge the complexity of time at a glance?
The process of halving the cycle? O (LOGN)
Several loops are the complexity of N's several sides.
4. Complexity of space
Spatial complexity: An equation used to evaluate the size of an algorithm's memory footprint
5. List Lookup
List lookup: Find the specified element from the list
Input: list, find element
Output: element subscript or no element found
6. Sequential Search
Start with the first element of the list and search in order until you find it.
7, two-point search
Starting from the candidate area of the ordered list Data[0:n], the candidate area can be halved by comparing the value of the lookup with the median of the candidate area.
defBin_search (data_set,val):" "Mid: Subscript Low: The leftmost list of each loop is subscript High: the rightmost subscript for Each loop:p Aram Data_set: List:p Aram Val: The value to find: return:" " Low=0 High= Len (data_set)-1 whileLow <=High:mid= (Low+high)//2ifData_set[mid] = =Val:returnMidelifData_set[mid] >Val:high= Mid-1Else: Low= Mid + 1return
View Code
8. List sorting
Change an unordered list to an ordered list
Application scenarios: Various lists of various tables for the two-point search for other algorithms with
Input: Unordered list
Output: Ordered list
9. Three kinds of slow in sorting: Bubble sort Select sort Insert Sort
Quick Sort
Sort NB Two person group: heap Sort Merge sort
There's no one to use. Sort: Cardinal sort Hill Sort Bucket sort
Algorithm key points: unordered area of ordered area
10. Bubble sort
First, the list of every two contiguous number, if the front is larger than the back, then the exchange of these two numbers
n = len (list), loop I-pass (i=n-1), I-cycle comparison (j = n-i-1) times, J is the number of times each cycle is compared
ImportRandom,time#Decorative DevicedefCal_time (func):defWrapper (*args,**Kwargs): T1=time.time () ret= Func (*args,**Kwargs) T2=time.time ()Print('Time cost :%s \r\nfunc from%s'% (T2-t1,func.__name__)) returnfuncreturnWrapper@cal_timedefBubble_sort (LI): forIinchRange (Len (LI)-1): forJinchRange (Len (LI)-i-1): #L continued ifLI[J] > Li[j+1]: Li[j],li[j+1]=li[j+1],li[j]#down-Continuation #if LI[J] < li[j+1]: #Li[j],li[j+1]=li[j+1],li[j]Data= List (range (1000) ) random.shuffle (data)Print(data) bubble_sort (data)Print(data)
View Code
Optimized bubble Sort:
If a trip is performed in a bubbling sort without swapping, the list is already ordered, and the algorithm can be terminated directly.
ImportRandom,time#Decorative DevicedefCal_time (func):defWrapper (*args,**Kwargs): T1=time.time () ret= Func (*args,**Kwargs) T2=time.time ()Print('Time cost :%s \r\nfunc from%s'% (T2-t1,func.__name__)) returnfuncreturnWrapper@cal_timedefBubble_sort (LI): forIinchRange (Len (LI)-1): Exchange=False forJinchRange (Len (LI)-i-1): #L continued ifLI[J] > Li[j+1]: Li[j],li[j+1]=li[j+1],li[j] Exchange=True#down-Continuation #if LI[J] < li[j+1]: #Li[j],li[j+1]=li[j+1],li[j] #Exchange = True #here refers to the previous trip, the value does not occur between exchanges, the exit loop if notExchange: BreakData= List (range (1000) ) random.shuffle (data)Print(data) bubble_sort (data)Print(data)
View Code
11. Select Sort
A trip through the smallest number of records, put it in the first position; go through the smallest number in the remaining list of records and continue to place;
ImportRandom,time#Decorative DevicedefCal_time (func):defWrapper (*args,**Kwargs): T1=time.time () ret= Func (*args,**Kwargs) T2=time.time ()Print('Time cost :%s --\nfunc from%s'% (T2-t1,func.__name__)) returnfuncreturnWrapper@cal_timedefSelect_sort (LI): forIinchRange (Len (LI)-1): Min_loc=I forJinchRange (i+1, Len (LI)):ifLI[J] <Li[min_loc]: Min_loc=J Li[i],li[min_loc]= Li[min_loc],li[i]
View Code
12. Insert Sort
def Insert_sort (LI): for in range (1, Len (LI)): = Li[i] = i-1 while and tmp < Li[j]: + 1] = Li[j] -= 1 + 1] = tmp
View Code
13. Practice using the bubbling method to sort the scrambled information table with ID
ImportRandomdefrandom_list (n): IDs= Range (1000,1000+N) Result=[] A1= ["Wang","Chen","Li","Zhao","Money","Sun","Wu"] A2= ["Dan","ze","","","Crystal","Jay","Gold"] A3= ["Strong","Hua","Country","Rich","Yu","Qi","Star"] forIinchrange (N): age= Random.randint (16,38) ID=Ids[i] Name='%s%s%s'%(Random.choice (A1), Random.choice (A2), Random.choice (A3)) DiC={} dic['ID'] =ID dic['name'] =name dic['Age'] =Age result.append (DIC)returnresultdefBubble_sort (LI): forIinchRange (Len (LI)-1): forJinchRange (Len (LI)-i-1): ifli[j]['ID'] > li[j+1]['ID']: Li[j],li[j+1] = li[j+1],li[j]data1= Random_list (100) random.shuffle (data1)Print(data1) bubble_sort (data1)Print(DATA1)
View Code
Python automatic Development (algorithm) Day 27th