Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/libgambatte/src/insertion_sort.h
2 views
1
/***************************************************************************
2
* Copyright (C) 2007 by Sindre Aam�s *
3
* [email protected] *
4
* *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License version 2 as *
7
* published by the Free Software Foundation. *
8
* *
9
* This program is distributed in the hope that it will be useful, *
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12
* GNU General Public License version 2 for more details. *
13
* *
14
* You should have received a copy of the GNU General Public License *
15
* version 2 along with this program; if not, write to the *
16
* Free Software Foundation, Inc., *
17
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18
***************************************************************************/
19
20
#ifndef INSERTION_SORT_H
21
#define INSERTION_SORT_H
22
23
#include <functional>
24
25
template<typename T, class Less>
26
void insertionSort(T *const start, T *const end, Less less) {
27
if (start >= end)
28
return;
29
30
T *a = start;
31
32
while (++a < end) {
33
const T e = *a;
34
35
T *b = a;
36
37
while (b != start && less(e, *(b - 1))) {
38
*b = *(b - 1);
39
b = b - 1;
40
}
41
42
*b = e;
43
}
44
}
45
46
template<typename T>
47
inline void insertionSort(T *const start, T *const end) {
48
insertionSort(start, end, std::less<T>());
49
}
50
51
#endif /*INSERTION_SORT_H*/
52
53