What is an algorithm
is a computational process, the way to solve the problem
Use the Knowledge point
Recursive
Call itself
Have an end condition
The next time you perform the corresponding complexity, reduce
Order of time complexity (sorted by efficiency)
O (1) <o (LOGN) <o (n) <o (Nlogn) <o (n2) <o (N2logn) <o (n3)
Judging the complexity of time
1. The process of halving the cycle is O (logn)
2. Several loops are the complexity of several sides of n
Space complexity (space change time)
Evaluate algorithm memory consumption size
List Lookup
Sequential Lookup
Start with the first element of the list and search in order until you find it.
def Linear_seach (data_set,val): for in range (5, Data_set): if i = = val: print (i )return i return' not found '
Two-point Search
Starting from the candidate area Data[0:n] of the ordered list , the candidate area can be halved by comparing the value of the lookup with the median of the candidate area.
def bin_seacher (data_set,val): Low=0 High= Len (data_set)-1 whileLow <=High:mid= (Low+high)//2 ifData_set[mid] = =Val:print ('Index Location:', mid)returnmid elif Data_set[mid]<Val:low= Mid +1 Else: High= Mid-1Print ('not found') returnNoneli= Range (100000) Bin_seacher (Li,557)
Case
Import Randomdef random_list (n):" "generate random Data:p Aram N::return: " "RET =[] A1= ['Zhao','Money','Sun','Li','Zou','Wu','Zheng','Wang','Week'] A2= ['Force','Good','Ceremony','Li','text','built','Mei','Beauty','High',"'] A3= ['Strong','text','bin','Wide','text','Ying','Super','Cloud','Dragon',"'] IDs= Range (1001,1001+N) forIinchrange (N): Name= Random.choice (A1) + random.choice (A2) +Random.choice (A3) age= Random.randint ( -, -) DiC= {'ID': Ids[i],'name': Name,' Age': Age} ret.append (DIC)returnretdef Id_seacher (data_list,id): Low=0 High= Len (data_list)-1 whileLow <=High:mid= (Low+high)//2 ifdata_list[mid]['ID'] ==Id:print ('Index Location:', mid)returnMid elif data_list[mid]['ID'] <Id:low= Mid +1 Else: High= Mid-1Print ('not found') returnnonedata_list= Random_list ( -) Ind= Id_seacher (Data_list,1025) print (data_list[ind]['name']) #输入人名
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
The number of cyclic unordered areas continues to be compared
import randomdef Bubble_sort (LI): forIinchRange (Len (LI)-1): # Several trips to Exchange=False # flag bit forJinchRange (Len (LI)-i-1): ifLI[J] > Li[j +1]: li[j], li[j+1] = Li[j +1], Li[j] Exchange=TrueifNot Exchange: BreakLi= List (range ( +)) Random.shuffle (LI) print (LI) bubble_sort (LI) print (Li )
Time is complicated.
Best Case O (n)
General Conditions O (n2)
Worst case O (N2)
Select Sort
A trip through the smallest number of records, put in the first position;
And then go through the smallest number in the remaining list of records and continue to place;
def select_sort (LI): for inch 1 ): #循环次数 = i for in 1, Len (LI)): #从无序区找 if li[j] < Li[min_loc]: = J == List (range ( )) Random.shuffle (LI) print (LI) select_sort (LI) print (Li )
Insert Sort
The list is divided into two sections of ordered and unordered areas. The first ordered area has only one element.
Each time an element is selected from the unordered area, it is inserted into the position of the ordered area until the unordered area becomes empty.
def insert_sort (LI): for in range (1, Len (LI)): = li[i] 1while 0 and TMP < li[j]: # Determine if the new number is smaller than the previous one, and move the previous number back one position 1] = Li[j] 1 1] == List (range)random.shuffle (li) print (LI) insert_ Sort (li) print (LI)
Python basic common algorithm