filmov
tv
Insertion Sort Algorithm Explained (Full Code Included) - Python Algorithm Series for Beginners
Показать описание
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!
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!!
--- Channel FAQ --
What text editor do you use?
What Equipment do you use to film videos?
What computer do you use/desk setup?
What editing software do you use?
Premiere Pro for video editing
Photoshop for images
After Effects for animations
Do I have any courses available?
Yes & always working on more!
Where do I get my music?
I get all my music from the copyright free Youtube audio library
Let me know if there's anything else you want answered!
-------------------------
Always looking for suggestions on what video to make next -- leave me a comment with your project! Happy Coding!
Комментарии