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

preview_player
Показать описание
Continuing the Python algorithm series with Selection Sort. Check out the full playlist here:

The Selection Sort Algorithm starts by setting the first item in an unsorted list as the position with the lowest (minimum) value. We then use this to compare each of the items to the right. Whenever we find a new item with a lower value, that becomes our new minimum value and we continue on.

After one iteration of the selection sort algorithm, we create two sublists. One will be for unsorted items and the other will be for sorted ones. We move the minimum item from the unsorted sublist into the last position of our sorted sublist.

After we finish all the iterations, we should be left with only the largest number in our unsorted sublist (which is now sorted to the highest position as it is the highest value.)

The selection sort algorithm has a complexity of O(n^2) still but nearly always outperforms the bubble sort algorithm in situations where writing is important (fewer switches, more efficient per iteration).

If you had any questions about the selection sort algorithm, or any of the other algorithms in this series - please let me know in the comments below!

Join The Socials

Thanks so much for all the support! 5390+ subscribers at the time of writing. Super awesome. I still get so hyped that I get more than 4 views on a new upload. So thankful for you all. I appreciate all the kind words, comments, and support.

#Python #Algorithm #SelectionSort
*****************************************************************
Full code from the video:
def selection_sort(list_a):
indexing_length = range(0, len(list_a)-1)

for i in indexing_length:
min_value = i

for j in range(i+1, len(list_a)):
if list_a[j] #less than list_a[min_value]: #"angled brackets not allowed in youtube description"
min_value = j

if min_value != i:
list_a[min_value], list_a[i] = list_a[i], list_a[min_value]

return list_a

print(selection_sort([7,8,9,8,7,6,5,6,7,8,9,0]))

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!
Рекомендации по теме
Комментарии
Автор

I wish I have teachers like him at my college

janmuhammad
Автор

Yeah, you're really talented at teaching. Believe me when I say you've got a real gift. And thank you for sharing it because you don't know how daunting this concept was to me when hearing it from other instructors.

lovesignal
Автор

4 years later and you are literally saving my life
Such a gift for teaching
Thank you

abhivaryakumar
Автор

Hey Derrick this is the best Algorithm Playlist I ever came across
So please it's a humble request to make more videos on Algorithm

utkarshmalik
Автор

this is absolutely the best series on algorithms, I struggled a lot watching other videos, but this is really amazing, clear and short.

navodyaliyanage
Автор

Still loving this video two years later Derrick!

AlfredDHull
Автор

This tutorial is incredible. Thank you for teaching it so effectively

andrewbrower
Автор

def selection_sort(list_a):


for i in indexing_length:
min_value=i
for j in range(i+1, len(list_a)):
if list_a[j]<list_a[min_value]:
min_value=j

list_a[min_value], list_a[i]=list_a[i], list_a[min_value]
return list_a


more efficient code, last if in j loop is not necessary

RadioKilledMusic
Автор

This is a great, amazing and super instructional video. My code for this algorithm is by far more complex. Love the code you created.

johnhillescobar
Автор

Dude!!! Derrick this is so awesome. you make these concepts so easy to learn. Keep it up!!!

coolios
Автор

i have to admit.. I love your short but badass bass music at the intro..

easynow
Автор

Pls teach all of those fundamental algorithms. Thanks

abletmuhtar
Автор

Here again for another alogorithm treat! Thanks buddy for yet again great video. Looking forward to look into the code on my comp later today

timohelasvuo
Автор

You're an amazing videos are very helpful.... Thanks

shrutikanikhar
Автор

Hi Derrick, first of all I would like to thank you for your videos. They are helping me a lot!
I think I found an indentation error inside your script in your githup area.
The "if min_value !=1" is not indented with the "for j", and this can cause some sort errors if the first element of the list is equal to the last element.
thanks!





for j in range(i+1, len(list_a)):

if list_a[j] < list_a[min_value]:

min_value = j




if min_value != i:

list_a[min_value], list_a[i] = list_a[i], list_a[min_value]




return list_a




print(selection_sort([6, 7, 8, 7, 6, 5, 4, 5, 6, 7, 6, 7, 8, 9, 7, 9, 0]))

tuborao
Автор

I always wonder, why my professor spends 50 mins lecture just to teach what you show in 5 mins. Thank you always.

AlternateMelatonin
Автор

Thanks bro, I have an exam tomorrow. And this is really gonna help.

mahadkhurram
Автор

I like the new intro and the outro! Content is also great!

rushas
Автор

Great tutorial! Easy to grasp the concepts.. Subbed!

lone.wof
Автор

Your videos as terrific, thank you. Below is what I came up with - it is almost twice as fast as your code. Why is that??

def
sorted_list = []
while len(unsorted_list) > 0:
min_index, min = 0, unsorted_list[0]
for i in range(0, (len(unsorted_list) - 1)):
if unsorted_list[i] < min:
min_index, min = i, unsorted_list[i]

return sorted_list

aqualung