-
Notifications
You must be signed in to change notification settings - Fork 0
/
homework_1.cpp
139 lines (123 loc) · 4.55 KB
/
homework_1.cpp
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
/***************************************************************
** Program: homework_1.cpp
** Author: Andy James
** Date: 10/9/2021
** Description: runs tasks for assignemnt 1
** Input: num_array_xxx.txt
** Output: none
***************************************************************/
#include <iostream>
#include <sstream> //needed to read text
#include <algorithm> //needed for max function
#include <limits> //needed for min int
#include <fstream> // needed to read text
#include <vector> // needed to store text
#include <time.h> // needed to time algo
#include <string>
#include <chrono> // needed to time algo
int INT_MIN = -2147483648;
using namespace std;
int max_subarray_enumeration(int* arr, int size);
int max_subarray_iteration(int* arr, int size);
int max_subarray_simplification_delegation(int* arr, int start, int end);
int max_subarray_recursion_inversion(int* arr, int size);
int main(int argc, char* argv[]) {
auto begin = std::chrono::high_resolution_clock::now();
auto stop = std::chrono::high_resolution_clock::now();
int* arr;
int number;
fstream f;
vector<int>numbers;
int answer;
f.open(argv[1]);
while(!f.eof()) {
f >> number;
numbers.push_back(number);
}
arr = new int[numbers.size()];
for(int i = 0; i < numbers.size(); i++) {
arr[i] = numbers[i];
}
begin = std::chrono::high_resolution_clock::now();
answer = max_subarray_enumeration(arr,numbers.size());
stop = std::chrono::high_resolution_clock::now();
cout << "max_subarray_enumeration: " << (float)std::chrono::duration_cast<std::chrono::nanoseconds>(stop-begin).count()/1000000000 << "seconds | answer: " << answer << endl;
begin = std::chrono::high_resolution_clock::now();
answer = max_subarray_iteration(arr,numbers.size());
stop = std::chrono::high_resolution_clock::now();
cout << "max_subarray_iteration: " << (float)std::chrono::duration_cast<std::chrono::nanoseconds>(stop-begin).count()/1000000000<< " seconds | answer: " << answer << endl;
begin = std::chrono::high_resolution_clock::now();
answer = max_subarray_simplification_delegation(arr,0,numbers.size());
stop = std::chrono::high_resolution_clock::now();
cout << "max_subarray_simplification_delegation: " << (float)std::chrono::duration_cast<std::chrono::nanoseconds>(stop-begin).count()/1000000000<< " seconds | answer: " << answer << endl;
begin = std::chrono::high_resolution_clock::now();
answer = max_subarray_recursion_inversion(arr,numbers.size());
stop = std::chrono::high_resolution_clock::now();
cout << "max_subarray_recursion_inversion: " << (float)std::chrono::duration_cast<std::chrono::nanoseconds>(stop-begin).count()/1000000000 << " seconds | answer: " << answer << endl;
}
int max_subarray_enumeration(int* arr, int size) {
int check = 0;
int max = INT_MIN;
for(int i = 0; i < size; i++) {
for(int j = size - 1; j >= i; j--) {
for(int k = i; k <= j; k++) {
check += arr[k];
}
if(check > max) {
max = check;
}
check = 0;
}
}
return max;
}
int max_subarray_iteration(int* arr, int size) {
int check = 0;
int max = INT_MIN;
for(int i = 0; i < size; i++) {
for(int j = i; j < size; j++) {
check += arr[j];
if(check > max) {
max = check;
}
}
check = 0;
}
return max;
}
int max_subarray_simplification_delegation(int* arr, int start, int end) {
if(start == end) {
return arr[start];
}
int middle = (start+end)/2;
int l_sum = INT_MIN;
int r_sum = INT_MIN;
int check = 0;
for(int i = middle; i>= start; i--) {
check += arr[i];
if(check > l_sum) {
l_sum = check;
}
}
check = 0;
for(int i = middle + 1; i <= end; i++) {
check += arr[i];
if(check > r_sum) {
r_sum = check;
}
}
int suf_sum = max(max_subarray_simplification_delegation(arr,start,middle),
max_subarray_simplification_delegation(arr,middle+1,end));
return max(suf_sum,l_sum+r_sum);
}
int max_subarray_recursion_inversion(int* arr, int size) {
int max_sub = INT_MIN;
int check = 0;
for(int i = 0; i < size; i++) {
check = max(0,check + arr[i]);
if(check > max_sub) {
max_sub = check;
}
}
return max_sub;
}