From 57835746db23753a17bc712882b25ef58b6f7589 Mon Sep 17 00:00:00 2001 From: PritiShaw Date: Tue, 6 Oct 2020 15:46:29 +0530 Subject: [PATCH 1/3] Priti| insertionSort.c| insertionSort_Add --- Sorting/Insertion Sort/insertionSort.c | 47 ++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Sorting/Insertion Sort/insertionSort.c diff --git a/Sorting/Insertion Sort/insertionSort.c b/Sorting/Insertion Sort/insertionSort.c new file mode 100644 index 0000000..9c25b50 --- /dev/null +++ b/Sorting/Insertion Sort/insertionSort.c @@ -0,0 +1,47 @@ +#include +#include + +void insertionSort(int *arr, int n) +{ + int i, key, j; + for (i = 1; i < n; i++) { + key = arr[i]; + j = i - 1; + + while (j >= 0 && arr[j] > key) { + arr[j + 1] = arr[j]; + j = j - 1; + } + arr[j + 1] = key; + } +} + +void input(int *arr, int n){ + printf("Elements: "); + for (int i=0; i Date: Tue, 6 Oct 2020 15:56:36 +0530 Subject: [PATCH 2/3] Priti| insertionSort.c| Documentation --- Sorting/Insertion Sort/insertionSort.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sorting/Insertion Sort/insertionSort.c b/Sorting/Insertion Sort/insertionSort.c index 9c25b50..f2df0f3 100644 --- a/Sorting/Insertion Sort/insertionSort.c +++ b/Sorting/Insertion Sort/insertionSort.c @@ -33,14 +33,18 @@ void printArray(int *arr, int n){ int main(){ int *arr, NoOfElements; + // Taking number of Elements in the array from the user printf("Number of elements: "); scanf("%d",&NoOfElements); arr=(int*)malloc(NoOfElements*sizeof(int)); + //calling input function which will take elements of array from the user input(arr, NoOfElements); + //callling function insertionSort which will sort the array using insertionSort insertionSort(arr, NoOfElements); printf("Sorted array is: \n"); + //Printing sorted array printArray(arr, NoOfElements); return 0; From d1731e775d0b5ca9b635ca3e0c2eccad19524c07 Mon Sep 17 00:00:00 2001 From: PritiShaw Date: Tue, 6 Oct 2020 16:44:32 +0530 Subject: [PATCH 3/3] Priti| insertionSort.c| Edge case addition --- Sorting/Insertion Sort/insertionSort.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sorting/Insertion Sort/insertionSort.c b/Sorting/Insertion Sort/insertionSort.c index f2df0f3..e99a21c 100644 --- a/Sorting/Insertion Sort/insertionSort.c +++ b/Sorting/Insertion Sort/insertionSort.c @@ -36,6 +36,10 @@ int main(){ // Taking number of Elements in the array from the user printf("Number of elements: "); scanf("%d",&NoOfElements); + if(NoOfElements==0){ + printf("You entered 0. Nothing to Sort\n"); + return 0; + } arr=(int*)malloc(NoOfElements*sizeof(int)); //calling input function which will take elements of array from the user