-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionSort.cpp
More file actions
162 lines (101 loc) · 2.95 KB
/
Copy pathselectionSort.cpp
File metadata and controls
162 lines (101 loc) · 2.95 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
#include <iostream>
using namespace std;
class libro {
private:
string titolo;
public:
libro(string titolo) : titolo(titolo) {}
const string& get_titolo() const {
return titolo;
}
bool operator < (libro& secondo){
return this->titolo > secondo.get_titolo();
}
};
class compound {
private:
int src;
public:
compound(int src) : src(src) {}
const int& get_src() const {
return src;
}
bool operator < (compound& secondo){
return this->src > secondo.get_src();
}
};
template <class T> class scaffale {
private:
T* insieme;
public:
scaffale(T* insieme) : insieme(insieme) {}
const T* get_insieme() const {
return insieme;
}
};
template <class T> const T* get_insieme() {}
ostream& operator <<(ostream& out, compound& obj){
out << obj.get_src();
return out;
}
ostream& operator<<(ostream& out, const libro& obj) {
out << obj.get_titolo();
return out;
}
//typename
template <class T>/*segnaposto*/ void selectionSort(T *arr , size_t n){ // uso template per indicsre la presenza di una funzione standard
for (size_t rimanenti = n - 1 ; rimanenti > 0; rimanenti--){
size_t max = 0;
for(size_t i = 1; i <= rimanenti; i++){
if(arr[max] < arr[i]){ // una volta selezionato il valore controllo se uìquesto e minore o maggiore del sottoarray generato
max = i;
}
}
if( max != rimanenti){
swap(arr[max], arr[rimanenti]);
}
}
}
template <class T> bool compare(T x, T y) {
if constexpr (is_pointer <T> :: value) //constexpr lavorano sia con valore che il suo ptr
return *x > *y;
else
return x > y;
}
int main(){
int arr[] = { 4,2,1,6,0};
selectionSort(arr, 5);
for(auto val : arr){
cout << " " << val;
}
cout << endl;
double arr2[] = { 4.6, 5.7, 6.9, 1.2, 9.5};
selectionSort(arr2, 5);
for(auto val : arr2){
cout << " " << val;
}
cout << endl;
compound arr3[] = {compound(1), compound(0), compound(4)};
selectionSort(arr3, 3);
for(auto val : arr3){
cout << " " << val;
}
cout << endl;
cout << endl;
compound** test = new compound* [3];
test[0] = new compound(3);
test[1] = new compound(6);
test[2] = new compound(9);
libro** test2 = new libro* [3];
test2[0] = new libro("primo");
test2[1] = new libro("secondo");
test2[2] = new libro("terzo");
scaffale<compound*> scaffale_compound(test);
cout << *(scaffale_compound.get_insieme()[1]) << endl;
scaffale<libro*> scaffale_libro(test2);
cout << *(scaffale_libro.get_insieme()[2]) << endl;
int a = 10;
int b = 12;
compare(a, b);
return 0;
}