Algorithms and Time Complexity
Find Maximum Element int maximum(int B[],int n) { int max = B[0]; for(int j=0;j<n;j++) if(B[j] > max) max = [j]; } return max;
Linear Search int search(int B[],int size, int value) { for(int j=0;j<size;j++) if(B[j] = = value) return 1; } return 0;
Binary Search Algorithm found = false; low = 0; high = N – 1; while (( ! found) && ( low <= high)) { mid = (low + high)/2; if (A[mid] = = key) found = true; else if (A[mid] > key) high = mid – 1; else low = mid + 1; }
found = false; low = 0; high = N – 1; while (( ! found) && ( low <= high)) { mid = (low + high)/2; if (A[mid] = = key) found = true; else if (A[mid] > key) high = mid – 1; else low = mid + 1; } 15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 22 28 29 32 47 48 50 55 73 8 7 11 7 11 9 15 10 29 key = 29 Average number of comparisons ? Order of “log(N)” as compared to N.
Performance Comparison NADRA database: ~80,000,000 records Computer which can perform 10,000 comparisons per second Linear search: ~2.22 hours Binary search: ~0.005 seconds Roughly 1.6 million times less
Performance Analysis Does the program efficiently use primary and secondary storage? Is the program’s running time acceptable for the task? Space Complexity: The space complexity of a program is the measure of the amount of memory that it needs to run to completion. Time Complexity: The time complexity of a program is the measure of the amount of computer time it needs to run to completion.
Performance Estimation How to determine which algorithm is better? We need some mechanism to predict the performance without actually executing the program.
Example 1 – Summing of a list of numbers #include <iostream> using namespace std; int main() { int size=10; int a[size]; for (int n =0; n < size;n++) cin >> a[n]; } int sum = 0; for (int i=0; i<size; i++) sum = sum + a[i]; cout << sum << endl; return 0; O(n))
Example 2 – Matrix addition #include <iostream> using namespace std; const int row =2; const int col=3; void sum (int a[][col], int b[][col],int c[][col], int row, int col); int main() { int a[row][col]; int b[row][col]; int c[row][col]; for (int i =0; i < row;i++) { for(int j=0; j<col;j++) { cout<<"Enter values for first matrix"<<endl; cin >> a[i][j]; cout<<"Enter values for second matrix"<<endl; cin >> b[i][j]; } sum (a,b,c,row,col); { for(int j=0; j<col;j++) cout<< c[i][j]; cout<<" "; cout<<endl; return 0; void sum (int a[][col], int b[][col],int c[][col], int row, int col) c[i][j]=a[i][j]+b[i][j]; Example 2 – Matrix addition O(n2))
for (int i = 1; i <= 4; i++) { int sum = 0; if (i for (int i = 1; i <= 4; i++) { int sum = 0; if (i != 4) { for (j = 1; j <= 4; j++) cin >> num; cout << num << " "; sum = sum + num; } cout << "sum = " << sum << endl; O(n2))
int main() { int i,j,k; for(k=1;k<=32;k++) { for(i=k;i<=30;i++) cout<<"*"; for(j=2;j<=k;j++) cout<<" "; cout<<endl; } O(n2))
int main() { for(int i=0; i<1;i++) { cout<<i+1<<endl; for(int j=0;j<2;j++) { cout<<j+1; } cout<<"\n"; for(int k=0; k<3; k++) { cout<<k+1; for(int l=0; l<4; l++) { cout<<l+1; for(int m=4; m>0; m--) { cout<<m; for(int o=2; o>0; o--) { cout<<o; } cout<<"\n"; for(int p=0; p<1; p++) { cout<<p+1; return 0;
Big Oh (O) Determining the exact step count of a program can be a very difficult task Because of the inexactness of the definition of a step, exact step count is not very useful for comparative purposes. e.g., which one is better 45n+3 or 100n+10 We use some asymptotic notations as measure of growth.
Big Oh (O) An estimate of how will the running time grow as a function of the problem size.
Big Oh (O) O(n) int search_min(int list[], int from, int to) { } int i; int min= from; for (i=from; i<=to; i++) If (list[i] < list[min]) min = i; return min; } O(n)
void swap(int &a, int &b) { int temp=a; a = b; b = temp; } O(1)
Big Oh (O) O(1) - constant O(log n) - logarithmic O(n) - linear O(n log n) - log linear O(n2) - quadratic O(n3) - cubic O(2n) - exponential
Big Oh (O) Summing of list of numbers Matrix addition Searching a key in an array Binary Search O(n) O(rows.cols) O(log n)