Common Order-of-Growth classifications
Small set of functions
1,logN,N,NlogN,N2,N3,abN
linearithmic and linear scale with the input size!!
Binary Search
Implementation
public static int binarySearch( int[] a, int key )
{
int lo = 0,
hi = a.length - 1;
while( lo <= hi )
{
int mid = ( hi + lo ) / 2;
if( key < a[mid] )
{
hi = mid - 1;
}
if( key > a[mid] )
{
lo = mid + 1;
}
else return mid;
}
return -1;
}
Mathematical Analysis
Proposition
BS uses at most 1+logN compares to search in a sorted array of size N
Def
T(N)=# compares to BS in a sorted subarray of size ≤N
BS in recurrence: T(N)≤T(N2)+1 for N>1 with T(1)=1 . Iterate and we will get the proof.
T(N)≤logN+1
An N2logN algorithm for 3-SUM
- Sort the N distinct numbers
- For each pair of numbers a[i] and a[j], binary search for -(a[i] + a[j])
Guiding Principle
Typically, better order of growth = faster in practice