One, List lookup
1. List lookup: Find the specified element from the list
- Input: list, find element
- Output: element subscript or no element found
2, Sequential lookup: Start with the first element of the list, search in order until it is found. Returns the index that was found
3, Binary search: Starting from the candidate area of the ordered list Data[0:n], the candidate area can be reduced by half by comparing the value of the lookup with the median of the candidate area.
Binary search: Time complexity is O (LOGN)
Prerequisites for binary search: lists are ordered
The complex read of the slice is O (n) #因为切的时候是赋值的
Two-Part Lookup example
defSerach (li,val): Low= 0#Start IndexHigh = Len (li)-1#End Index whilelow<=High:mid= (Low+high)//2ifLi[mid] > val:#If the median value is larger than the value passed in, find it from the left of the middle value.High = Mid-1elifli[mid]<Val:low= Mid +1Else: returnMidElse: return-1Li= List (range (0,101,2))Print(Serach (li,98))#================== recursive version of binary lookup ===========defBin_serach_rec (li,val,low,high):iflow<=High:mid= (Low+high)//2ifLi[mid] >Val:returnBin_serach_rec (li,val,low,mid-1,) elifli[mid]<Val:returnBin_serach_rec (li,val,mid+1, High)Else: returnMidElse: returnLi= List (range (0,101,2))Print(Serach (li,98))
Second, list sorting
1. List sorting
Change an unordered list to an ordered list
2. Application Scenario
- Various charts
- Various forms
- Two-point lookup with
- to other algorithms with
Input: Unordered list
Output: Ordered list
Algorithm----list lookup and list sorting