forked from CodXCrypt/Cpp-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.cpp
More file actions
45 lines (42 loc) · 789 Bytes
/
InsertionSort.cpp
File metadata and controls
45 lines (42 loc) · 789 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
41
42
43
44
45
#include <iostream>
using namespace std;
template<class T>
void insertionSort(T *arr, int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
temp = arr[i];
j = i-1;
while(j >=0 && arr[j] > temp)
{
arr[j+1] = arr[j];
j--;
}
arr[j+1] = temp;
}
}
int main()
{
int size,i;
cout<<"Enter the size of the array : ";
cin>>size;
int arr[size];
cout<<"\nEnter "<<size<<" elements : ";
for(i=0;i<size;i++)
{
cin>>arr[i];
}
cout<<"\nBefore sorting : \n";
for(i=0;i<size;i++)
{
cout<<arr[i]<<" ";
}
insertionSort(arr,size);
cout<<"\nAfter sorting in ascending order : \n";
for(i=0;i<size;i++)
{
cout<<arr[i]<<" ";
}
return 0;
}