Last active
December 2, 2018 13:07
-
-
Save thasan3003/05d0301b76bcc583ffea815533aaf7c0 to your computer and use it in GitHub Desktop.
Insertion Sort in C/C++
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
/* | |
Md Tahmid Hasan | |
CSE, BSMRSTU | |
tahmidhasan3003 | |
*/ | |
#include<bits/stdc++.h> | |
using namespace std; | |
void insertion_sort(int *a,int s) | |
{ | |
int pos; | |
for(int i=1;i<=s;i++) | |
{ | |
pos=i; | |
while((pos>0)&&(a[pos]<a[pos-1])) | |
{ | |
a[pos]+=a[pos-1]; | |
a[pos-1]=a[pos]-a[pos-1]; | |
a[pos]-=a[pos-1]; | |
pos--; | |
} | |
} | |
} | |
int main() | |
{ | |
int arr[1000],n; | |
cout<<"How many element(s):\n"; | |
cin>>n; | |
cout<<"Inter the elements:\n"; | |
for(int i=0;i<n;i++) | |
cin>>arr[i]; | |
insertion_sort(arr,n); | |
cout<<"After sorting:\n"; | |
for(int i=0;i<n;i++) | |
cout<<arr[i]<<" "; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment