From 52e4c1981ed3ed7b6f36519a4ac0f8078b2e0c95 Mon Sep 17 00:00:00 2001 From: Tanya Date: Mon, 4 Oct 2021 19:47:56 +0530 Subject: [PATCH 1/4] Added post for Insertion sort --- _posts/2021-10-04-insertion-sort.md | 111 ++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 _posts/2021-10-04-insertion-sort.md diff --git a/_posts/2021-10-04-insertion-sort.md b/_posts/2021-10-04-insertion-sort.md new file mode 100644 index 0000000..c1ba702 --- /dev/null +++ b/_posts/2021-10-04-insertion-sort.md @@ -0,0 +1,111 @@ +---- + title: "Insertion Sort" + description: "Insertion sort is a simple sorting technique. Here in this post, first we will see the algorithm, then we will see an example of its working, we will also have a look on its implementation in C++ language, then in the last we will discuss its other characteristics." + category: dsa + image: /assets/images/categories/algorithm.jpg + keywords: # Keywords your want to ad for SEO + - Mentor + - Mentro + - Mentorship + author: TD-17 + permalink: /dsa/:title/ + tags: + - sorting algorithm + - dsa +---- + +## Insertion Sort +``` +Insertion sort is a simple sorting algorithm. Here, the list of elements is virtually divided into sorted and unsorted parts. Element from unsorted side is taken and placed in the appropriate position in sorted side of the list. +``` + +## Algorithm +``` +1. Assume the first element to be already sorted. +2. Pick the next element from unsorted array and compare it with the elements from sorted array. +3. Shift the greater elements towards the right and insert the picked element in the appropriate position in sorted array. +4. Repeat the process until the array is completely sorted. +``` + +## Working of Insertion sort +``` +Let's take an example of unsorted array: +Array = {12, 32, 29, 9, 34, 21, 46, 50} + +//We will see sorting in ascending order. + +//First two values will be compared. +//They are already in sorted order, so the array will remain same. +Array = {12, 32, 29, 9, 34, 21, 46, 50} + +//For now, 12 is in sorted array. 29 will be compared to 32. +//29 is smaller than 32. So, they will be swaped. +Array = {12, 29, 32, 9, 34, 21, 46, 50} + +//Now 12 and 29 are in sorted array. Now 32 will be compared to 9. +//32 is greater than 9, they will be swaped. +Array = {12, 29, 9, 32, 34, 21, 46, 50} + +//29 and 9 are unsorted, so they will be swaped. +Array = {12, 9, 29, 32, 34, 21, 46, 50} + +//Again, 12 is greater than 9. So we will swap them again. +Array = {9, 12, 29, 32, 34, 21, 46, 50} + +//Now we have 9, 12 and 29 in sorted array. This process will be continued until all the elements get sorted. +``` + +## Implementation of Insertion sort in C++. +``` +#include +using namespace std; + +void insertionSort(int arr[], int n) +{ + int i, j, value; + for (i = 1; i < n; i++) + { + value = arr[i]; + j = i - 1; + + //Shifting elements of sorted array, which are greater than value, towards right. + while (j >= 0 && arr[j] > value) + { + arr[j + 1] = arr[j]; + j = j - 1; + } + arr[j + 1] = value; + } + + //Printing sorted array. + for (i = 0; i < n; i++) + cout << arr[i] << " "; +} + +int main() +{ + int n; + + //'n' is the number of elements in an array. + cin >> n; + + int arr[n]; + + //Taking elements in the array 'arr'. + for(int i = 0; i < n; i++) + { + cin >> arr[i]; + } + + //Calling insertionSort function. + insertionSort(arr, n); + + return 0; +} +``` + + + + + + From 91c12bce929453de1026d52d041152102277ecf0 Mon Sep 17 00:00:00 2001 From: Tanya Date: Mon, 4 Oct 2021 20:20:01 +0530 Subject: [PATCH 2/4] added some changes --- _posts/2021-10-04-insertion-sort.md | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/_posts/2021-10-04-insertion-sort.md b/_posts/2021-10-04-insertion-sort.md index c1ba702..7217261 100644 --- a/_posts/2021-10-04-insertion-sort.md +++ b/_posts/2021-10-04-insertion-sort.md @@ -15,17 +15,14 @@ ---- ## Insertion Sort -``` Insertion sort is a simple sorting algorithm. Here, the list of elements is virtually divided into sorted and unsorted parts. Element from unsorted side is taken and placed in the appropriate position in sorted side of the list. -``` + ## Algorithm -``` 1. Assume the first element to be already sorted. 2. Pick the next element from unsorted array and compare it with the elements from sorted array. 3. Shift the greater elements towards the right and insert the picked element in the appropriate position in sorted array. 4. Repeat the process until the array is completely sorted. -``` ## Working of Insertion sort ``` @@ -68,7 +65,7 @@ void insertionSort(int arr[], int n) value = arr[i]; j = i - 1; - //Shifting elements of sorted array, which are greater than value, towards right. + //Shifting elements of sorted array, which are greater than value towards right. while (j >= 0 && arr[j] > value) { arr[j + 1] = arr[j]; @@ -104,6 +101,21 @@ int main() } ``` +## Time Complexity +The worst case time complexity of Insertion sort is O(n^2). +The average case time complexity of Insertion sort is O(n^2) +The best case time complexity of Insertion sort is O(n). +Insertion sort takes minimum timw when the arrray is already sorted. + +## Sorting In Place +Insertion sort places the element from unsorted list to sorted list. It does not take extra space to sort the elements. So here, sorting is done in place. + +## Stable +Yes, Insertion sort is stable. + +## Auxiliary Space +The extra space required by the insertion sort is constant i.e., O(1). + From f4b96fca5138627ae119263ba00faa7eff81a17f Mon Sep 17 00:00:00 2001 From: Tanya Dixit <61655940+TD-17@users.noreply.github.com> Date: Tue, 5 Oct 2021 11:51:22 +0530 Subject: [PATCH 3/4] Completed required changes. --- _posts/2021-10-04-insertion-sort.md | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/_posts/2021-10-04-insertion-sort.md b/_posts/2021-10-04-insertion-sort.md index 7217261..c7d0eed 100644 --- a/_posts/2021-10-04-insertion-sort.md +++ b/_posts/2021-10-04-insertion-sort.md @@ -3,7 +3,7 @@ description: "Insertion sort is a simple sorting technique. Here in this post, first we will see the algorithm, then we will see an example of its working, we will also have a look on its implementation in C++ language, then in the last we will discuss its other characteristics." category: dsa image: /assets/images/categories/algorithm.jpg - keywords: # Keywords your want to ad for SEO + keywords: - Mentor - Mentro - Mentorship @@ -14,17 +14,22 @@ - dsa ---- + ## Insertion Sort + Insertion sort is a simple sorting algorithm. Here, the list of elements is virtually divided into sorted and unsorted parts. Element from unsorted side is taken and placed in the appropriate position in sorted side of the list. ## Algorithm + 1. Assume the first element to be already sorted. 2. Pick the next element from unsorted array and compare it with the elements from sorted array. 3. Shift the greater elements towards the right and insert the picked element in the appropriate position in sorted array. 4. Repeat the process until the array is completely sorted. + ## Working of Insertion sort + ``` Let's take an example of unsorted array: Array = {12, 32, 29, 9, 34, 21, 46, 50} @@ -52,7 +57,9 @@ Array = {9, 12, 29, 32, 34, 21, 46, 50} //Now we have 9, 12 and 29 in sorted array. This process will be continued until all the elements get sorted. ``` + ## Implementation of Insertion sort in C++. + ``` #include using namespace std; @@ -101,19 +108,27 @@ int main() } ``` + ## Time Complexity + The worst case time complexity of Insertion sort is O(n^2). The average case time complexity of Insertion sort is O(n^2) The best case time complexity of Insertion sort is O(n). -Insertion sort takes minimum timw when the arrray is already sorted. +Insertion sort takes minimum time when the array is already sorted. + ## Sorting In Place + Insertion sort places the element from unsorted list to sorted list. It does not take extra space to sort the elements. So here, sorting is done in place. + ## Stable + Yes, Insertion sort is stable. + ## Auxiliary Space + The extra space required by the insertion sort is constant i.e., O(1). From 8daf39aed9cdf88384c9027ded62029059707d4b Mon Sep 17 00:00:00 2001 From: Tanya Dixit <61655940+TD-17@users.noreply.github.com> Date: Wed, 6 Oct 2021 11:25:16 +0530 Subject: [PATCH 4/4] Added author --- _authors/tanya.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 _authors/tanya.md diff --git a/_authors/tanya.md b/_authors/tanya.md new file mode 100644 index 0000000..b0b8ef0 --- /dev/null +++ b/_authors/tanya.md @@ -0,0 +1,4 @@ +--- +author: tanya +permalink: /author/tanya/ +---