Тёмный
No video :(

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

Derrick Sherrill
Подписаться 84 тыс.
Просмотров 71 тыс.
50% 1

Continuing the Python algorithm series with Selection Sort. Check out the full playlist here:
• Bubble Sort Algorithm ...
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
FB - / codewithderrick
Insta - / codewithderrick
Twitter - / codewithderrick
LinkedIn - / derricksherrill
GitHub - github.com/Der...
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]))
github.com/Der...
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:
github.com/Der...
Check out my website:
www.derrickshe...
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 - atom.io/
What Equipment do you use to film videos?
Blue Yeti Microphone - amzn.to/2PcNj5d
Mic sound shield - amzn.to/3bVNkEt
Soundfoam - amzn.to/37NV9ci
Camera desk stand - amzn.to/3bX8xhm
Box Lights - amzn.to/2PanL95
Side Lights - amzn.to/37KSNut
Green Screen - amzn.to/37SFFnc
What computer do you use/desk setup?
Film on imac (4k screen) - amzn.to/37SEu7g
Work on Macbook Pro - amzn.to/2HJ5b3G
Video Storage - amzn.to/2Pey8sw
Mouse - amzn.to/2PhCtv3
Desk - amzn.to/37O1Mv1
Chair - amzn.to/2uqHE4E
What editing software do you use?
Adobe CC - www.adobe.com/...
Premiere Pro for video editing
Photoshop for images
After Effects for animations
Do I have any courses available?
Yes & always working on more!
www.udemy.com/...
Where do I get my music?
I get all my music from the copyright free RU-vid audio library
www.youtube.co...
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!

Опубликовано:

 

