Skip to content
Snippets Groups Projects
Commit db88304d authored by Fernando K's avatar Fernando K
Browse files

Remove memory access errors

parent 76fa3609
No related branches found
No related tags found
No related merge requests found
......@@ -53,8 +53,12 @@ int busca_intervalo_0_n_end (vector<int> a, int n, int target) {
}
}
if (n > 0) comp++;
return n > 0 && a[lo] == target ? lo : -1;
if (0 <= lo && lo < n) {
comp++;
return a[lo] == target ? lo : -1;
} else {
return -1;
}
}
int busca_intervalo__1_n (vector<int> a, int n, int target) {
......@@ -201,6 +205,7 @@ int esquerda_intervalo_0_n_1_ceil_hi (vector<int> a, int n, int target) {
}
}
comp++;
return a[hi] >= target ? hi : hi + 1;
}
......@@ -407,12 +412,32 @@ string found_if_in_odd_array(binary_search_function impl) {
return "";
}
vector<int> r (500001);
vector<int> rp_odd (5);
vector<int> rp_even (5);
string found_if_in_odd_random_array(binary_search_function impl) {
int n = 500000 + 1;
for (int i = 0; i < 5; i++) if (impl(r, n, r[rp_odd[i]]) == -1) {
return "false";
}
return "";
}
string found_if_in_even_array(binary_search_function impl) {
vector<int> a { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 };
for (int i = 0; i < 10; i++) if (impl(a, 10, a[i]) == -1) { return "false"; }
return "";
}
string found_if_in_even_random_array(binary_search_function impl) {
int n = 500000;
for (int i = 0; i < 5; i++) if (impl(r, n, r[rp_even[i]]) == -1) {
return "false";
}
return "";
}
string found_left_in_odd_array(binary_search_function impl) {
vector<int> a { 2, 2, 4, 4, 4, 6, 8, 8, 10 };
vector<int> r { 0, 0, 0, 2, 2, 5, 5, 6, 6, 8, 8, 9 };
......@@ -474,22 +499,45 @@ void perform_tests(
test_binary_search_function test_function;
tie(test_name, test_function) = test;
vector<pair<int, string>> results;
int best_comp = 987654321;
cout << " Teste '" << test_name << "'\n";
for (auto implementation : implementations) {
for (int i = 0; i < implementations.size(); i++) {
string implementation_name;
binary_search_function search_function;
tie(implementation_name, search_function) = implementation;
tie(implementation_name, search_function) = implementations[i];
comp = 0;
string success = test_function(search_function);
cout << " " << (success == "" ? "\033[32mAC" : "\033[91mWA") << " " << implementation_name;
if (success != "") cout << ": " << success;
cout << " : " << comp << " comparações" << "\033[0m\n";
string result = string(success == "" ? "AC" : "WA") + " " + implementation_name;
if (success != "") result += ": " + success;
if (comp) result += " : " + to_string(comp) + " comparações";
results.push_back(make_pair(comp, result));
if (comp != 0 && comp < best_comp) { best_comp = comp; }
}
for (int i = 0; i < results.size(); i++) {
cout << " ";
if (results[i].second[0] == 'W') { cout << "\033[91m"; }
else if (results[i].first == best_comp) { cout << "\033[32m"; }
else { cout << "\033[94m"; }
cout << results[i].second << "\033[0m\n";
}
}
}
int main() {
generate(r.begin(), r.end(), rand);
sort(r.begin(), r.end());
for (int i = 0; i < 5; i++) {
rp_even[i] = rand() % 50000;
rp_odd[i] = rand() % 50001;
}
cout << "Buscas" << "\n";
vector<pair<string, binary_search_function>> searches {
......@@ -513,10 +561,52 @@ int main() {
found_if_in_odd_array),
make_pair("Elemento encontrado em vetor par",
found_if_in_even_array),
make_pair("Elemento encontrado em vetor aleatório ímpar",
found_if_in_odd_random_array),
make_pair("Elemento encontrado em vetor aleatório par",
found_if_in_even_random_array),
};
perform_tests(searches, search_tests);
cout << "Buscas usando primeiro elemento a esquerda" << "\n";
vector<pair<string, binary_search_function>> success_searches {
make_pair("Esquerda: Intervalo [0, n), resposta lo",
esquerda_intervalo_0_n_lo),
make_pair("Esquerda: Intervalo [0, n), resposta hi",
esquerda_intervalo_0_n_hi),
make_pair("Esquerda: Intervalo [0, n-1], resposta lo",
esquerda_intervalo_0_n_1_lo),
make_pair("Esquerda: Intervalo [0, n-1], resposta hi",
esquerda_intervalo_0_n_1_hi),
make_pair("Esquerda: Intervalo [0, n-1], variável auxiliar",
esquerda_intervalo_0_n_1_aux),
make_pair("Esquerda: Intervalo [0, n-1], teto, resposta lo",
esquerda_intervalo_0_n_1_ceil_lo),
make_pair("Esquerda: Intervalo [0, n-1], teto, resposta hi",
esquerda_intervalo_0_n_1_ceil_hi),
make_pair("Esquerda: Intervalo [0, n-1], teto, variável auxiliar",
esquerda_intervalo__1_n_lo),
make_pair("Esquerda: Intervalo (-1, n), resposta hi",
esquerda_intervalo__1_n_lo),
make_pair("Esquerda: lower_bound",
esquerda_lower_bound)
};
vector<pair<string, test_binary_search_function>> success_search_tests {
make_pair("Elemento encontrado em vetor ímpar",
found_if_in_odd_array),
make_pair("Elemento encontrado em vetor par",
found_if_in_even_array),
make_pair("Elemento encontrado em vetor aleatório ímpar",
found_if_in_odd_random_array),
make_pair("Elemento encontrado em vetor aleatório par",
found_if_in_even_random_array)
};
perform_tests(success_searches, success_search_tests);
cout << "Primeiro elemento a esquerda" << "\n";
vector<pair<string, binary_search_function>> lefts {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment