-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsertion-sort_recursive.cpp
62 lines (56 loc) · 1.05 KB
/
Insertion-sort_recursive.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
/*Presented by:
Sakshi Jain
14/5017
*/
#include<iostream>
#include<conio.h>
int counter = 0;
int shiftVacRec(int a[], int vacant, int x);
void insertionsort(int a[],int n);
int shiftVacRec(int a[], int vacant, int x)
{
int loc;
if(vacant == 0)
loc=vacant;
else if(a[vacant-1] <= x)
{
counter++;
loc=vacant;
}
else
{
a[vacant] = a[vacant-1];
loc = shiftVacRec(a,vacant-1,x);
counter++;
}
return loc;
}
void insertionsort(int a[],int n)
{
int xindex;
for (xindex=1;xindex<n;xindex++)
{
int current = a[xindex];
int loc = shiftVacRec(a,xindex,current);
a[loc] = current;
}
return;
}
void main()
{
int n;
cout << "\nEnter the size of the array: ";
cin >> n;
int * a = new int[n];
cout <<"Enter 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 total no. of comparisons are: " << counter;
getch();
}