Skip to content

Commit a779f62

Browse files
authored
Merge pull request kothariji#447 from samprati97/master
Create Insertion Sort.cpp
2 parents 57ba0d3 + 7fe327d commit a779f62

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

Sorting/Insertion Sort.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
void insertionSort(int arr[]) {
5+
int key;
6+
int j = 0;
7+
for (int i = 1; i<5; i++) {
8+
key = arr[i];
9+
j = i - 1;
10+
while (j >= 0 && arr[j] > key) {
11+
arr[j + 1] = arr[j];
12+
j = j - 1;
13+
}
14+
arr[j + 1] = key;
15+
}
16+
17+
}
18+
19+
int main() {
20+
int myarray[5];
21+
cout<<"Enter 5 integers in any order"<<endl;
22+
for (int i = 0; i<5; i++) {
23+
cin>>myarray[i];
24+
}
25+
26+
cout<<"Before Sorting: "<<endl;
27+
for (int i = 0; i<5; i++) {
28+
cout<<myarray[i]<<" ";
29+
}
30+
31+
insertionSort(myarray);
32+
33+
cout<<endl<<"After Sorting: "<<endl;
34+
for (int i = 0; i<5; i++) {
35+
cout<<myarray[i]<<" ";
36+
}
37+
38+
return 0;
39+
}

0 commit comments

Comments
 (0)