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