-
Notifications
You must be signed in to change notification settings - Fork 41
Added post for Insertion sort #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
| author: tanya | ||
| permalink: /author/tanya/ | ||
| --- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| ---- | ||
| 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: | ||
| - 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 <iostream> | ||
| 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; | ||
| } | ||
| ``` | ||
|
|
||
|
|
||
| ## 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 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). | ||
|
|
||
|
|
||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leave one line after every sub-heading and you can probably add some pictures demonstrating the above example to make the blog look more intuitive if possible
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@Ruchip16 Will I have to create arrays for demonstration using graphics or I can take any image from google ?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Ruchip16 ! Please review the requested changes in my PR.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, you can take images from google. Just make sure to add credits. |
||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
leave one line after the sub-headings i.e leave one line after ## Insertion Sort heading and follow it for every sub-heading