Skip to content

Commit

Permalink
Added C++ code for Insertion Sort
Browse files Browse the repository at this point in the history
  • Loading branch information
Prince Siwal authored and sangamcse committed Sep 30, 2018
1 parent ec9b647 commit 54b23ea
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions insertion_sort/C++/insertion_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include<bits/stdc++.h>
using namespace std;
void insertionsort(int arr[],int n)
{
// Fuction to do insertion sort.
for(int i=1;i<10;i++)
{
int temp;
temp=arr[i];
int x;
for(int j=i;j>=0;j--)
{
/* Move elements of arr[0..i-1], that are
greater, to one position ahead
of their current position */
if(temp<=arr[j])
{
arr[j]=arr[j-1];
x=j;
}
}
arr[x]=temp;
}
}
int main()
{
int arrsize;
arrsize=6;
int arr[arrsize]={6,1,2,4,5,3};
insertionsort(arr,arrsize);
for(int i=0;i<arrsize;i++)
{
cout<<arr[i]<<" ";
}
return 0;
}

0 comments on commit 54b23ea

Please sign in to comment.