-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortingalgorithms.h
193 lines (158 loc) · 4.65 KB
/
sortingalgorithms.h
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#ifndef SORTINGALGORITHMS_H
#define SORTINGALGORITHMS_H
#include "datamodel.h"
#include <algorithm>
#include <functional>
#include <vector>
#include <QAbstractItemView>
namespace SortingAlgorithms {
class VisualIterator{
private:
std::vector<int>::iterator iter_;
QAbstractItemView* view_;
void highlight_item();
void fade_item();
public:
VisualIterator():
view_(nullptr) {
}
VisualIterator(const std::vector<int>::iterator to_copy)
:iter_(to_copy) {
}
VisualIterator(const VisualIterator& to_copy)
:iter_(to_copy.iter_),
view_(to_copy.view_) {
}
bool operator==(const VisualIterator& to_cmp) {
return iter_ == to_cmp.iter_;
}
bool operator!=(const VisualIterator& to_cmp) {
return iter_ != to_cmp.iter_;
}
VisualIterator& operator++(int) {
++iter_;
return *this;
}
VisualIterator operator++() {
VisualIterator to_return = VisualIterator(*this);
++*this;
return to_return;
}
VisualIterator operator--(int) {
--iter_;
return *this;
}
VisualIterator operator--() {
VisualIterator to_return = VisualIterator(*this);
--*this;
return to_return;
}
VisualIterator operator+=(int offset) {
iter_ += offset;
return *this;
}
VisualIterator operator-=(int offset) {
iter_ -= offset;
return *this;
}
int operator*() {
return *iter_;
}
int* operator->() {
return &(*iter_);
}
void swap (VisualIterator& rhs) {
std::swap(iter_, rhs.iter_);
}
void swap_data(VisualIterator& rhs) {
std::iter_swap(iter_, rhs.iter_);
}
friend VisualIterator operator+(VisualIterator lhs, int rhs) {
return (lhs += rhs);
}
friend VisualIterator operator-(VisualIterator lhs, int rhs) {
return (lhs -= rhs);
}
friend int operator-(const VisualIterator& lhs, const VisualIterator& rhs) {
return lhs.iter_ - rhs.iter_;
}
friend void std::iter_swap(VisualIterator& lhs, VisualIterator& rhs);
};
//std::iter_swap(SortingAlgorithms::VisualIterator& lhs, SortingAlgorithms::VisualIterator& rhs) {
// std::swap(*lhs,*rhs);
//}
template <typename IteratorType ,typename Order>
void BubbleSort(IteratorType begin, IteratorType end, Order order) {
for (IteratorType i = begin; i != end; ++i) {
for (IteratorType j = i + 1; j != end; ++j) {
if (order(*i,*j)) {
std::iter_swap(i,j);
}
}
}
}
// QuickSort helper. Get make <= or >= from > or <.
template<typename Order> struct non_strict_op:
public std::binary_function<typename Order::second_argument_type,
typename Order::first_argument_type,
bool>
{
non_strict_op(Order o): order(o) {}
bool operator()(typename Order::second_argument_type arg1,
typename Order::first_argument_type arg2) const
{
return !order(arg2, arg1);
}
private:
Order order;
};
template<typename Order> non_strict_op<Order> non_strict(Order o) {
return non_strict_op<Order>(o);
}
template<typename T>
T median(T t1, T t2, T t3)
{
if (t1 < t2)
{
if (t2 < t3)
return t2;
else if (t1 < t3)
return t3;
else
return t1;
}
else
{
if (t1 < t3)
return t1;
else if (t2 < t3)
return t3;
else
return t2;
}
}
template <typename IteratorType, typename Order>
void QuickSort(IteratorType first, IteratorType last, Order order) {
if (last - first > 2) {
typedef typename IteratorType::value_type value_type;
IteratorType mid = first + (last - first)/2;
value_type pivot = median(*first, *mid, *(last-1));
IteratorType split1 = std::partition(first, last, std::bind2nd(order, pivot));
IteratorType split2 = std::partition(split1, last, std::bind2nd(non_strict(order), pivot));
QuickSort(first, split1, order);
QuickSort(split2, last, order);
}
}
template <typename IteratorType, typename Order>
void InsertionSort(IteratorType first, IteratorType last, Order order)
{
for (auto i = first; i != last; ++i) {
std::rotate(std::upper_bound(first, i, *i, order), i, i + 1);
}
}
//void MergeSort(DataModel::DataContainer& container) {
//}
//void ShellSort(DataModel::DataContainer& container);
//}
}
#endif // SORTINGALGORITHMS_H