From ad6073753a31b6ff39e653a9b24ec73e51c753e5 Mon Sep 17 00:00:00 2001 From: Viraj Nirbhavane Date: Mon, 30 Aug 2021 18:24:10 +0530 Subject: [PATCH] docs: added insertion sort (#438) --- docs/en/README.md | 7 +++++- docs/en/Sorting/Insertion-Sort.md | 36 +++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 docs/en/Sorting/Insertion-Sort.md diff --git a/docs/en/README.md b/docs/en/README.md index a3a79115..30303782 100644 --- a/docs/en/README.md +++ b/docs/en/README.md @@ -1,15 +1,20 @@ # Algorithms + ## Sorting + - [Bubble Sort](./Sorting/Bubble-Sort.md) - [Merge Sort](./Sorting/Merge-Sort.md) - [Selection Sort](./Sorting/Selection-Sort.md) +- [Insertion Sort](./Sorting/Insertion-Sort.md) ## Strings + - [Palindrome](./Strings/Palindrome.md) -## Searching +## Searching - [Binary Search](./Searching/Binary-Search.MD) ## Others + [How to add new algorithm documentation?](./CONTRIBUTING.md) diff --git a/docs/en/Sorting/Insertion-Sort.md b/docs/en/Sorting/Insertion-Sort.md new file mode 100644 index 00000000..3a0c3481 --- /dev/null +++ b/docs/en/Sorting/Insertion-Sort.md @@ -0,0 +1,36 @@ +# Insertion Sort + +Inserion sort is an **inplace** sorting algorithm meaning it won't take any extra space to sort the array items. It works similar to the way you sort playing cards in your hand. +Consider you are dealt 4 cards one after the other. You pick one and keep in your hand, after that for the next card (key) you compare with the one in your hand and place it accordingly and so on until all the cards are in your hand. + +## Steps + +To sort an array of size n in ascending order: + +1. Iterate from second element, arr[1] to arr[n-1] over the array. +2. Compare the current element (key) to its predecessor. +3. If the key element is smaller than its predecessor, compare it to the elements before. Move the greater elements one position up to make space for the swapped element. + +## Example + +Given array is +**12 11 13 5 6 7** + +Sorted array is +**5 6 7 11 12 13** + +## Implementation + +- [Java](../../../algorithms/Java/sorting/insertion-sort.java) +- [C](../../../algorithms/C/sorting/insertion-sort.c) +- [C++](../../../algorithms/CPlusPlus/Sorting/insertion-sort.cpp) +- [JavaScript](../../../algorithms/JavaScript/src/sorting/insertion-sort.js) +- [Python](../../../algorithms/Python/sorting/insertion_sort.py) + +## Video URL + +[Youtube Video about Insertion Sort](https://www.youtube.com/watch?v=BO145HIUHRg) + +## Others + +[Wikipedia](https://en.wikipedia.org/wiki/Insertion_sort)