forked from martinjrobins/SPH-DEM
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sort.h
executable file
·68 lines (61 loc) · 1.32 KB
/
sort.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
#ifndef SORT_H
#define SORT_H
#include <vector>
template <typename T>
void insertionSort(std::list<T> &v) {
list<CpInfo>::iterator second = v.begin();
second++;
for (list<CpInfo>::iterator i = second;i!=v.end();i++) {
for (list<CpInfo>::iterator j=v.begin();j!=i;j++) {
if (j->key > i->key) {
v.splice(i,v,j);
}
}
}
}
template <typename T1,typename T2>
void insertionSort(std::vector<T1> &v,std::vector<T2> &keys)
{
const int size = v.size();
for(int i=1;i<size;++i)
{
for(int j=0;j<i;++j)
{
if (keys[j]>keys[i])
{
T1 tempv=v[j];
T2 tempkey=keys[j];
v[j]=v[i];
keys[j]=keys[i];
for(int k=i;k>j;--k) { v[k]=v[k-1];keys[k]=keys[k-1]; }
v[j+1]=tempv;
keys[j+1]=tempkey;
}
}
}
}
template <typename T1,typename T2,typename T3>
void insertionSort(std::vector<T1> &v,std::vector<T2> &v2,std::vector<T3> &keys)
{
const int size = v.size();
for(int i=1;i<size;++i)
{
for(int j=0;j<i;++j)
{
if (keys[j]>keys[i])
{
T1 tempv=v[j];
T2 tmpv2=v2[j];
T3 tempkey=keys[j];
v[j]=v[i];
v2[j]=v2[i];
keys[j]=keys[i];
for(int k=i;k>j;--k) { v[k]=v[k-1];v2[k]=v2[k-1];keys[k]=keys[k-1]; }
v[j+1]=tempv;
v2[j+1]=tmpv2;
keys[j+1]=tempkey;
}
}
}
}
#endif