-
Notifications
You must be signed in to change notification settings - Fork 168
/
Insertion_Sorting.cpp
46 lines (45 loc) · 992 Bytes
/
Insertion_Sorting.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
#include <iostream>
#include <math.h>
#include <climits>
using namespace std;
int main()
{
int n;
cout<<"Enter the number n for an array's element\n";
cin>>n;
int arr[n],key,t;
for (int i = 0; i< n; i++)
{
cin>>arr[i];
}
cout<<"The elements in array are:\n";
for (int i = 0; i< n; i++)
{
cout<<arr[i]<<" ";
}
for (int i = 0; i< n; i++)
{
if (arr[i]>arr[i+1])
{
t=arr[i];
arr[i]=arr[i+1];
arr[i+1]=t;
key=i;
for (int j = i-1; j >=0; j--)
{
if (arr[j]>arr[key])
{
t=arr[j];
arr[j]=arr[key];
arr[key]=t;
key--;
}
}
}
}
cout<<"\nThe final elements in array are:\n";
for (int i = 0; i< n; i++)
{
cout<<arr[i]<<" ";
}
}