forked from dulalsaurab/sorts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertion-sort - Copy - Copy.cpp
64 lines (58 loc) · 1.1 KB
/
insertion-sort - Copy - Copy.cpp
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
//============================================================================
// Name : insertion-sort.cpp
// Author :
// Date :
// Copyright :
// Description : Implementation of insertion sort in C++
//============================================================================
#include "sort.h"
#include <ostream>
void
InsertionSort::sort(int A[], int size) // main entry point
{
int j=0;
int temp=0;
int num=0;
for (int i=1; i<size; ++i)
{
temp= A[i];
j = i-1;
for(num=0; (j >= 0) && (A[j] > temp); num+=2)
{
A[j+1] = A[j];
j -= 1;
}
A[j+1]=temp;
num_cmps+=num;
}
}
/*int i=0;
int j=0;
int key=0;
{
for (j=1; j<size; j++)
{
key= array[j];
for (i=j-1; (i>=0) && (array[i]<key); i++)
{
array[i+1]= array[i];
}
array[i+1]=key;
}
return;
}
while(A[i]<A[i-1])
{
temp=A[i-1];
A[i-temp] = A[i];
A[i]=temp;
}
int temp=0;
int j = 0;
for(int i=1; i<size; ++i)
{
temp=A[i];
for(j=i-1; j>=0 && A[j]<temp; ++j)
A[j+1]=A[j];
}
A[j+1]=temp; */