Skip to content

Commit 7ee9b04

Browse files
committed
Added insertion-sort algorithm
1 parent fbd13c1 commit 7ee9b04

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Sorting/insertion_sort.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# include <iostream>
2+
using namespace std;
3+
4+
int main(){
5+
cout<<"Enter the length of array"<<endl;
6+
int n;
7+
cin>>n;
8+
int arr[n];
9+
cout<<"Enter the elements of array"<<endl;
10+
for(int i =0; i<n; i++){
11+
cin>>arr[i];
12+
}
13+
14+
for(int i=1; i<n;i++){
15+
int key = arr[i];
16+
int j = i-1;
17+
while (key<arr[j] && j>=0)
18+
{
19+
arr[j+1]=arr[j];
20+
j--;
21+
}
22+
arr[j+1]=key;
23+
}
24+
for (int i = 0; i < n; i++)
25+
{
26+
cout<<arr[i]<<" ";
27+
}
28+
29+
}

0 commit comments

Comments
 (0)