From a56fccfa6f61e4afeffdcf201bbffd0b0d32bd19 Mon Sep 17 00:00:00 2001 From: Ujjwal <75884061+UG-SEP@users.noreply.github.com> Date: Tue, 20 Apr 2021 17:52:15 +0530 Subject: [PATCH] chore: added insertion sort in C (#235) --- algorithms/C/README.md | 1 + algorithms/C/sorting/insertion-sort.c | 64 +++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 algorithms/C/sorting/insertion-sort.c 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]