diff --git a/algorithms/C/README.md b/algorithms/C/README.md index 5ca9b8f3..4e43a459 100644 --- a/algorithms/C/README.md +++ b/algorithms/C/README.md @@ -16,6 +16,7 @@ ## Sorting - [Merge Sort](sorting/merge-sort.c) +- [Insertion Sort](sorting/insertion-sort.c) ## Strings - [Count Words](strings/count-words.c) diff --git a/algorithms/C/sorting/insertion-sort.c b/algorithms/C/sorting/insertion-sort.c new file mode 100644 index 00000000..c481295f --- /dev/null +++ b/algorithms/C/sorting/insertion-sort.c @@ -0,0 +1,64 @@ +/* +Program to Print the array in sorted order using Insertion Sort +Insertion Sort: It is a algorithm to sort array like you play cards. +*/ + +#include +#include + +//Function to sort array using insertion sort +void insertion_sort(int a[],int size) +{ + int round,i,shift=0; + // assign 1 to round until the round is less than size run the loop and increment round by 1 + for(round=1;round<=size-1;round++) + { + // take a[round] value in shift + shift=a[round]; + // now assign round to i and until i is greater than 0 run the loop + for(i=round;i>=1;i--) + { + // check whether the a[i-1] is less than the shift variable value if yes then break + if(a[i-1]