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