-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionSortObj.cpp
More file actions
69 lines (53 loc) · 1.4 KB
/
Copy pathselectionSortObj.cpp
File metadata and controls
69 lines (53 loc) · 1.4 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
#include <iostream>
using namespace std;
class compound {
private:
int src;
public:
compound(int src) : src(src) {} // Aggiunto costruttore predefinito
const int& get_src() const {
return src;
}
bool operator < (const compound& secondo) const { // Aggiunto const
return this->src < secondo.get_src();
}
};
ostream& operator <<(ostream& out, const compound& obj) { // Aggiunto const
out << obj.get_src();
return out;
}
template <class T>
void selectionSort(T *arr , size_t n){
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]){ // Ordinamento crescente
max = i;
}
}
if(max != rimanenti){
swap(arr[max], arr[rimanenti]);
}
}
}
int main(){
int arr[] = {4,2,1,6,0};
selectionSort(arr, 5);
for(int i = 0; i<5; i++){
cout << arr[i] << " ";
}
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;
return 0;
}