-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.cpp
More file actions
59 lines (53 loc) · 1.04 KB
/
SelectionSort.cpp
File metadata and controls
59 lines (53 loc) · 1.04 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
#include <iostream>
using namespace std;
template <class H>
class selectionSort {
H* array;
int dim, start, end;
H addElement(int x);
void swap(int i, int j);
public:
selectionSort(int dim) {
this->dim = dim;
start = 0;
end = this->dim - 1;
array = new H();
for (int i = 0; i < this->dim; i++)
array[i] = addElement(i);
}
void sort();
void stamp();
};
template <class H>
void selectionSort<H>::sort() {
int min;
for (int i = 0; i < end; i++) {
min = i;
for (int j = i + 1; j < dim; j++)
if (array[min] > array[j])
min = j;
swap(i, min);
}
}
template <class H>
void selectionSort<H>::stamp() {
cout << endl;
for (int i = 0; i < dim; i++)
cout << array[i] << " ";
cout << endl
<< endl;
}
template <class H>
void selectionSort<H>::swap(int i, int j) {
H tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
template <class H>
H selectionSort<H>::addElement(int x) {
H temp;
cout << "Add a new element n." << x + 1 << " @> ";
cin >> temp;
return temp;
;
}