Insertion Sort Algorithm Explained (Full Code Included) - Python Algorithm Series for Beginners

Insertion Sort Algorithm Explained (Full Code Included) - Python Algorithm Series for Beginners


Full Code From This Example:

YouTube Description:

This video is a part of a full algorithm series: In this one we'll cover the insertion sort algorithm. The insertion sort algorithm breaks a list into two sublists, one with sorted values and the other with unsorted values. We move one unsorted value to the sorted sublist, and compare its value to the values to the left. We move it into the correct position by switching, each time the item to the left is larger. #InsertionSort #Python #Algorithm One we have sorted the element, we start the next iteration and sort the next unsorted item in the unsorted sublist. The Insertion sort algorithm has a complexity of O(n^2) because we're still doing comparisons with two nested lists. However, this one is preferable to the bubble and selection sort algorithms. We take the "best" parts of both of those and combine them to insertion sort. If you need more of an idea on how we use the Insertion Sort Algorithm, think of how we organize hands of playing cards. When we draw a new card into our hand, we place it in the front, find the position that it would go to within the hand, and then draw the next. The Insertion Sort Algorithm is attempting to do the same thing. Join The Socials -- Picking Shoutouts Across YouTube, Insta, FB, and Twitter! FB - Insta - Twitter - LinkedIn - GitHub - Thanks so much for the continued support 5440+ subscribers at the time of writing! Very incredible that I get this opportunity to share content about something I'm passionate about. Thank you for your kind words of encouragement and support. You all are awesome! ***************************************************************** Full code from the video: def insertion_sort(list_a): indexing_length = range(1, len(list_a)) for i in indexing_length: value_to_sort = list_a[i] while list_a[i-1] (greater than) value_to_sort and i(greater than)0: list_a[i], list_a[i-1] = list_a[i-1], list_a[i] i = i -1 #Sorry - Youtube doesn't allow angled brackets in the description :( return list_a print(insertion_sort([7,8,9,8,7,6,5,6,7,8,9,8,7,6,5,6,7,8])) Packages (& Versions) used in this video: Python 3.7 Atom Text Editor ***************************************************************** Code from this tutorial and all my others can be found on my GitHub: Check out my website: If you liked the video - please hit the like button. It means more than you know. Thanks for watching and thank you for all your support!! Always looking for suggestions on what video to make next -- leave me a comment with your project! Happy Coding!

Enjoy this content? Consider Subbing to the Youtube channel

Derrick Sherrill

By: Derrick Sherrill

Thanks for visiting my page! I'm working hard to make the best content I can for you. I love watching people learn and teaching others. Happy Coding!

Become a Patreon!