1. Divide and conquer (divide and conquer, d&c)
How it works: find simple baseline conditions and determine how to scale the problem to meet baseline conditions.
2. Recursive summation
def addarr (arr):
if arr==[]: return
0
else: return
Arr[0]+addarr (arr[1:])
3. Quick Sort
def quicksort (array):
If Len (array) <2: return
array# Baseline condition: An array that is empty or contains only one element is "ordered"
else:
Pivot=array [0] #递归条件
less=[i for I-array[1:] If I <=pivot] #由所有小于基准值的元素组成的子数组
greater=[i for I in array[1:] If I>=pivo T] #由所有大于基准值的元素组成的子数组 return
quicksort (less) +[pivot]+quicksort (greater)
4, the understanding of the large O representation
Large O indicates time complexity often does not consider often bright, because of the growth of different orders of magnitude, the role of constants is negligible, such as simple lookup (o (n)) and binary lookup (O (logn)). But consideration must be given to the effect of constants when considering the growth of the same order of magnitude. such as quick sort and merge sort.
5. Two extreme cases of fast sequencing
Worst case scenario: The first element is selected for each datum value, the O (n) layer is total, the time required for each layer is O (n), and the algorithm runs at O (n) *o (n) =o (n^2)
Worst case scenario: Each datum value selects the intermediate element, the total O (logn) layer, the time required for each layer is O (n), and the algorithm runs for O (logn) *o (n) =o (NLOGN)
6. Summary
D&c the problem to a gradual breakdown. When using D&C to process a list, the baseline condition is most likely an empty array or an array containing only one element. When implementing a quick sort, randomly select the elements that are used as datum values. The average elapsed time for a quick sort is O (n log n). Constants in Big O notation sometimes matter, which is why a quick sort is faster than merging a sort. When comparing simple and binary lookups, constants are almost irrelevant, because O (log n) is much faster than O (n) when the list is very long.