EXPERIMENT 1(A) AIM-To implement following algorithm using array as a data structure and analyse its time complexity. a. Bubble sort b. Insertion Sort c. Selection sort #include <bits/stdc++.h> using namespace std; using namespace std :: chrono; void insertionSort(int arr[], int n) { int i, key, j; for (i = 1; i < n; i++) { key = arr[i]; j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j = j - 1; } arr[j + 1] = key; }} void selectionSort(int arr[], int n) { int i, j, min_idx; for (i = 0; i < n-1; i++) { min_idx = i; for (j = i+1; j < n; j++) if (arr[j] < arr[min_idx]) min_idx = j; swap(arr[min_idx],arr[i]); } } void bubbleSort(int arr[], int n) { int i, j; for (i = 0; i < n-1; i++) for (j = 0; j < n-i-1; j++) if (arr[j] > arr[j+1]) swap(arr[j], arr[j+1]); } int main() { int size_arr[]={10,100,1000,2000,3000,4000,5000,6000,7000,8000,9000}; string cases[]={"BEST","AVERAGE","WORST"}; string names[]={"SELECTION_SORT","BUBBLE_SORT","INSERTION_SORT"}; void (*sort_select[])(int[], int) = {selectionSort,bubbleSort,insertionSort}; for(int k=0;k<3;k++) { cout<<" "<<names[k]<<"\n\n"; for(int u=0;u<3;u++) { cout<<"FOR "<<cases[u]<<" CASE\n"; cout<<"SIZE OF ARRAY"<<setw(20)<<" TIME_TAKEN(IN MICROSECONDS)\n"; for(int j=0;j<sizeof(size_arr)/sizeof(int);j++) { int n=size_arr[j]; int arr[n],mainarr[n]; for(int i=0;i<n;i++) { if(u==0) arr[i]=i; else if (u==1) arr[i]=rand()%1000; else arr[i]=n-i; mainarr[i]=arr[i]; } clock_t start,end; // auto start = high_resolution_clock::now(); start=clock(); (*sort_select[k])(arr,n); //auto stop = high_resolution_clock::now(); end=clock(); double duration=double(end-start)/double(CLOCKS_PER_SEC); cout <<setw(6)<< size_arr[j]<<setw(20)<<duration*1000000<<setprecision(10)<< endl;} cout<<endl; }} return 0;} OUTPUT/TABLES: GRAPHS: SELECTION_SORT TIME TAKEN(IN MICROSEC.) 180000 160000 140000 120000 100000 80000 60000 40000 20000 0 0 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000 INPUT SIZE BEST CASE AVERAGE CASE WORST CASE TIME TAKEN(IN MICRO_SECONDS) BUBBLE_SORT 800000 700000 600000 500000 400000 300000 200000 100000 0 0 1000 2000 3000 4000 5000 6000 7000 8000 9000 8000 9000 10000 INPUT SIZE BEST CASE AVERAGE CASE WORST CASE TIME TAKEN(IN MICROSECONDS) INSERTION_SORT 180000 160000 140000 120000 100000 80000 60000 40000 20000 0 0 1000 2000 3000 4000 5000 6000 7000 INPUT SIZE BEST CASE AVERAGE CASE WORST CASE 10000 EXPERIMENT(1B) AIM-To implement following algorithm using array as a data structure and analyse its time complexity. a. Quick Sort b. Merge Sort SOURCE CODE: #include <bits/stdc++.h> using namespace std; int partition (int arr[], int low, int high) { int pivot = arr[high]; int i = (low - 1); for (int j = low; j <= high - 1; j++) {if (arr[j] < pivot) { i++; swap(arr[i], arr[j]);} } swap(arr[i + 1], arr[high]); return (i + 1);} void quickSort(int arr[], int low, int high) { if (low < high) { int pi = partition(arr, low, high); quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); }} void merge(int arr[], int l, int m, int r) { int i, j, k; int n1 = m - l + 1; int n2 = r - m; int L[n1], R[n2]; for (i = 0; i < n1; i++) L[i] = arr[l + i]; for (j = 0; j < n2; j++) R[j] = arr[m + 1+ j]; i = 0; j = 0; k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++;} else{arr[k] = R[j]; j++;} k++;} while (i < n1) { arr[k] = L[i]; i++; k++;} while (j < n2) { arr[k] = R[j]; j++; k++; } } void mergeSort(int arr[], int l, int r) { if (l < r) { int m = l+(r-l)/2; mergeSort(arr, l, m); mergeSort(arr, m+1, r); merge(arr, l, m, r); }} int main() {int size_arr[]={10,100,1000,2000,3000,4000,5000,6000,7000,8000,9000}; int size_arr1[]={10,100,10000,20000,30000,40000,50000,60000,70000,80000,90000}; string cases[]={"BEST","WORST"}; string names[]={"QUICK_SORT","MERGE_SORT"}; void (*sort_select[])(int[], int,int) = {quickSort,mergeSort}; for(int k=0;k<2;k++) { cout<<" "<<names[k]<<"\n\n"; for(int u=0;u<2;u++) { cout<<"FOR "<<cases[u]<<" CASE\n"; cout<<"SIZE OF ARRAY"<<setw(20)<<" TIME_TAKEN(IN MICROSECONDS)\n"; for(int j=0;j<sizeof(size_arr)/sizeof(int);j++) { int n; if(k==0) n=size_arr[j]; else n=size_arr1[j]; int arr[n],mainarr[n]; for(int i=0;i<n;i++) { arr[i]=i; mainarr[i]=arr[i]; } if(u==0) arr[n-1]=5; clock_t start,end; // auto start = high_resolution_clock::now(); start=clock(); (*sort_select[k])(arr,0,n-1); //auto stop = high_resolution_clock::now(); end=clock(); float duration=float(end-start)/float(CLOCKS_PER_SEC); if(k==0) cout <<setw(6)<< size_arr[j]<<setw(20)<<duration*1000000<<setprecision(10)<< endl; else cout <<setw(6)<< size_arr1[j]<<setw(20)<<duration*1000000<<setprecision(10)<< endl;} cout<<endl;}} return 0;} OUTPUT: GRAPHS: QUICK SORT 400 350 300 250 200 150 100 50 0 0 2000 4000 WORST CASE 6000 8000 10000 80000 100000 BEST CASE MERGE SORT 18 16 14 12 10 8 6 4 2 0 0 20000 40000 BEST CASE 60000 WORST CASE EXPERIMENT 2 AIM- TO IMPLEMENT LINEAR SEARCH AND BINARY SEARCH AND ANALYZE ITS TIME COMPLEXITY SOURCE CODE: #include <bits/stdc++.h> using namespace std; using namespace std :: chrono; int linear_search(int arr[], int l,int n, int x) { int i; for (i = 0; i < n; i++) if (arr[i] == x) return i; return -1;} int binary_Search(int arr[], int l, int r, int x) {r--; while (l <= r) { int m = l + (r - l) / 2; if (arr[m] == x) return m; if (arr[m] < x) l = m + 1; else r = m - 1; } return -1; } int main() { int size_arr[]={50000,60000,70000,80000,100000,200000,300000,400000}; string cases[]={"BEST","WORST"}; string names[]={"LINEAR_SEARCH","BINARY_SEARCH"}; int (*sort_select[])(int[], int,int,int) = {linear_search,binary_Search}; for(int k=0;k<2;k++) { cout<<" "<<names[k]<<"\n\n"; for(int u=0;u<2;u++) { cout<<"FOR "<<cases[u]<<" CASE\n"; if(k==1) cout<<setw(10)<<"INPUT_SIZE"<<setw(20)<<"NO_OF_OPERATIONS\n"; for(int j=0;j<7;j++) {int n=size_arr[j]; int arr[n]; for(int i=0;i<n;i++) arr[i]=i; if(k==0) { if(u==0) {auto start = chrono::high_resolution_clock::now(); int ele=linear_search(arr,0,n,0); auto end = chrono::high_resolution_clock::now(); double time_taken = chrono::duration_cast<chrono::nanoseconds>(end - start).count(); time_taken *= 1e-9; cout << setw(20)<<size_arr[j]<<setw(10)<< time_taken << setprecision(9)<<endl; } else {auto start = chrono::high_resolution_clock::now(); int ele=linear_search(arr,0,n,-1); auto end = chrono::high_resolution_clock::now(); double time_taken = chrono::duration_cast<chrono::nanoseconds>(end - start).count(); time_taken *= 1e-9; cout << setw(20)<<size_arr[j]<<setw(10)<< time_taken << setprecision(9)<<endl; else { if(u==0) cout<<setw(10)<<n<<setw(20)<<1<<endl; else cout<<setw(10)<<n<<setw(20)<<(int)(log(n)/0.3)<<endl; }} } } } }} OUTPUT/TABLES GRAPHS: LINEAR SEARCH TIME_TAKEN(SEC) 0,0025 0,002 0,0015 0,001 0,0005 0 0 50000 100000 150000 200000 250000 300000 350000 INPUT SIZE BEST CASE WORST CASE BINARY SEARCH 45 40 35 30 25 20 15 10 5 0 0 50000 100000 150000 BEST CASE 200000 250000 WORST CASE 300000 350000 EXPERIMENT 3 AIM: To implement Matrix Multiplication using Strassen’s equations and analyse its time complexity. SOURCE CODE: #include<bits/stdc++.h> using namespace std; int main() { int mat[2][2],mat1[2][2]; map<char,int> mp; char c='a'; cout<<"ENTER THE FIRST 2 x 2 MATRIX A\n"; for(int i=0;i<2;i++ ) for(int j=0;j<2;j++) { cin>>mat[i][j]; mp[c]=mat[i][j]; c++; } cout<<"ENTER THE SECOND 2 x 2 MATRIX B\n"; for(int i=0;i<2;i++ ) for(int j=0;j<2;j++) { cin>>mat1[i][j]; mp[c]=mat1[i][j]; c++; } int p1,p2,p3,p4,p5,p6,p7; p1=mp['a']*(mp['f']-mp['h']); p2=(mp['a']+mp['b'])*mp['h']; p3=(mp['c']+mp['d'])*mp['e']; p4=mp['d']*(mp['g']-mp['e']); p5=(mp['a']+mp['d'])*(mp['e']+mp['h']); p6=(mp['b']-mp['d'])*(mp['g']+mp['h']); p7=(mp['a']-mp['c'])*(mp['e']+mp['f']); int resmat[2][2]; resmat[0][0]=p5+p4-p2+p6; resmat[0][1]=p1+p2; //STRASSEN'S EQUATIONS...................... resmat[1][0]=p3+p4; resmat[1][1]=p1+p5-p3-p7; cout<<"THE RESULTANT MATRIX A X B IS\n"; for(int i=0;i<2;i++) { for(int j=0;j<2;j++) cout<<resmat[i][j]<<" "; cout<<endl; } return 0; } TIME COMPLEXITY ANALYSIS FOR STRASSEN’S ALGORITHM… OUTPUT: