forked from amanss00/ForNewbies
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertionsort.cpp
More file actions
44 lines (44 loc) · 905 Bytes
/
insertionsort.cpp
File metadata and controls
44 lines (44 loc) · 905 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
#include <iostream>
using namespace std;
void insertionsort(int *a, int n)
{
for (int i = 1; i < n; i++)
{
if (a[i] < a[i - 1])
{
int k = a[i];
int l;
for (l = i; l >= 0; l--)
{
if (k < a[l - 1] && l != 0)
{
a[l] = a[l - 1];
}
else
{
a[l] = k;
break;
}
}
}
}
}
int main()
{
int n;
cout<<"enter no. of elements: ";
cin >> n;
cout<<"Enter the elements: ";
int *a = new int[n];
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
cout<<"The result is: "<<endl;
insertionsort(a, n);
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
delete[] a;
}