diff --git a/contributor_database b/contributor_database index 17e428769..471bb5072 100644 --- a/contributor_database +++ b/contributor_database @@ -21,4 +21,6 @@ palindrome:[Rohit Tanwar](@kriadmin) is_upper_case:[Rohit Tanwar](@kriadmin) is_lower_case:[Rohit Tanwar](@kriadmin) count_by:[Rohit Tanwar](@kriadmin) -difference_by:[Rohit Tanwar](@kriadmin) \ No newline at end of file +insertion_sort:[Meet Zaveri](@meetzaveri) +difference_by:[Rohit Tanwar](@kriadmin) + diff --git a/snippets/insertion_sort.md b/snippets/insertion_sort.md new file mode 100644 index 000000000..6c6625272 --- /dev/null +++ b/snippets/insertion_sort.md @@ -0,0 +1,21 @@ +### insertion_sort + +On a very basic level, an insertion sort algorithm contains the logic of shifting around and inserting elements in order to sort an unordered list of any size. The way that it goes about inserting elements, however, is what makes insertion sort so very interesting! + +```python +def insertionsort(arr): + + for i in range(1, len(arr)): + key = arr[i] + j = i-1 + while j>=0 and key < arr[j]: + arr[j+1] = arr[j] + j -= 1 + arr[j+1] = key +``` + +```python +arr = [7,4,9,2,6,3] +insertionsort(arr) +print('Sorted %s' %arr) # sorted [2, 3, 4, 6, 7, 9] +``` diff --git a/tag_database b/tag_database index f8be5804e..4c4918959 100644 --- a/tag_database +++ b/tag_database @@ -21,4 +21,5 @@ palindrome:string is_upper_case:string is_lower_case:string count_by:list -difference_by:list \ No newline at end of file +difference_by:list +insertion_sort:list