Hasty Badger
Small UI library (a branch of Turbo Badger)
 All Classes Namespaces Functions Variables Enumerations Enumerator Friends Groups Pages
tb_sort.h
1 // ================================================================================
2 // == This file is a part of Turbo Badger. (C) 2011-2014, Emil SegerÃ¥s ==
3 // == See tb_core.h for more information. ==
4 // ================================================================================
5 
6 #ifndef TB_SORT_H
7 #define TB_SORT_H
8 
9 namespace tb {
10 
11 template<class CONTEXT, class TYPE>
12 static void insertion_sort(TYPE *elements, size_t element_count, CONTEXT context, int(*cmp)(CONTEXT context, const TYPE *a, const TYPE *b))
13 {
14  size_t i, j;
15  for (i = 1; i < element_count; i++)
16  {
17  TYPE value = elements[i];
18  for (j = i; j > 0 && cmp(context, &value, &elements[j - 1]) < 0; j--)
19  elements[j] = elements[j - 1];
20  elements[j] = value;
21  }
22 }
23 
24 } // namespace tb
25 
26 #endif // TB_SORT_H