From ea50acb609d15752176c90abf8af10611df867dc Mon Sep 17 00:00:00 2001 From: Sudeepam <31586878+Sudeepam97@users.noreply.github.com> Date: Wed, 18 Oct 2017 02:02:52 +0530 Subject: [PATCH 1/2] Update InsertionSort.c --- sorting/InsertionSort.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/sorting/InsertionSort.c b/sorting/InsertionSort.c index 4f3e749e..fcdb05d0 100644 --- a/sorting/InsertionSort.c +++ b/sorting/InsertionSort.c @@ -2,28 +2,34 @@ #incude #define MAX 20 - +//i and j act as counters +//arraySort is the array that is to be sorted +//elmtToInsert will be the element that we will be trying to move to its correct index in the current iteration int main() { int i, elmtToInsert , j , arraySort[MAX] = {0}; - for(i = 1 ; i < MAX ; i++) - { - elmtToInsert = arraySort[i]; - j = i - 1 ; + for(i = 1 ; i < MAX ; i++) //This array is being sorted in the ascending order. + { + elmtToInsert = arraySort[i]; //Pick up the ith indexed element of the array. It will be the elmtToInsert. + j = i - 1 ; - while( j >= 0 && elmtToInsert < arraySort[j]) - { - arraySort[j+1] = arraySort[j]; - j--; - } + while(j >= 0 && elmtToInsert < arraySort[j]) /*compare it with each (i-1)th, (i-2)th... max 0th element, till the correct + position of the elmtToInsert, where it is finally greater than the element just + before it, is found */ + { + // You'll enter the loop if the elmtToInsert is less than the element just before it. + + arraySort[j+1] = arraySort[j]; /*shift the current element one place forward to create room for insertion of the + elmtToInsert when the correct position is found */ + j--; + } + //when we exit the loop, j+1 will be the index of the correct position of the elmtToInsert - arraySort[j+1] = elmtToInsert ; + arraySort[j+1] = elmtToInsert ; //'insert' the elmtToInsert into its correct position } - - - + return EXIT_SUCCESS; } From 41f029253146088e628562321781ccd72a93eba6 Mon Sep 17 00:00:00 2001 From: Sudeepam <31586878+Sudeepam97@users.noreply.github.com> Date: Wed, 18 Oct 2017 02:05:57 +0530 Subject: [PATCH 2/2] Update InsertionSort.c --- sorting/InsertionSort.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sorting/InsertionSort.c b/sorting/InsertionSort.c index fcdb05d0..9e5da501 100644 --- a/sorting/InsertionSort.c +++ b/sorting/InsertionSort.c @@ -21,13 +21,13 @@ int main() { // You'll enter the loop if the elmtToInsert is less than the element just before it. - arraySort[j+1] = arraySort[j]; /*shift the current element one place forward to create room for insertion of the - elmtToInsert when the correct position is found */ + arraySort[j+1] = arraySort[j]; //shift the current element one place forward to create room for insertion of the elmtToInsert j--; } //when we exit the loop, j+1 will be the index of the correct position of the elmtToInsert arraySort[j+1] = elmtToInsert ; //'insert' the elmtToInsert into its correct position + }