Skip to content

Commit

Permalink
Fix Asiatik#87: Added Documentation for Insertion Sort (Asiatik#168)
Browse files Browse the repository at this point in the history
* Fix Asiatik#87: Added Documentation for Insertion Sort

Added a README.md

* Delete README.md

*  Fix Asiatik#87: Added Documentation for Insertion Sort

 Fix Asiatik#87: Added Documentation for Insertion Sort
  • Loading branch information
struong9 authored and tstreamDOTh committed Oct 3, 2018
1 parent fab212e commit 9f3642a
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Sorting/Insertion Sort/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Insertion Sort
Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. However, insertion sort provides several advantages:
- Simple implementation: Jon Bentley shows a three-line C version, and a five-line optimized version[2]
- Efficient for (quite) small data sets, much like other quadratic sorting algorithms
- More efficient in practice than most other simple quadratic (i.e., O(n2)) algorithms such as selection sort or bubble sort
- Adaptive, i.e., efficient for data sets that are already substantially sorted: the time complexity is O(nk) when each element in the input is no more than k places away from its sorted position
- Stable; i.e., does not change the relative order of elements with equal keys
- In-place; i.e., only requires a constant amount O(1) of additional memory space
- Online; i.e., can sort a list as it receives it
#

![Insertion Sort](https://cdn.programiz.com/sites/tutorial2program/files/Insertion-Sort-Algorithm-Programming.jpg)

### Explanation
Suppose, you want to sort elements in ascending as in above figure. Then:
- Step 1: The second element of an array is compared with the elements that appears before it (only first element in this case). If the second element is smaller than first element, second element is inserted in the position of first element. After first step, first two elements of an array will be sorted.
- Step 2: The third element of an array is compared with the elements that appears before it (first and second element). If third element is smaller than first element, it is inserted in the position of first element. If third element is larger than first element but, smaller than second element, it is inserted in the position of second element. If third element is larger than both the elements, it is kept in the position as it is. After second step, first three elements of an array will be sorted.
- Step 3: Similary, the fourth element of an array is compared with the elements that appears before it (first, second and third element) and the same procedure is applied and that element is inserted in the proper position. After third step, first four elements of an array will be sorted.

0 comments on commit 9f3642a

Please sign in to comment.