-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlg-Eff-main.cpp
More file actions
170 lines (146 loc) · 3.79 KB
/
Alg-Eff-main.cpp
File metadata and controls
170 lines (146 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* T07: Algorithmic Efficiency Explorer
This program is designed to explore big O notion and algorithms
by Dr. Jan Pearce of Berea College
Licensed under a Creative Commons Attribution,
Share Alike 3.0 United States License */
#include <iostream>
#include <cmath>
using namespace std;
int looponconstant1(int n) {
/* demonstrates O(1) behavior
pre: n is positive integer
post: returns count # iterations*/
int count = 0;
for (int i = 0; i < 1; i++) {
count++; // # this line executes
}
return (count);
}
int looponconstant4(int n) {
/* demonstrates O(1) behavior
pre: n is a positive integer
post: returns count # iterations*/
int count = 0;
for (int i = 0; i < 4; i++) {
count++; // # this line executes
}
return (count);
}
int loopn(int n) {
/* demonstrates O(n) behavior
pre: n is a positive integer
post: returns count # iterations */
int count = 0;
for (int i = 0; i < n; i++) {
count++; // # this line executes
}
return (count);
}
int sequential(int n) {
/* demonstrates O(n) behavior
pre: n is positive integer
post: returns count # iterations */
int count = 0;
for (int i = 0; i < n; i++) {
count++; //this # iterations
}
for (int i = 0; i < n; i++) {
count++; //plus this # iterations
}
return (count);
}
int nestedconstant(int n) {
/* demonstrates O(n) behavior
pre: n is a positive integer
post: returns count # iterations*/
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 10; j++) {
count++; // # this line executes
}
}
return (count);
}
int twonested(int n) {
/* demonstrates O(n**2) behavior
pre: n is positive integer
pot: returns count # iterations*/
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
count++; // # this line executes
}
}
return (count);
}
int threenested(int n) {
/* threenested demonstrates O(n**3) behavior
pre: n is a positive integer
post: returns count # iterations*/
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
count++; // # this line executes
}
}
}
return (count);
}
int powern(int n) {
/* powern demonstrates O(2**n)) behavior
pre: n is a positive integer
pot: returns count # iterations*/
int count = 0;
for (int i = 0; i < pow(2, n); i++) {
count++; // # this line executes
}
return (count);
}
int halving(int n) {
/* demonstrates O(log(int n)) behavior
pre: n is a positive integer
pot: returns count # iterations*/
int count = 0;
int loopcounter = n;
while (loopcounter > 1) {
count++; // # this line executes
loopcounter = loopcounter / 2; // integer division
}
return (count);
}
int loopincrementn(int n) {
/* demonstrates O(n) behavior
pre: n is a positive integer
post: returns count # iterations */
int count = 0;
int s = 10;
int add = 2;
for (int i = s; i < n; i = i + add) {
count++; // # this line executes
// cout << count << endl;
}
return (count);
}
int main() {
int n;
cout << "\nPlease enter n: ";
cin >> n;
while (n > 0) {
// execution based on # inputs
cout << "\nfor n = " << n << ":" << endl;
cout << "\tlooponconstant1(" << n << ") = " << looponconstant1(n) << endl;
cout << "\tlooponconstant4(" << n << ") = " << looponconstant4(n) << endl;
cout << "\tloopn(" << n << ") = " << loopn(n) << endl;
cout << "\tsequential(" << n << ") = " << sequential(n) << endl;
cout << "\tnestedconstant(" << n << ") = " << nestedconstant(n) << endl;
cout << "\ttwonested(" << n << ") = " << twonested(n) << endl;
cout << "\tthreenested(" << n << ") = " << threenested(n) << endl;
cout << "\tpowern(" << n << ") = " << powern(n) << endl;
cout << "\thalving(" << n << ") = " << halving(n) << endl;
cout << "\tloopincrementn(" << n << ") = " << loopincrementn(n) << endl;
cout << "\nPlease enter another n: ";
cin >> n;
}\
return 0;
}