Professional Documents
Culture Documents
Fundamentals
Input
Output
Definiteness
Finiteness
Correctness
First example: Linear Search(A,n)
Input: A- array of integers, n – an integer
Output: location of n if n is present in the array A
i=0
while A[i]!=n && i <size(A)
i=i+1
If i< size(A) return i
Else return -1
Second example: Binary Search(A,n)
Input: A- sorted array of integers, n – an integer
Output: location of n if n is present in the array A
i=0, j=size(A)
while i < j
Mid=(i+j)/2
If A[(i+j)/2] <= n
j=mid
Else
i=mid+1
If A[i]==n return i
Else return -1
Third example: Bubble Sort(A,n)
Input: A- array of integers, n – size of the array
Output: array A where numbers are arranged in ascending order
For (i=0;i<n;i++)
For (j=0;j<n-i;j++)
If A[j] > A[j+1]
swap A[j] and A[j+1]
The Random Access Machine (RAM)
Model
• A CPU
• An potentially unbounded 2
bank of memory cells, 0
1
Algorithm arrayMax(A, n)
# operations
currentMax A[0] 1
for i 1 to n 1 do 1+n
if A[i] currentMax then (n 1)
currentMax A[i] (n 1)
{ increment counter i } (n 1)
return currentMax 1
Total 4n
Growth Rates
1E+30
• Growth rates of 1E+28 Cubic
functions: 1E+26
1E+24 Quadratic
• Linear n 1E+22
Linear
• Quadratic n2 1E+20
1E+18
• Cubic n3
T (n )
1E+16
1E+14
1E+12
• In a log-log chart, 1E+10
the slope of the 1E+8
1E+6
line corresponds to 1E+4
the growth rate of 1E+2
the function 1E+0
1E+0 1E+2 1E+4 1E+6 1E+8 1E+10
n
Constant Factors
1E+26
• The growth rate is 1E+24 Quadratic
T (n )
1E+12
• 102n + 105 is a 1E+10
linear function 1E+8
• 105n2 + 108n is a 1E+6
1E+4
quadratic function
1E+2
1E+0
1E+0 1E+2 1E+4 1E+6 1E+8 1E+10
n
Goal: to simplify analysis by getting rid of
unneeded information (like “rounding”
1,000,001≈1,000,000)
Asymptotic Notation
Big-Oh Notation
2. Intractable problems:
Cannot be solved by an algorihm with polynomial time worst case complexity.
Unsolvable problems
-- Halting problem
Worst case complexity
Number of operations needed for the algorithm to guarantee output in the worst
case
i=0
while A[i]!=n && i <size(A)
i=i+1
If i< size(A) return i
Else return -1