-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathINSERTIO.CPP
More file actions
60 lines (57 loc) · 807 Bytes
/
INSERTIO.CPP
File metadata and controls
60 lines (57 loc) · 807 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<bits/stdc++.h>
using namespace std;
void insertionsort(int x[],int n)
{
int temp=0,i=0,j=0;
for(i=1;i<n;i++)
{
temp=x[i];
j=i-1;
while(temp<x[j]&&j>=0)
{
x[j+1]=x[j];
j--;
}
x[j+1]=temp;
}
}
void insertionsort1(int x[],int n)
{
int temp=0,i=0,j=0;
for(i=1;i<n;i++)
{
temp=x[i];
j=i-1;
while(temp>x[j]&&j>=0)
{
x[j+1]=x[j];
j--;
}
x[j+1]=temp;
}
}
int main()
{
//clrscr();
int a[100],n=0,i=0;
cout<<"Enter number of elements in array";
cin>>n;
cout<<"Enter the elements";
for(int i=0;i<n;i++)
cin>>a[i];
cout<<"The old array is\n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<"\n\n";
insertionsort(a,n);
cout<<"The ascending order is\n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<"\n\n";
insertionsort1(a,n);
cout<<"The descending order is\n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
//getch();
}