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

Derrick Sherrill
Derrick Sherrill
91 هزار بار بازدید - 5 سال پیش - This video is a part
This video is a part of a full algorithm series: Bubble Sort Algorithm Explained (Full...

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 - Facebook: CodeWithDerrick
Insta - Instagram: codewithderrick
Twitter - Twitter: codewithderrick
LinkedIn - LinkedIn: 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/P...

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/D...

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!!

--- Channel FAQ --

What text editor do you use?
Atom - https://atom.io/

What Equipment do you use to film videos?
Blue Yeti Microphone - https://amzn.to/2PcNj5d
Mic sound shield - https://amzn.to/3bVNkEt
Soundfoam - https://amzn.to/37NV9ci
Camera desk stand - https://amzn.to/3bX8xhm
Box Lights - https://amzn.to/2PanL95
Side Lights - https://amzn.to/37KSNut
Green Screen - https://amzn.to/37SFFnc

What computer do you use/desk setup?
Film on imac (4k screen) - https://amzn.to/37SEu7g
Work on Macbook Pro - https://amzn.to/2HJ5b3G
Video Storage - https://amzn.to/2Pey8sw
Mouse - https://amzn.to/2PhCtv3
Desk - https://amzn.to/37O1Mv1
Chair - https://amzn.to/2uqHE4E

What editing software do you use?
Adobe CC - https://www.adobe.com/creativecloud.html
Premiere Pro for video editing
Photoshop for images
After Effects for animations

Do I have any courses available?
Yes & always working on more!
https://www.udemy.com/user/derrick-sh...

Where do I get my music?
I get all my music from the copyright free Youtube audio library
https://www.youtube.com/audiolibrary/...

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!
5 سال پیش در تاریخ 1398/06/16 منتشر شده است.
91,097 بـار بازدید شده
... بیشتر