Skip to content

Commit ea338f8

Browse files
Add files via upload
1 parent abf7725 commit ea338f8

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

insertion_sort.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include<iostream>
2+
using namespace std;
3+
int main ()
4+
{
5+
int n;
6+
cout << "Enter number of elements: ";
7+
cin >> n;
8+
int *arr = new int [n];
9+
cout<<"\nEnter "<< n <<" elements\n";
10+
for(int i = 0; i < n; i++)
11+
cin >> arr[i];
12+
cout<<"\nOriginal List\n";
13+
for(int i=0;i<n;i++)
14+
cout <<arr[i]<<" ";
15+
for(int i=1; i<n; i++)
16+
{
17+
int temp = arr[i];
18+
int j= i-1;
19+
while(j>=0 && temp <= arr[j])
20+
{
21+
arr[j+1] = arr[j];
22+
j = j-1;
23+
}
24+
arr[j+1] = temp;
25+
}
26+
cout<<"\nSorted List in Ascending Order\n";
27+
for(int i=0;i<n;i++)
28+
cout <<arr[i]<<" ";
29+
for(int i=1; i<n; i++)
30+
{
31+
int temp = arr[i];
32+
int j= i-1;
33+
while(j>=0 && temp >= arr[j])
34+
{
35+
arr[j+1] = arr[j];
36+
j = j-1;
37+
}
38+
arr[j+1] = temp;
39+
}
40+
cout<<"\nSorted List in Descending Order\n";
41+
for(int i=0;i<n;i++)
42+
cout <<arr[i]<<" ";
43+
}

0 commit comments

Comments
 (0)