Skip to content

Commit

Permalink
Merge pull request tarunsinghofficial#1830 from SoumyadipGhosh23/main
Browse files Browse the repository at this point in the history
Adding Insertion sort
  • Loading branch information
tarunsinghofficial authored Oct 20, 2021
2 parents 188e346 + 22f9c1f commit 8e9c868
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions C Program/insertionSort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/***Insertion Sort Algorithm***/

//@Author : SoumyadipGhosh23 (GitHub: https://github.com/SoumyadipGhosh23)

#include<stdio.h>
#include<stdlib.h>

void printArr(int* a, int n){
for(int i=0; i<n; i++){
printf("%d ",a[i]);
}
printf("\n");
}

void insertionSort(int* a, int n){
int temp,j;
for(int i =1; i<n; i++){
printf("working on the pass no: %d\n",i);
temp = a[i];
j= i-1;
while(j>=0 && a[j]>temp){
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
}

int main(){
//int arr[] = {2,36,1,31,10,27,11,78,80,45};
int arr[] = {1,2,3,4,5,6,7,8,9,10};
printArr(arr,10);
insertionSort(arr,10);
printArr(arr,10);
}

0 comments on commit 8e9c868

Please sign in to comment.