Analysis of algorithms
Mathematical model to estimate running time
Total running time: sum of cost * frequency for all operations
Order-of-Growth Classifications: logN, N, N logN,N2,N3,and 2N
Binary search: example of logN
public static binarySearch(int[] a,int key)
{
int lo = 0, hi = a.length-1;
while(lo<=hi)
{
int mid = lo + (hi-lo)/2;
if (key < a[mid]) hi = mid - 1;
else if (key > a[mid]) lo = mid + 1;
else return mid;
}
return -1;
}
Binary search uses at most 1+logN compares to search in a sorted array of size N.
学习目标:给一个算法,能算出来时间复杂度
Memory
There is a way to calculate the memory the program needs.