Final Assingmentpdfcse 373
Final Assingmentpdfcse 373
1000 1007
100000 1013
10000000 1020
100000000 1099
1100
Time Taken in ms
1080
1060
1040
1020
1000
-50000000 0 50000000 100000000 150000000
Input Size
Source Code:
#include <bits/stdc++.h>
using namespace std;
void n_function(){
int n = 100000000;
int cnt = 0;
for(int i = 0; i<n; i++){
cnt++;
}
cout << cnt << "\n";
}
int main(){
auto start =
chrono::high_resolution_clock::now();
this_thread::sleep_for(chrono::seconds(
1));
n_function();
auto stop =
chrono::high_resolution_clock::now();
auto duration =
chrono::duration_cast<chrono::millisec
onds>(stop - start);
cout << "Time taken by function: "
<< duration.count() << "
milliseconds" << endl;
}
x(input) y(time ms)
10000 1004
1000000 1011
100000000 1013
1000000000 1018
1014
1012
1010
1008
1006
1004
1002
-2E+08 200000000 600000000 1E+09
Input Size
Source code:
#include <bits/stdc++.h>
using namespace std;
void logn_function(){
int n = 1000000000;
int cnt = 0;
for(int i = n; i > 0; i /= 2){
cnt++;
}
cout << cnt << "\n";
}
int main(){
auto start =
chrono::high_resolution_clock::now();
this_thread::sleep_for(chrono::seconds(
1));
logn_function();
auto stop =
chrono::high_resolution_clock::now();
auto duration =
chrono::duration_cast<chrono::millisec
onds>(stop - start);
cout << "Time taken by function: "
<< duration.count() << "
milliseconds" << endl;}
x(input) y(time ms)
10000 1006
1000000 1036
10000000 1275
100000000 3772
2000
1500
1000
500
0
0 50000000 100000000 150000000
Input Size
Source Code:
#include <bits/stdc++.h>
using namespace std;
void nlogn_function(){
int n = 100000;
int cnt = 0;
for(int j = 1; j <= n; j++){
for(int i = n; i > 0; i /= 2){
cnt++;
}
}
cout << cnt << "\n";
}
int main(){
auto start =
chrono::high_resolution_clock::now();
this_thread::sleep_for(chrono::seconds(
1));
nlogn_function();
auto stop =
chrono::high_resolution_clock::now();
auto duration =
chrono::duration_cast<chrono::millisec
onds>(stop - start);
cout << "Time taken by function: "<<
duration.count() << " milliseconds" <<
endl;
x(input) y(time ms)
100 1003
1000 1011
10000 1054
100000 4606
O(n^2)
5000
4500
4000
Time Taken in ms
3500
3000
2500
2000
1500
1000
500
0
-20000 0 20000 40000 60000 80000 100000 120000
Input Size
Source Code:
#include <bits/stdc++.h>
void n2(){
int n = 100000;
int cnt = 0;
cnt++;
int main(){
n2();