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: https://www.youtube.com/watch?v=g_xesqdQqvA&list=PLc_Ps3DdrcTsizjAG5uMhpoDfhDmxpOzv 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 - https://www.facebook.com/CodeWithDerrick/ Insta - https://www.instagram.com/codewithderrick/ Twitter - https://twitter.com/codewithderrick LinkedIn - https://www.linkedin.com/in/derricksherrill/ GitHub - https://github.com/Derrick-Sherrill 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])) https://github.com/Derrick-Sherrill/Python-Algorithms-YouTube-Series/blob/master/3-insertion_sort.py 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: https://github.com/Derrick-Sherrill/DerrickSherrill.com Check out my website: https://www.derricksherrill.com/ 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!