Skip to content

Instantly share code, notes, and snippets.

@thasan3003
Last active December 2, 2018 13:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thasan3003/05d0301b76bcc583ffea815533aaf7c0 to your computer and use it in GitHub Desktop.
Save thasan3003/05d0301b76bcc583ffea815533aaf7c0 to your computer and use it in GitHub Desktop.
Insertion Sort in C/C++
/*
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