14 авг 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 93   
@janmuhammad7376
@janmuhammad7376 3 года назад
I wish I have teachers like him at my college
@utkarshmalik4471
@utkarshmalik4471 3 года назад
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
@thatcherkane4955
@thatcherkane4955 3 года назад
Sorry to be off topic but does anybody know a way to log back into an Instagram account?? I was stupid forgot the password. I love any assistance you can give me
@kasontrevor966
@kasontrevor966 3 года назад
@Thatcher Kane instablaster =)
@thatcherkane4955
@thatcherkane4955 3 года назад
@Kason Trevor thanks so much for your reply. I found the site through google and Im waiting for the hacking stuff now. Looks like it's gonna take quite some time so I will reply here later when my account password hopefully is recovered.
@thatcherkane4955
@thatcherkane4955 3 года назад
@Kason Trevor it did the trick and I now got access to my account again. I'm so happy! Thank you so much you saved my account !
@kasontrevor966
@kasontrevor966 3 года назад
@Thatcher Kane Glad I could help :)
@lovesignal
@lovesignal 9 месяцев назад
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.
@navodyaliyanage473
@navodyaliyanage473 3 года назад
this is absolutely the best series on algorithms, I struggled a lot watching other videos, but this is really amazing, clear and short.
@andrewbrower4158
@andrewbrower4158 3 года назад
This tutorial is incredible. Thank you for teaching it so effectively
@AlfredDHull
@AlfredDHull 3 года назад
Still loving this video two years later Derrick!
@johnhillescobar
@johnhillescobar 5 лет назад
This is a great, amazing and super instructional video. My code for this algorithm is by far more complex. Love the code you created.
@aqualung1466
@aqualung1466 2 года назад
@Maryam Ashraf Below is what I did to test - setup params for "my_range" and "num_elements", or do like I did and make these arguments to your script with sys.argv. import sys import time import random my_range = int(sys.argv[1]) num_elements = int(sys.argv[2]) my_array = [random.randint(1, my_range) for i in range(0, num_elements)] start_time = time.time() bubble_sorted_array = list(bubble_sort(my_array)) bubble_sort_run_time = time.time() - start_time start_time = time.time() selection_sorted_array = list(selection_sort(my_array)) selection_sort_run_time = time.time() - start_time selection_sort_efficiency = bubble_sort_run_time / selection_sort_run_time print("bubble sort took {:.2f} seconds to run".format(bubble_sort_run_time)) print("selection sort took {:.2f} seconds to run".format(selection_sort_run_time)) print("selection sort was {:.2f} times more efficient than bubble sort".format(selection_sort_efficiency)) HTH
@RadioKilledMusic
@RadioKilledMusic 4 года назад
def selection_sort(list_a): indexing_length=range(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]
@abletmuhtar8979
@abletmuhtar8979 5 лет назад
Pls teach all of those fundamental algorithms. Thanks
@tuborao
@tuborao 4 года назад
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]))
@cassidybrookland2174
@cassidybrookland2174 2 года назад
this fixed my error!
@coolios685
@coolios685 3 года назад
Dude!!! Derrick this is so awesome. you make these concepts so easy to learn. Keep it up!!!
@easynow6599
@easynow6599 2 года назад
i have to admit.. I love your short but badass bass music at the intro..
@shrutikanikhar7987
@shrutikanikhar7987 4 года назад
You're an amazing teacher.......your videos are very helpful.... Thanks
@timohelasvuo9650
@timohelasvuo9650 5 лет назад
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
@jiahuizhu3911
@jiahuizhu3911 3 года назад
A basic question. How come the 'changing position' could work? such as list[j], list[i] = list[i], list[j]. The first assignment should change list[j], the the second assignment will come back to i, isn't it? Has python put some secrete third item to keep the value temporarily? I am a learner on Python:) Thank you very much in advance
@rahulnegi456
@rahulnegi456 3 года назад
its swapping the expression: “ a, b = b, a”, first right gets assigned to first left and second right get assigned to second left at the same time therefore swap values of a and b Search for one liner swapping in python you will get it!
@aqualung1466
@aqualung1466 2 года назад
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 selection_sort(unsorted_list): 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] sorted_list.append(unsorted_list.pop(min_index)) return sorted_list
@rayanrahmoune1064
@rayanrahmoune1064 4 года назад
I did it another way i don't know if it's selection sort def selectionsort(mylist): indexing_length = len(mylist) sorted = [] if indexing_length mylist[value]: min = mylist[value] mylist[0], mylist[value] = mylist[value] , mylist[0] mylist.pop(0) notsorted = mylist sorted.append(min) return sorted + selectionsort(notsorted)
@SEE.ME.N0.M0RE
@SEE.ME.N0.M0RE 3 года назад
Thank you so much!
@DeepakSah3.0
@DeepakSah3.0 4 месяца назад
Love from India - Thanks
@lone.wo1f
@lone.wo1f 4 года назад
Great tutorial! Easy to grasp the concepts.. Subbed!
@anirbanc88
@anirbanc88 2 года назад
you are the coolest guy ever man, thanks so much
@AlternateMelatonin
@AlternateMelatonin Год назад
I always wonder, why my professor spends 50 mins lecture just to teach what you show in 5 mins. Thank you always.
@carloschaccon8465
@carloschaccon8465 5 лет назад
Awesome Derrick, Have you considered a video for Regular Expressions to find data over large files, It is just an idea, thanks again, and keep sharing your knowledge .
@user-uw3hu2bb4s
@user-uw3hu2bb4s 3 года назад
hey buddy thanks for helping us and i coded my own version def selectionSort(l): indexLength = range(0,len(l) - 1) for i in indexLength: minvalue = i for j in range(i+1,len(l)): if l[j] < l[minvalue]: l[j],l[minvalue] = l[minvalue],l[j] j = j -1 return l print(selectionSort([1,3,0])) this works for me!
@rushas
@rushas 4 года назад
I like the new intro and the outro! Content is also great!
@mahadkhurram2950
@mahadkhurram2950 3 года назад
Thanks bro, I have an exam tomorrow. And this is really gonna help.
@nowyouknow2249
@nowyouknow2249 5 лет назад
Nice video like it's really cool. Fire on bruv!
@mehmetozel9001
@mehmetozel9001 3 года назад
Really really easy explained I love u
@Futball860
@Futball860 2 года назад
Would the result be different if we used indexing_length = range(0, len(list_a))?
@xynerd
@xynerd 2 года назад
awesome explanation!
@alexeyigonen3170
@alexeyigonen3170 4 года назад
Thanks for your videos. Will this code be shorter? (prints really help to see the work of the two cycles) for k in range(0,n-1): print('for cycle, k: ', k) for x in range(1, n): print("for cycle, x:",x,a) if a[k] > a[x] and x > k: a[k], a[x] = a[x], a[k]
@codex8332
@codex8332 4 года назад
Please I don't understand the logic behind.the last part. If the min_value != i . Please I need an explanation
@KeepCoding69
@KeepCoding69 Год назад
It's not required actually u can just simply write the switch statement and it would still work.
@DispelTV
@DispelTV 2 года назад
When I measure the time it takes to sort with bubble vs selection, selection always takes longer to execute. How is selection better than bubble if bubble takes less time and there's 2 for loops in selection sort which leads to higher complexity O(n^2)? Otherwise, fantastic and simple videos to learn from.
@dewman7477
@dewman7477 3 года назад
I don’t get why indexing length only goes to second last item in unsourced list. What if the item in the unworried list is smaller than the previous minimum value?
@navodyaliyanage473
@navodyaliyanage473 3 года назад
please make more videos on python..on what's after algorithms
@lyricsdelivered
@lyricsdelivered 2 года назад
How do you calculate time complexity
@sebastianlupa7355
@sebastianlupa7355 3 года назад
I think calling min_value index_of_min_value could make it clearer, took me a while to figure it out :-)
@aqualung1466
@aqualung1466 2 года назад
Agreed.
@breakoutgaffe4027
@breakoutgaffe4027 2 года назад
great explanation
@abdullahshahid6216
@abdullahshahid6216 3 года назад
Line 7: Should the min_value be equal to list_a[ i ]??? Please answer
@dhaval1489
@dhaval1489 5 лет назад
Hey Dirreck, can you please do the same in OOP, most tutorial don't cover OOP request you to do that for future videos
@hardiktmustudent5851
@hardiktmustudent5851 3 года назад
hey can you plz tell me a program for to rotate a matrix 90 degree anti-clockwise
@handle-2
@handle-2 3 года назад
can someone explain what this line does? its line 14 in his code list_a[min_value],list_a[i]=list_a[i],list_a[min_value]
@rahulnegi456
@rahulnegi456 3 года назад
its swapping the expression: “ a, b = b, a”, first right gets assigned to first left and second right get assigned to second left at the same time therefore swap values of a and b Search for one liner swapping in python you will get it!
@erikcarcelen1663
@erikcarcelen1663 4 года назад
Hey, amazing video and so beautiful explanation, but I have a question hehe How do you do your animations?
@betobernal486
@betobernal486 4 года назад
Hi Derrick thanks for your videos. I solved in a recursive way, take a look! def selection_sort(lista,sortedd): if len(lista) !=0: mini=lista[0] for i in lista[1:]: mini=min(mini,i) sortedd.append(mini) lista.remove(mini) selection_sort(lista,sortedd) return sortedd def sorting(lista): return selection_sort(lista,[]) a=[3,3,54,2345,542,456,0,3,4,4,3,6] sorting(a) The problem is that my function requires 2 args, thats why I had to implement the second function. Is there a way to do it this way with only one function? Thanks!
@jaket750
@jaket750 Год назад
thanks man you saved me
@alighaith9717
@alighaith9717 5 лет назад
thank you for effort Derrick , can you help me with this issue (I have a file of 1000 images and i want to classify it at specific folders i already created for example : from pic 1 to 10 move to folder number 1 and from 12 to 15 to folder number 2 etc ....)I'm biggner on python if you help me I really appreciated it .
@ahmedghanem3126
@ahmedghanem3126 4 года назад
Hey Bro You have wrong code if you put [ '1', '4', '19'] output will be [ '1', '19' , '4']
@gaganjeetsingh673
@gaganjeetsingh673 4 года назад
No. The code is absolutely fine. You must've implemented it wrong.
@SReaperz
@SReaperz 3 года назад
his code is the problem to fix it just add if min_value != i: list_a[min_value], list_a[i] = list_a[i], list_a[min_value] min_value = i
@lukasreissig4500
@lukasreissig4500 3 года назад
I am a beginner with Python, which programming evironment is that?
@sergdonskikh2140
@sergdonskikh2140 4 года назад
Hi! Good lessons!
@somyamaniktala1090
@somyamaniktala1090 4 года назад
best explanation..
@aminetlemcani693
@aminetlemcani693 3 года назад
Thanks for the video
@shanmugaraj3539
@shanmugaraj3539 3 года назад
bro can you do more videos like this...
@mhd403
@mhd403 4 года назад
when we changed iMin = j does that change the iMin value or the value stored at list[iMin]?
@16aasthadoshi95
@16aasthadoshi95 3 года назад
imin value whereas list[imin]=j changes value in the list to value of j
@luffyhat3852
@luffyhat3852 2 года назад
your code isnt working for [3,1,2,5,4] this input . it shows the output as [2,1,3,4,5]
@KeepCoding69
@KeepCoding69 Год назад
You need to write the switch statement outside the 2nd for loop. I know it's late but I hope you will find it useful.
@johndunn6253
@johndunn6253 5 лет назад
Good stuff. Thanks
@krissh6563
@krissh6563 4 года назад
Which software you are using for this video editing
@vivalavida7839
@vivalavida7839 4 года назад
Thank you sir
@RS-el7iu
@RS-el7iu 4 года назад
thanks a lot...👍🏻👍🏻👍🏻👍🏻
@samuelchen8635
@samuelchen8635 3 года назад
thank you for making this :0
@prasikrar3927
@prasikrar3927 3 года назад
thanks sir iam from wkwkw island
@MadhushreeSinha
@MadhushreeSinha 4 года назад
Isn't the min_val != i : is redundant?
@rahulnegi456
@rahulnegi456 3 года назад
yes its not necessary
@leas9854
@leas9854 3 года назад
What do i and j stand for?
@KeepCoding69
@KeepCoding69 Год назад
Those are the counter variables
@expat2010
@expat2010 4 года назад
Cool!
@masoud4843
@masoud4843 2 года назад
❤👌
@rishabhawasthi8192
@rishabhawasthi8192 2 года назад
David Bowie teaching CS
@darthbolloful
@darthbolloful 4 года назад
Derrick stop it you're scaring me
@yunkel
@yunkel 5 лет назад
👍
@AbdullahKhan-dl9lm
@AbdullahKhan-dl9lm 3 года назад
ahhh....The algorithm doesn't work.
@rw4877
@rw4877 3 года назад
You have beautiful eyes man
@hamdamjonganijonov8825
@hamdamjonganijonov8825 4 года назад
lmao, why is his code sooo easy to understand!
@strade740
@strade740 6 месяцев назад
bro look like rizzler
Далее
Selection Sort | Python | Algorithms Tutorial
8:10
Просмотров 27 тыс.
25 nooby Python habits you need to ditch
9:12
Просмотров 1,7 млн
Dijkstra's Algorithm - Computerphile
10:43
Просмотров 1,3 млн
Quick Sort Theory | DSA
21:18
Просмотров 18 тыс.
5 Useful F-String Tricks In Python
10:02
Просмотров 293 тыс.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Merge Sort In Python Explained (With Example And Code)
13:35