-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsertion-sort_iterative.cpp
66 lines (59 loc) · 1.07 KB
/
Insertion-sort_iterative.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
65
66
/*Presented by:
Sakshi Jain
14/5017
*/
#include<iostream>
#include<conio.h>
int count =0;
int shiftVac(int a[],int xindex, int val);
void insertionsort(int a[],int n);
int shiftVac(int a[],int xindex, int val)
{
int i;
int loc;
i = xindex;
loc = 0;
while(i>0)
{
if(a[i-1]<= val)
{
count++;
loc = i;
break;
}
else
count++;
a[i] = a[i-1];
i--;
}
return loc;
}
void insertionsort(int a[],int n)
{
int xindex;
for (xindex=1;xindex<n;xindex++)
{
int current = a[xindex];
int loc = shiftVac(a,xindex,current);
a[loc] = current;
}
return;
}
void main()
{
int n;
cout << "\n Enter the size of the array: ";
cin >> n;
int* a = new int[n];
cout <<"\nEnter the elements of an unsorted array: ";
for (int i=0;i<n;i++)
cin >> a[i];
insertionsort(a,n);
cout << "New array is : " << endl;
for(int j=0; j<n ; j++)
{
cout << " "<< a[j];
}
cout <<"\nThe no.of comparisons are:" << count;
getch();
}