-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble_sort.hpp
More file actions
44 lines (39 loc) · 1.01 KB
/
bubble_sort.hpp
File metadata and controls
44 lines (39 loc) · 1.01 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
#ifndef BUBBLE_SORT_H
#define BUBBLE_SORT_H
template <template <typename> class S, typename T>
void BubbleSort(S<T> &sequence)
{
for (size_t i = sequence.Size() - 1; i > 0; i--)
for (size_t j = 0; j < i; j++)
{
if (*sequence.AtIndex(j) > *sequence.AtIndex(j + 1))
{
T temp = *sequence.AtIndex(j);
*sequence.AtIndex(j) = *sequence.AtIndex(j + 1);
*sequence.AtIndex(j + 1) = temp;
}
}
}
template <typename T, template <class> class S>
void IT_BubbleSort(S<T> &vec)
{
typename S<T>::Iterator it = vec.End();
while (it != vec.Begin())
{
typename S<T>::Iterator prec = vec.Begin(), succ = prec;
++succ;
while (succ != it)
{
if (*prec > *succ)
{
T temp = *prec;
*prec = *succ;
*succ = temp;
}
++prec;
++succ;
}
--it;
}
}
#endif // BUBBLE_SORT_H