-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertSort.cpp
More file actions
40 lines (37 loc) · 783 Bytes
/
InsertSort.cpp
File metadata and controls
40 lines (37 loc) · 783 Bytes
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
#ifndef __INSTSOT_CPP__
#define __INSTSOT_CPP__
#include "Sort.h"
#include <stdlib.h>
template<typename T>
long long Sort::InsertSort(T* t_, const size_t n, const _FLAG flag){
size_t i;
T* t1 = NULL; //compared item
T* t2 = NULL;
T temp;
long long step = 0;
for (i = 1; i < n; i++){
t1 = (t_ + i);
t2 = (t_ + i - 1);
if (((flag == POS) && ( *t1 >= *t2)) || ((flag == NEG) && (*t1 <= *t2))){
continue;
} //t2 free
temp = *t1;
while (((*t2 > temp) && (flag == POS)) || ((*t2 < temp) && (flag == NEG))){
*(t2 + 1) = *t2;
step++;
if (t2 == t_){
step++;
break;
}
--t2;
}
if ((t2 == t_)&& ( ((flag == POS)&&(*t_ > temp))||((flag == NEG)&&(*t_ < temp)) )){
*t2 = temp;
}
else{
*(t2 + 1) = temp;
}
}
return step;
}
#endif