Week1-9Order-of-Growth classifications

来源:互联网 发布:linux内核开发工程师 编辑:程序博客网 时间:2024/06/05 00:31

Common Order-of-Growth classifications

Small set of functions

1,logN,N,NlogN,N2,N3,abN

这里写图片描述

linearithmic and linear scale with the input size!!

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

  1. Sort the N distinct numbers
  2. 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

0 0
原创粉丝点击