Algorithms and Time Complexity Find Maximum Element int

![Find Maximum Element int maximum(int B[], int n) { int max = B[0]; for(int Find Maximum Element int maximum(int B[], int n) { int max = B[0]; for(int](https://slidetodoc.com/presentation_image_h2/30c6772741719c27bc6f7e02ca0b1afe/image-2.jpg)
![Linear Search int search(int B[], int size, int value) { for(int j=0; j<size; j++) Linear Search int search(int B[], int size, int value) { for(int j=0; j<size; j++)](https://slidetodoc.com/presentation_image_h2/30c6772741719c27bc6f7e02ca0b1afe/image-3.jpg)












![Big Oh (O) int search_min(int list[], int from, int to) { int i; int Big Oh (O) int search_min(int list[], int from, int to) { int i; int](https://slidetodoc.com/presentation_image_h2/30c6772741719c27bc6f7e02ca0b1afe/image-16.jpg)



- Slides: 19

Algorithms and Time Complexity
![Find Maximum Element int maximumint B int n int max B0 forint Find Maximum Element int maximum(int B[], int n) { int max = B[0]; for(int](https://slidetodoc.com/presentation_image_h2/30c6772741719c27bc6f7e02ca0b1afe/image-2.jpg)
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 searchint B int size int value forint j0 jsize j Linear Search int search(int B[], int size, int value) { for(int j=0; j<size; j++)](https://slidetodoc.com/presentation_image_h2/30c6772741719c27bc6f7e02ca0b1afe/image-3.jpg)
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) else if (A[mid] > key) else } found = true; high = mid – 1; low = mid + 1;

found = false; low = 0; high = N – 1; while (( ! found) && ( low <= high)) { mid = (low + high)/2; if (A[mid] = = key) else if (A[mid] > key) else } 5 6 7 8 9 found = true; high = mid – 1; low = mid + 1; 0 1 2 3 4 10 11 12 13 14 15 2 3 5 7 10 12 15 22 28 29 32 47 48 50 55 73 key = 29 Average number of comparisons ? Order of “log(N)” as compared to N.

Performance Comparison NADRA database: ~80, 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: 1. 2. 3. • The space complexity of a program is the measure of the amount of memory that it needs to run to completion. Time Complexity: 4. • 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 O(n 2)) #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 i =0; i < row; i++) { 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) { for (int i =0; i < row; i++) { for(int j=0; j<col; j++) { c[i][j]=a[i][j]+b[i][j]; } } }

for (int i = 1; i <= 4; i++) { int sum = 0; if (i != 4) { for (j = 1; j <= 4; { cin >> num; cout << num sum = sum + } cout << "sum = " << } } O(n 2)) j++) << " "; num; sum << endl;

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<<" "; for(i=k; i<=30; i++) cout<<"*"; cout<<endl; } } O(n 2))

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; } cout<<"n"; for(int l=0; l<4; l++) { cout<<l+1; } cout<<"n"; for(int m=4; m>0; m--) { cout<<m; } cout<<"n"; 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 45 n+3 or 100 n+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 int searchminint list int from int to int i int Big Oh (O) int search_min(int list[], int from, int to) { int i; int](https://slidetodoc.com/presentation_image_h2/30c6772741719c27bc6f7e02ca0b1afe/image-16.jpg)
Big Oh (O) 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) O(log n) O(n 2) O(n 3) O(2 n) - constant logarithmic linear log linear quadratic cubic 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(n) O(log n)