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

  Рет қаралды 143,596

Derrick Sherrill

Derrick Sherrill

Күн бұрын

Пікірлер: 183
@BN-hy1nd
@BN-hy1nd Жыл бұрын
There are thousands of python tutorials on the internet but this dude has the attitude of "keep it simple , stupid" which suits me fine as a beginner who asks stupid questions. Thank you👏
@vidushimohan9949
@vidushimohan9949 3 жыл бұрын
Man, you're a godsend! Such a crisp and on-point elucidation, I've never felt anything clearer than this in my life!
@kartikchauhan2778
@kartikchauhan2778 2 жыл бұрын
yes
@samyeung122
@samyeung122 2 жыл бұрын
agree!
@marwanhamdaoui8327
@marwanhamdaoui8327 8 ай бұрын
True
@abhijaiallgamesgamer2075
@abhijaiallgamesgamer2075 5 ай бұрын
no kiddin 🫡
@andrewtate8792
@andrewtate8792 6 ай бұрын
Nice, you kept it very simple. A quick improvement tip: in every outer loop you can actually decrease the index length since bubblesprt always sorts the biggest element each iteration.
@gachle
@gachle 6 ай бұрын
This is the first time of thousands, that I check a tutorial o YT and I see no troubles with me understanding it... Simple and easy! Thanks
@EllenDivaIbanez
@EllenDivaIbanez 6 ай бұрын
same, this guy can explain the concept in the simple way and also in the short time
@rajanhossain9250
@rajanhossain9250 5 ай бұрын
Same here, finally I get this clearly! Explaining the whole thing while writing code makes it super easy to understand.
@leonquadros4552
@leonquadros4552 5 жыл бұрын
This is great stuff. Pls continue making videos
@uppalapraveen959
@uppalapraveen959 4 жыл бұрын
Fell in love with the way, you explain things. It's easy to grasp. Please keep posting the videos. (if possible , could you please post a video on recursion and how to apply it in different scenarios). Thank You
@kharlopena7512
@kharlopena7512 3 жыл бұрын
Been searching for so many explanations for sorting and I get it, but yours have been the easiest to understand by far. Godly!
@adithyaramesh7251
@adithyaramesh7251 3 жыл бұрын
Bro u are an absolute legend. I was trying to learn this from my teacher but did not understand anything but after watching this video I have no more doubt. TYSM
@tareqmahmud3902
@tareqmahmud3902 3 жыл бұрын
Boss I have solved the problem when you said to try it by myself. You are the extraordinary teacher I could ever find.
@codeNexus-bb2mb
@codeNexus-bb2mb Жыл бұрын
after 4 years this is by far the best video
@RoqiMohajeri
@RoqiMohajeri 2 ай бұрын
Your way of teaching is simple, short, and understandable. Great job. Thanks a lot.
@timohelasvuo9650
@timohelasvuo9650 5 жыл бұрын
Really a fan of your videos. Well presented every time and it keeps getting better and better. Keep up the good work. This series seems very interesting. Cheers!
@banquotheholstein
@banquotheholstein Жыл бұрын
I was lost and unable to code this until I watched your video, I am very thankful for the help you've provided me! I am looking forward to using your channel to learn more.
@Impedance_Z
@Impedance_Z Жыл бұрын
People go to indians for tutorial and I'm an indian coming to you for tutorial 😂
@nasttygamer08
@nasttygamer08 10 ай бұрын
Bro first learn English
@Impedance_Z
@Impedance_Z 10 ай бұрын
@@nasttygamer08 What was wrong in it could please enlighten me with my mistake?
@nasttygamer08
@nasttygamer08 10 ай бұрын
@@Impedance_Z people go to Indians for tutorial ❌ people go to Indian tutorial ✅ The meaning of both the things is correct but according to what you want to say, I said it is correct, And you said that it sounds difficult to read and listen to. 👍
@t.h.e.unknown_
@t.h.e.unknown_ 7 ай бұрын
​@@nasttygamer08 bruh yours sounds incomplete... Leave lil bro alone💀
@ivanxavier9022
@ivanxavier9022 5 ай бұрын
@@nasttygamer08you have no right to tell someone to learn English when your grammar is no different from a 4 year old 😂😂
@ToughenUpkids
@ToughenUpkids 8 ай бұрын
Great video - very clearly explained and helped the penny drop very quickly!
@fareselamine8115
@fareselamine8115 3 жыл бұрын
In my opinion, this is a more elegant solution than the two for loops a lot of other videos suggest using. This makes more intuitive sense, thank you!
@TechEndeavor
@TechEndeavor 4 жыл бұрын
Great explanation. I've been trying to wrap my head around this one for a minute.
@serranoaaronjamesv.6436
@serranoaaronjamesv.6436 2 жыл бұрын
The easiest tutorial to follow, straight to the point, will definitely recommend this to my colleagues.
@erikajanecabanban5100
@erikajanecabanban5100 2 жыл бұрын
true
@coreyjordan6733
@coreyjordan6733 2 жыл бұрын
I was researching this algorithm, your code and explanation is so much more simple and I appreciate that.
@williancerqueira
@williancerqueira Сағат бұрын
I almost died trying to understand Bubble sort yesterday, this video explains it so clearly!
@nithin_in_ireland
@nithin_in_ireland 3 жыл бұрын
This is how all the explanation should be done. precise and on point !! way to go buddy
@kvelez
@kvelez Жыл бұрын
Great tutorial, please continue uploading more Algorithms, Data Structures, and Interview coding challenges. def bubble_sort(arr): ordered = False length = len(arr) - 1 while not ordered: ordered = True for i in range(length): if arr[i] > arr[i+1]: ordered = False arr[i], arr[i+1] = arr[i+1], arr[i] return arr print(bubble_sort([65,677,55,45,0,3,1]))
@thedataprofs3867
@thedataprofs3867 Жыл бұрын
Great, short and straight to the point explanation!
@paula98033
@paula98033 3 жыл бұрын
Hi Derrick , please start making video from begging for python!. You really explain the bubble sort very clearly.Please make more videos in python programming. Thanks
@devindeepak
@devindeepak 4 жыл бұрын
I just love the way you're making the concepts simpler. Please keep doing...
@thescoobiestdoo2642
@thescoobiestdoo2642 4 жыл бұрын
how tf is this dude not huge this shit is life saving
@jupjyotkhosla4640
@jupjyotkhosla4640 2 жыл бұрын
Great video! Simply explained. Just one thing you could use indexing_length=indexing_length-1 outside the for loop to improve the speed, as bubble sort keeps sorting the last element.
@lucysnowe9974
@lucysnowe9974 3 жыл бұрын
Thank you - first guide I have managed to find that explains each line of code.
@shrutikanikhar7987
@shrutikanikhar7987 4 жыл бұрын
OMG!....the best video on bubble sort. Thankyou and keep up the good work
@benduitsman3161
@benduitsman3161 2 жыл бұрын
Perfect explanations. Never followed a channel so fast XD
@aminetlemcani693
@aminetlemcani693 4 жыл бұрын
Thanks for the video: This is mine: unslist = [3,7,2,6,4,9,1,5,1] totlist = [ ] j =0 while True: for i in range(len(unslist)-1): if unslist[i]>unslist[i+1]: unslist[i],unslist[i+1]=unslist[i+1],unslist[i] else: continue totlist.append(unslist) if len(totlist)>len(unslist)-2: if totlist[len(totlist)-1] == totlist[-1]: break print(unslist)
@MaxHamilton559
@MaxHamilton559 4 жыл бұрын
Clear and concise, thanks mate!
@rhodabaruch4
@rhodabaruch4 Жыл бұрын
Perfectly clear. Thanks, Derrick.
@respect_gamers5695
@respect_gamers5695 4 жыл бұрын
Well, I had been messing up with the codes i get in my Book which is definitely not for beginners but it is preferred for them. Then comes your codes directly hits my brain and i catch it very easily. Ty for the codings. Plz come up with Data structure with python series. Thanks. Keep up the Good Work Sir
@theminertom11551
@theminertom11551 4 жыл бұрын
Of course, as usual, GREAT content!
@kuykuy8800
@kuykuy8800 3 жыл бұрын
Great explanation! So much clearer and easier to understand!
@shaharyarahmed5777
@shaharyarahmed5777 4 жыл бұрын
This was an amazing tutorial man! You just earned a new subscriber!
@FrenchyFlyTv
@FrenchyFlyTv 4 жыл бұрын
This video show simplicity and great explanation for reading the Head First: Learn to Code
@vivekr4690
@vivekr4690 5 жыл бұрын
Thanks for your videos. They're well presented and quality is crisp!
@austintownsend3974
@austintownsend3974 Жыл бұрын
You accent is really calming while learning
@MrAmbarish710
@MrAmbarish710 4 жыл бұрын
Different implementation than the usual nested for loop implementation. But really nice and easy explanation!
@tjmns
@tjmns 11 ай бұрын
there's another way without the sorted boolean. ________________________________________________ def bubble_sort(arr) n = len(arr) for in range(n): for j in range(n - i -1): # notice the -1 here also if arr[j] > arr[j+1]: arr[j], arr[j+1] = arr[j+1], arr[j] #swaps here if needed #example numbers = [5,2,8,12,1] bubble_sort(numbers) print(numbers) #output will be [1,2,3,8,12]
@pappymekk4790
@pappymekk4790 3 жыл бұрын
Thanks dude, you actually made a simple implementation than the one I was struggling with
@kasvisreemurthy7009
@kasvisreemurthy7009 4 жыл бұрын
This stuff was real helpful !! Thanks for makin' it
@ReverantKnight
@ReverantKnight Жыл бұрын
Great explanation! Thanks Derrick.
@Princeali736
@Princeali736 Жыл бұрын
@derricksherill3511. I wish u could do more on DSA. I haven't understood these alogrithms with a lot of ease before you. Come back Legend.
@adittorahman8255
@adittorahman8255 4 жыл бұрын
you way of explaining things is greatv .. A new member in your hood
@natashasen3548
@natashasen3548 2 жыл бұрын
Awesome! The way you implement the technique is completely different from the usual one. But very clean explanation.
@ershadramezani6716
@ershadramezani6716 4 жыл бұрын
Hi. Thank you for the video. I didn’t get your mean for ‘while not sorted, sorted False and sorted True’. Could you please explain or suggest me a tutorial link for that? Thanks a lot again.
@cheeweng1060
@cheeweng1060 3 жыл бұрын
hope its not too late.. how this works is that the outer while loop, when initiated, assumes that the list is sorted, until it isn't, and this is verified by the inner for loop. This structure keeps the sorting going until eventually, the list becomes sorted
@subhashreebehera2994
@subhashreebehera2994 4 жыл бұрын
Never gonna miss ur videos now onwards..just awsm explanations...keep teaching us 🙏🙏
@poppyecave3966
@poppyecave3966 2 жыл бұрын
great going derrik just started coding
@генпол
@генпол 3 жыл бұрын
Этот канал просто находка, только начал учить Python
@CC-bm3wb
@CC-bm3wb 3 жыл бұрын
Good god this makes so much more sense than my textbook
@lucasm4299
@lucasm4299 2 жыл бұрын
Which is a better implementation, this one or the nested for loop one? I think this one is able to terminate the program earlier
@penteronto
@penteronto 2 жыл бұрын
@Lucas M both seems fine but personally i find the nested for loop simpler to undestand since you do not need to think about any way to escape the while loop since there is none. Even if the while loop version of bubble sorting executes faster than the classic one, they are both inefficient for bigger lists. I created a program in c++ that puts 50000 random numbers in a list, then i measured the time for the bubble sort function and it took 70 seconds while quicksort function took 0.019 seconds lol
@lamedev1342
@lamedev1342 3 жыл бұрын
You are truly gifted dude with your explanations awesome video!
@MANOJKumar-lk6qz344
@MANOJKumar-lk6qz344 2 жыл бұрын
Love the clarity bro 🙏
@mayankumar1312
@mayankumar1312 3 жыл бұрын
Great job Derrick.
@subramanianchenniappan4059
@subramanianchenniappan4059 4 жыл бұрын
came here from udemy. Thanks for great videos
@tambahsaah8005
@tambahsaah8005 2 жыл бұрын
Genius Derrick! Thanks
@likag.105
@likag.105 4 жыл бұрын
Quick and simple explanation, thank you!
@ted2091
@ted2091 3 жыл бұрын
clean and concise, thank you!
@NicoAn2
@NicoAn2 Жыл бұрын
it was easy to understand and follow thank you!
@dijkstra1144
@dijkstra1144 2 жыл бұрын
for i in range(len(arr)): for j in range(i+1,len(arr)): if arr[i]>arr[j]: arr[i],arr[j]=arr[j],arr[i]
@Re9sTwitch
@Re9sTwitch 3 жыл бұрын
Great vid, well explained. Thank you!
@nor123100
@nor123100 3 жыл бұрын
You explain in an amazing way! Thank you
@entity5678
@entity5678 Жыл бұрын
I took a different approach. "loop" is used in inner for loop bcoz we don't need to check the last elements after every iteration i.e if I have a list of 1000 elements, at the 500th iteration I would not be checking the last 500 elements and somehow tutorials missing this. def bubbleSort(unsorted): lst = list(unsorted) for loop in range(len(lst)-1): for num in range(len(lst)-1-loop): if lst[num] >= lst[num+1]: lst[num], lst[num+1] = lst[num+1], lst[num] return lst print(bubbleSort([5, 1, 3, 4, 2, 2, 5]))
@naiadbaksh3996
@naiadbaksh3996 3 жыл бұрын
Great series! Massive help.
@thaniacisneros5785
@thaniacisneros5785 7 ай бұрын
Wow!! Awesome, thank you so much!!!
@rasoulkhoshravan5912
@rasoulkhoshravan5912 3 жыл бұрын
Thanks for informative videos. I want to write a code to transfer Excel tables and charts from my laptop to a website. Di you have a tutorial in this regard?
@itaco8066
@itaco8066 2 жыл бұрын
Awesome video & great explanation
@iamjustahair1315
@iamjustahair1315 5 ай бұрын
this was just amazing. thank you so sos os much
@keuair
@keuair 4 жыл бұрын
Thanks for the clear explanation, great vid
@valenoveb3339
@valenoveb3339 3 жыл бұрын
Wonderful. If possible could you post a video on shuffling class in algorithms
@arjay_2002
@arjay_2002 3 жыл бұрын
time complexity?
@shrvansudhakar7514
@shrvansudhakar7514 4 жыл бұрын
Thanks a lot Derrick.
@jmitch474
@jmitch474 3 жыл бұрын
Wow amazing video, thank you!
@shakots
@shakots 5 жыл бұрын
Excellent Vid, keep it
@BN-hy1nd
@BN-hy1nd Жыл бұрын
managed to change two lines containing boolean statements to get sorting in descending order... thank you
@jst8922
@jst8922 11 ай бұрын
Regarding speed for vs. while in this case double for loop vs Derrick solution [code] def bubble_sort(arr): n = len(arr) for i in range(n): # Flag to check if any swaps are made in a pass swapped = False # Traverse through all array elements, excluding the last i elements for j in range(0, n-i-1): # Swap if the element found is greater than the next element if arr[j] > arr[j+1]: arr[j], arr[j+1] = arr[j+1], arr[j] swapped = True # If no swaps were made in the pass, the list is already sorted if not swapped: break # Example usage: my_list = [64, 34, 25, 12, 22, 11, 90] bubble_sort(my_list) print("Sorted array:", my_list) [/code] I did test case with 9999 elements in array (random numbers between 1-9999) [code] import random # Set the seed for reproducibility (optional) random.seed(42) # Generate a random array with 9999 elements random_array = [random.randint(1, 9999) for _ in range(9999)] # Print the generated array print(random_array) [/code] Benchmark was done here, before i generated a random array with 9999 elements, c/p into main code and ran 3 times jupyter.org/try-jupyter/notebooks/?path=notebooks/Intro.ipynb Results: while loop was 68% slower (11.1 vs 6.6s) update2 Benchmark was done here too www.programiz.com/python-programming/online-compiler/ while loop was 44% slower (6.5 vs 4.5s) localhost benchmark: ide: PyCharm Community Edition 2023.2.3, python 3.12.0, win10 x64, cpu usage 10% max while loop 5.5s double for loop 3.4s while loop was 61% slower
@jst8922
@jst8922 11 ай бұрын
Sorry, posted wrong code: [code] def bubble_sort(arr): n = len(arr) for i in range(n): # Flag to check if any swaps are made in a pass swapped = False # Traverse through all array elements, excluding the last i elements for j in range(0, n-i-1): # Swap if the element found is greater than the next element if arr[j] > arr[j+1]: arr[j], arr[j+1] = arr[j+1], arr[j] swapped = True # If no swaps were made in the pass, the list is already sorted if not swapped: break # Example usage: my_list = [64, 34, 25, 12, 22, 11, 90] bubble_sort(my_list) print("Sorted array:", my_list) [/code]
@jst8922
@jst8922 11 ай бұрын
One more thing, how to measure time add this into first two line of code: [code] import time start = time.time() def bubble_sort(arr): [/code] ... ... ... last three code line: [code] end = time.time() print(end - start) print("Sorted array:", my_list) [/code]
@dr1lltrckz147
@dr1lltrckz147 3 жыл бұрын
Hey Derrick can you explain why line 7 is necessary? Why do you change sorted to True?
@kyffran6084
@kyffran6084 5 жыл бұрын
Good video and explanation are clear !
@Hytension
@Hytension Жыл бұрын
I wish this would be broken down slower and in more detail, but I understand what you're doing.
@Kait07_
@Kait07_ Жыл бұрын
Great video!
4 жыл бұрын
Ley me know, you are genius instructor, I can send the subtitles
@emerymusicdisc7172
@emerymusicdisc7172 9 ай бұрын
this makes so much sense, unfortunatly i have to add unto pre-existing code and its at least 8 lines longer and i still have no clue wtf to do...
@barelycodingtoday
@barelycodingtoday Жыл бұрын
You could do the root For Loop as a enumeration here? This was such an easy explanation of Bubble Sort. Thanks!
@majidzarinkolah
@majidzarinkolah 3 жыл бұрын
Clear and fast!
@oldgregrl1934
@oldgregrl1934 Ай бұрын
Doesnt the way he structures where the booleans are make his loop run one too many times? He checks if the array is sorted BEFORE swapping the variables. After swapping the variables, the array might be sorted, so running through an extra time for no reason
@muhammadnasser312
@muhammadnasser312 4 жыл бұрын
Man u a Mighty God
@PIYdeveloper
@PIYdeveloper 4 жыл бұрын
Dude. You're awesome.
@manolo6048
@manolo6048 4 жыл бұрын
@Derrick.. you're the best
@joshnippleton3449
@joshnippleton3449 4 жыл бұрын
Hey Derrick. Do you have a video on Merge Sort?
@DerickGudo
@DerickGudo 4 жыл бұрын
What's the difference between using a for loop and a while loop for the outer loop?
@manashbehera961
@manashbehera961 2 жыл бұрын
such simple explanation
@tanzirhossain7159
@tanzirhossain7159 4 жыл бұрын
Great explanation! Can you please create a python critical problem solving playlist?
@rAndOmNeSs26
@rAndOmNeSs26 5 жыл бұрын
Thank you . well explained
@senahugtfl7789
@senahugtfl7789 2 жыл бұрын
literally ı loveeee it!!
@TomasMorales-d7r
@TomasMorales-d7r 4 ай бұрын
love you bro!
@yothinluangkhot601
@yothinluangkhot601 4 жыл бұрын
If there are flowchart, tracetable and pseudocode inclding in this video as well, it would be very helpful for some students who study computing foundation.
@Dallasjb
@Dallasjb Жыл бұрын
Thank you so much!!!
Bubble Sort in Plain English
11:35
Programming with Mosh
Рет қаралды 170 М.
Lazy days…
00:24
Anwar Jibawi
Рет қаралды 6 МЛН
FOREVER BUNNY
00:14
Natan por Aí
Рет қаралды 31 МЛН
Farmer narrowly escapes tiger attack
00:20
CTV News
Рет қаралды 11 МЛН
Merge Sort In Python Explained (With Example And Code)
13:35
FelixTechTips
Рет қаралды 226 М.
Why is Python 150X slower than C?
10:45
Mehul - Codedamn
Рет қаралды 18 М.
Сортировка пузырьком в python. Bubble sort in Python
14:27
Writing a Python Script to Control my Lights | Five Minute Python Scripts
8:15
Bubble Sort Algorithm in Python Explained Visually (with Code)
11:44
Coding with Estefania
Рет қаралды 10 М.
Intro to Sorting Algorithms in Python - Bubble Sort
9:00
Real Python
Рет қаралды 13 М.
Learn Bubble Sort in 7 minutes 🤿
7:44
Bro Code
Рет қаралды 292 М.
Lazy days…
00:24
Anwar Jibawi
Рет қаралды 6 МЛН