forked from narayancseian/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request narayancseian#45 from batlohiya12/main
Create Insertion sort.cpp
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* C++ Program to implement Insertion Sort using Array */ | ||
|
||
#include<iostream> | ||
|
||
using namespace std; | ||
|
||
int main() | ||
{ | ||
int i,j,n,temp,a[30]; | ||
|
||
cout<<"Enter size of Array :: "; | ||
cin>>n; | ||
cout<<"\nEnter elements to the array :: \n"; | ||
|
||
for(i=0;i<n;++i) | ||
{ | ||
cout<<"\nEnter "<<i+1<<" element :: "; | ||
cin>>a[i]; | ||
} | ||
|
||
for(i=1;i<=n-1;i++) | ||
{ | ||
temp=a[i]; | ||
j=i-1; | ||
|
||
while((temp<a[j])&&(j>=0)) | ||
{ | ||
a[j+1]=a[j]; | ||
j=j-1; | ||
} | ||
|
||
a[j+1]=temp; | ||
} | ||
|
||
cout<<"\nAfter Insertion Sort, Sorted list is :: \n\n"; | ||
for(i=0;i<n;i++) | ||
{ | ||
cout<<a[i]<<" "; | ||
} | ||
cout<<"\n"; | ||
|
||
return 0; | ||
} |