Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
test_random_search.C
Go to the documentation of this file.
1
2/* Aleph-w
3
4 / \ | | ___ _ __ | |__ __ __
5 / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6 / ___ \| | __/ |_) | | | |_____\ V V / version 1.9c
7 /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8 |_|
9
10 This file is part of Aleph-w library
11
12 Copyright (c) 2002-2018 Leandro Rabindranath Leon
13
14 This program is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <https://www.gnu.org/licenses/>.
26*/
27# include <iostream>
28# include <tpl_sort_utils.H>
29
30# include <ctime>
31# include <cstdlib>
32# include <cassert>
33using namespace std;
34
35struct Cmp
36{
37 bool operator () (Dlink * p, Dlink * q) const
38 {
39 return (static_cast<Dnode<int>*>(p)->get_data() <
40 static_cast<Dnode<int>*>(q)->get_data());
41 }
42};
43
44
45int main(int argc, char *argv[])
46{
47 int n = 1000;
48 if (argc > 1)
49 {
50 try { n = stoi(argv[1]); }
51 catch (...) { n = 1000; }
52 }
53
54 // Validate input to prevent invalid operations
55 if (n <= 0)
56 {
57 cerr << "Error: n must be a positive integer." << endl;
58 return 1;
59 }
60
61 unsigned int t = std::time(0);
62
63 if (argc > 2)
64 {
65 try { t = static_cast<unsigned int>(stoul(argv[2])); }
66 catch (...) { t = std::time(0); }
67 }
68
69 srand(t);
70
71 cout << argv[0] << " " << n << " " << t << endl;
72
73 Dnode<int> list;
74
75 for (int i = 0; i < n; ++i)
76 {
77 int value = 1 + (int) (1.0 * n * (rand() / (RAND_MAX + 1.0)));
78
79 list.append(new Dnode<int>(value));
80 }
81
82 for (Dnode<int>::Iterator it(list); it.has_curr(); it.next())
83 cout << it.get_curr()->get_data() << " ";
84 cout << endl;
85
86# ifdef nada
87 if (Aleph::random_search<int, Cmp>(list, n/2) != NULL)
88 cout << n/2 << " se encuentra en la lista" << endl;
89 else
90 cout << n/2 << " no se encuentra en la lista" << endl;
91# endif
92
93 assert(list.check());
94
95 if (Aleph::random_search(list, n/2) != NULL)
96 cout << n/2 << " se encuentra en la lista" << endl;
97 else
98 cout << n/2 << " no se encuentra en la lista" << endl;
99
100 assert(list.check());
101
102 for (Dnode<int>::Iterator it(list); it.has_curr(); it.next())
103 cout << it.get_curr()->get_data() << " ";
104 cout << endl;
105
106 assert(list.check());
107
108 Dnode<int> * ptr = Aleph::random_select <int> (list, n/2);
109
110 cout << "El elemento " << n/2 << " es: " << ptr->get_data() << endl;
111
113
114}
Iterator on a list of Dnode objects.
Definition tpl_dnode.H:261
Node belonging to a double circular linked list with header node.
Definition tpl_dnode.H:106
T & get_data() noexcept
Return a modifiable reference to the data contained in the node.
Definition tpl_dnode.H:232
long random_search(T *a, const T &x, const long l, const long r, const Compare &cmp=Compare())
Random search for an element in an array.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
STL namespace.
bool operator()(Dlink *p, Dlink *q) const
Comprehensive sorting algorithms and search utilities for Aleph-w.