Skip to content

Insertion sort in CPP added #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Sorting/Insertion Sort/insertion_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//insertion sort:it is a technique in which we start from 2nd element and compare it with the first element and put it in its proper place.
//this sorting technique is better than bubble sort and selection sort
//time complexity:O(n^2)
//space complexity:O(1)

#include<bits/stdc++.h>
using namespace std;

void insertion_sort(int arr[],int n){
int i,j,key;
for(int i=1;i<n;i++)
{
key = arr[i];
j=i-1;
while(j>=0 and arr[j]>key){
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=key;
}
}

void printing(int arr[],int n){
for(int i=0;i<n;i++)
cout<<arr[i]<<"";
cout<<endl;
}

int main(){
int arr[]={9,8,7,5,6,4,2,1,3};
int i;
int n=sizeof(arr)/sizeof(arr[i]);

insertion_sort(arr,n);
printing(arr,n);
return 0;
}
Binary file added Sorting/Insertion Sort/insertion_sort.exe
Binary file not shown.