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

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 ก.ย. 2024
  • Bubble Sort is a simple sorting algorithm that repeatedly swaps two adjacent elements through iterations through the list length to create a sort list.
    The Bubble sort algorithm is one of the simplest algorithms to learn in computer science and is a great starting point to learn tougher algorithms.
    In this one we'll cover how to implement the bubble sort algorithm using python programming.
    #Python #BubbleSort #Algorithm
    Join The Socials -- Picking Shoutouts Across TH-cam, Insta, FB, and Twitter!
    FB - / codewithderrick
    Insta - / codewithderrick
    Twitter - / codewithderrick
    LinkedIn - / derricksherrill
    GitHub - github.com/Der...
    Thanks so much for the continued support of the videos! 5320+ subscribers at the time of writing. So awesome. Thank you all for supporting the content I make! It's incredible to get the opportunity to make these videos for you all. Thanks so much!
    *****************************************************************
    Full code from the video:
    def bubble(list_a):
    indexing_length = len(list_a) - 1 #SCan not apply comparision starting with last item of list (No item to right)
    sorted = False #Create variable of sorted and set it equal to false
    while not sorted: #Repeat until sorted = True
    sorted = True # Break the while loop whenever we have gone through all the values
    for i in range(0, indexing_length): # For every value in the list
    if list_a[i] #"Angled brackets not allowed in TH-cam Description :( list_a[i+1]: #if value in list is greater than value directly to the right of it,
    sorted = False # These values are unsorted
    list_a[i], list_a[i+1] = list_a[i+1], list_a[i] #Switch these values
    return list_a # Return our list "unsorted_list" which is not sorted.
    print(bubble([4,8,1,14,8,2,9,5,7,6,6]))
    github.com/Der...
    Packages (& Versions) used in this video:
    Python 3.7
    *****************************************************************
    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?
    www.amazon.com...
    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 TH-cam 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!
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 181

  • @BN-hy1nd
    @BN-hy1nd ปีที่แล้ว +14

    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 ปีที่แล้ว +72

    Man, you're a godsend! Such a crisp and on-point elucidation, I've never felt anything clearer than this in my life!

  • @gachle
    @gachle 4 หลายเดือนก่อน +2

    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 4 หลายเดือนก่อน +1

      same, this guy can explain the concept in the simple way and also in the short time

    • @rajanhossain9250
      @rajanhossain9250 3 หลายเดือนก่อน +1

      Same here, finally I get this clearly! Explaining the whole thing while writing code makes it super easy to understand.

  • @RoqiMohajeri
    @RoqiMohajeri 25 วันที่ผ่านมา +1

    Your way of teaching is simple, short, and understandable. Great job. Thanks a lot.

  • @leonquadros4552
    @leonquadros4552 4 ปีที่แล้ว +32

    This is great stuff. Pls continue making videos

  • @uppalapraveen959
    @uppalapraveen959 4 ปีที่แล้ว +26

    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

  • @ToughenUpkids
    @ToughenUpkids 6 หลายเดือนก่อน +1

    Great video - very clearly explained and helped the penny drop very quickly!

  • @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

  • @kharlopena7512
    @kharlopena7512 3 ปีที่แล้ว +1

    Been searching for so many explanations for sorting and I get it, but yours have been the easiest to understand by far. Godly!

  • @codeNexus-bb2mb
    @codeNexus-bb2mb 10 หลายเดือนก่อน

    after 4 years this is by far the best video

  • @TechEndeavor
    @TechEndeavor 4 ปีที่แล้ว +4

    Great explanation. I've been trying to wrap my head around this one for a minute.

  • @paula98033
    @paula98033 3 ปีที่แล้ว +1

    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

  • @serranoaaronjamesv.6436
    @serranoaaronjamesv.6436 2 ปีที่แล้ว

    The easiest tutorial to follow, straight to the point, will definitely recommend this to my colleagues.

  • @fareselamine8115
    @fareselamine8115 3 ปีที่แล้ว +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!

  • @thedataprofs3867
    @thedataprofs3867 ปีที่แล้ว +1

    Great, short and straight to the point explanation!

  • @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.

  • @kvelez
    @kvelez 10 หลายเดือนก่อน

    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]))

  • @austintownsend3974
    @austintownsend3974 ปีที่แล้ว

    You accent is really calming while learning

  • @coreyjordan6733
    @coreyjordan6733 2 ปีที่แล้ว

    I was researching this algorithm, your code and explanation is so much more simple and I appreciate that.

  • @devindeepak
    @devindeepak 3 ปีที่แล้ว

    I just love the way you're making the concepts simpler. Please keep doing...

  • @benduitsman3161
    @benduitsman3161 2 ปีที่แล้ว

    Perfect explanations. Never followed a channel so fast XD

  • @champion_life
    @champion_life 3 ปีที่แล้ว

    This is how all the explanation should be done. precise and on point !! way to go buddy

  • @Impedance_Z
    @Impedance_Z ปีที่แล้ว +23

    People go to indians for tutorial and I'm an indian coming to you for tutorial 😂

    • @nasttygamer08
      @nasttygamer08 8 หลายเดือนก่อน

      Bro first learn English

    • @Impedance_Z
      @Impedance_Z 8 หลายเดือนก่อน

      @@nasttygamer08 What was wrong in it could please enlighten me with my mistake?

    • @nasttygamer08
      @nasttygamer08 8 หลายเดือนก่อน +1

      @@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_ 5 หลายเดือนก่อน +7

      ​@@nasttygamer08 bruh yours sounds incomplete... Leave lil bro alone💀

    • @ivanxavier9022
      @ivanxavier9022 3 หลายเดือนก่อน

      @@nasttygamer08you have no right to tell someone to learn English when your grammar is no different from a 4 year old 😂😂

  • @aminetlemcani693
    @aminetlemcani693 3 ปีที่แล้ว

    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)

  • @lucysnowe9974
    @lucysnowe9974 3 ปีที่แล้ว

    Thank you - first guide I have managed to find that explains each line of code.

  • @jupjyotkhosla4640
    @jupjyotkhosla4640 2 ปีที่แล้ว +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.

  • @rhodabaruch4
    @rhodabaruch4 ปีที่แล้ว

    Perfectly clear. Thanks, Derrick.

  • @poppyecave3966
    @poppyecave3966 2 ปีที่แล้ว

    great going derrik just started coding

  • @MaxHamilton559
    @MaxHamilton559 4 ปีที่แล้ว +2

    Clear and concise, thanks mate!

  • @shaharyarahmed5777
    @shaharyarahmed5777 4 ปีที่แล้ว +1

    This was an amazing tutorial man! You just earned a new subscriber!

  • @theminertom11551
    @theminertom11551 3 ปีที่แล้ว +1

    Of course, as usual, GREAT content!

  • @MrAmbarish710
    @MrAmbarish710 4 ปีที่แล้ว +1

    Different implementation than the usual nested for loop implementation. But really nice and easy explanation!

  • @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

  • @tambahsaah8005
    @tambahsaah8005 ปีที่แล้ว

    Genius Derrick! Thanks

  • @pappymekk4790
    @pappymekk4790 3 ปีที่แล้ว

    Thanks dude, you actually made a simple implementation than the one I was struggling with

  • @FrenchyFlyTv
    @FrenchyFlyTv 4 ปีที่แล้ว

    This video show simplicity and great explanation for reading the Head First: Learn to Code

  • @BN-hy1nd
    @BN-hy1nd ปีที่แล้ว

    managed to change two lines containing boolean statements to get sorting in descending order... thank you

  • @adittorahman8255
    @adittorahman8255 4 ปีที่แล้ว +1

    you way of explaining things is greatv .. A new member in your hood

  • @CC-bm3wb
    @CC-bm3wb 3 ปีที่แล้ว

    Good god this makes so much more sense than my textbook

  • @natashasen3548
    @natashasen3548 2 ปีที่แล้ว

    Awesome! The way you implement the technique is completely different from the usual one. But very clean explanation.

  • @ReverantKnight
    @ReverantKnight ปีที่แล้ว

    Great explanation!
    Thanks Derrick.

  • @subhashreebehera2994
    @subhashreebehera2994 3 ปีที่แล้ว

    Never gonna miss ur videos now onwards..just awsm explanations...keep teaching us 🙏🙏

  • @thaniacisneros5785
    @thaniacisneros5785 5 หลายเดือนก่อน

    Wow!! Awesome, thank you so much!!!

  • @MANOJKumar-lk6qz344
    @MANOJKumar-lk6qz344 2 ปีที่แล้ว

    Love the clarity bro 🙏

  • @iamjustahair1315
    @iamjustahair1315 3 หลายเดือนก่อน

    this was just amazing. thank you so sos os much

  • @генпол
    @генпол 3 ปีที่แล้ว

    Этот канал просто находка, только начал учить Python

  • @timohelasvuo9650
    @timohelasvuo9650 5 ปีที่แล้ว +19

    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!

  • @kasvisreemurthy7009
    @kasvisreemurthy7009 4 ปีที่แล้ว

    This stuff was real helpful !!
    Thanks for makin' it

  • @nor123100
    @nor123100 3 ปีที่แล้ว

    You explain in an amazing way! 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]

  • @majidzarinkolah
    @majidzarinkolah 3 ปีที่แล้ว

    Clear and fast!

  • @ershadramezani6716
    @ershadramezani6716 4 ปีที่แล้ว +3

    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

  • @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]))

  • @NicoAn2
    @NicoAn2 ปีที่แล้ว

    it was easy to understand and follow thank you!

  • @mayankumar1312
    @mayankumar1312 3 ปีที่แล้ว

    Great job Derrick.

  • @ted2091
    @ted2091 3 ปีที่แล้ว

    clean and concise, thank you!

  • @subramanianchenniappan4059
    @subramanianchenniappan4059 4 ปีที่แล้ว

    came here from udemy. Thanks for great videos

  • @naiadbaksh3996
    @naiadbaksh3996 3 ปีที่แล้ว

    Great series! Massive help.

  • @Kait07_
    @Kait07_ ปีที่แล้ว

    Great video!

  • @itaco8066
    @itaco8066 2 ปีที่แล้ว

    Awesome video & great explanation

  • @valenoveb3339
    @valenoveb3339 3 ปีที่แล้ว

    Wonderful. If possible could you post a video on shuffling class in algorithms

  • @shrvansudhakar7514
    @shrvansudhakar7514 3 ปีที่แล้ว

    Thanks a lot Derrick.

  • @jmitch474
    @jmitch474 3 ปีที่แล้ว

    Wow amazing video, thank you!

  • @shakots
    @shakots 5 ปีที่แล้ว +1

    Excellent Vid, keep it

  • @Dallasjb
    @Dallasjb ปีที่แล้ว

    Thank you so much!!!

  • @nidhihanda4827
    @nidhihanda4827 ปีที่แล้ว

    well i did a little different here is my code
    def bubblesort(lst: list):
    for i in range(len(lst) - 1):
    for i in range(len(lst) - 1):
    if lst[i] >= lst[i+1]:
    lst[i], lst[i+1] = lst[i+1], lst[i]
    return lst

  • @senahugtfl7789
    @senahugtfl7789 2 ปีที่แล้ว

    literally ı loveeee it!!

  • @anonranger6428
    @anonranger6428 ปีที่แล้ว

    Excellent

  • @kyffran6084
    @kyffran6084 5 ปีที่แล้ว

    Good video and explanation are clear !

  • @sivakumaranbazhagan1672
    @sivakumaranbazhagan1672 11 หลายเดือนก่อน

    Can you cover merge sort as well

  • @manolo6048
    @manolo6048 4 ปีที่แล้ว

    @Derrick.. you're the best

  • @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?

  • @PepeTostado
    @PepeTostado 2 ปีที่แล้ว

    Why isn’t the while lip breaking because of the variable that is true?

  • @dr1lltrckz147
    @dr1lltrckz147 3 ปีที่แล้ว

    Hey Derrick can you explain why line 7 is necessary? Why do you change sorted to True?

  • @rAndOmNeSs26
    @rAndOmNeSs26 4 ปีที่แล้ว

    Thank you . well explained

  • @Barrelofcrunkies12inDminor
    @Barrelofcrunkies12inDminor ปีที่แล้ว

    thanks alot mane

  • @DerickGudo
    @DerickGudo 4 ปีที่แล้ว

    What's the difference between using a for loop and a while loop for the outer loop?

  • @tjmns
    @tjmns 9 หลายเดือนก่อน

    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]

  • @tanzirhossain7159
    @tanzirhossain7159 4 ปีที่แล้ว

    Great explanation! Can you please create a python critical problem solving playlist?

  • @zeki7540
    @zeki7540 3 หลายเดือนก่อน

    Great!

  • @maamounhajnajeeb209
    @maamounhajnajeeb209 2 ปีที่แล้ว

    thank you

  • @thameemvm8571
    @thameemvm8571 5 ปีที่แล้ว

    Excellent videos...

  • @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.

  • @somarjaber1275
    @somarjaber1275 ปีที่แล้ว

    are that sublime text ? what extension you are using for text snippets and the them

  • @nathanpiedrafita9830
    @nathanpiedrafita9830 3 ปีที่แล้ว

    awesome !

  • @lez1462
    @lez1462 4 ปีที่แล้ว

    What if you wanted the list to be an input by the user?

  • @Illegal810
    @Illegal810 5 ปีที่แล้ว

    Hey, Derrick nice video as always, I am just playing all your videos looking for ideas for graduation final assignment work for my college. Maybe do you have any? It could be anything IT & finance related

    • @CodeWithDerrick
      @CodeWithDerrick  5 ปีที่แล้ว +2

      Hard to say, I always look towards things that interest me when it comes to project time. I'm super interested by the stock market, predicting supply chain demands, workflow automations, stuff of that nature. Really anything I can physically see the implications of. So a lot of the projects I work on are usually processing data and feeding it to machine learning algorithms (with some sort of visualization of the results and actions)
      There's tons of topics that I hardly ever touch that you can use python for though: Natural language processing, image recognition, robotics, chatbots, geoservices, and so much more. Let me know what you decide on and I'd be happy to help if you get stuck. Best of luck!

  • @barulli87
    @barulli87 4 ปีที่แล้ว

    can you do a trial by error type of approach?

  • @avery_3479
    @avery_3479 ปีที่แล้ว

    tysm

  • @michealarmero
    @michealarmero 5 หลายเดือนก่อน

    salamat

  • @andrewwu9550
    @andrewwu9550 4 ปีที่แล้ว

    Great vids

  • @kanchann8365
    @kanchann8365 3 ปีที่แล้ว

    I am not getting the output can anyone help. Its showing program finished. And no errors.

  • @eruiluvatar6688
    @eruiluvatar6688 4 ปีที่แล้ว

    Are you going to do one for mergesort too?

  • @dreiigHiveSGClips
    @dreiigHiveSGClips 3 ปีที่แล้ว

    thx

  • @arjay_2002
    @arjay_2002 3 ปีที่แล้ว

    time complexity?

  • @alvaroalfonso8754
    @alvaroalfonso8754 5 ปีที่แล้ว

    thanks

  • @harvanshsingh-qc4yu
    @harvanshsingh-qc4yu 5 หลายเดือนก่อน

    This code won't sort this list [5,7,2,1]. Because you not using nested loop.

  • @lux27.42
    @lux27.42 4 ปีที่แล้ว

    sir, is this course still update ?

  • @Niki-ue6fk
    @Niki-ue6fk 5 หลายเดือนก่อน

    Merge sort please

  • @Svendzeen
    @Svendzeen 4 ปีที่แล้ว +1

    I tried to do this but my code just turned my whole list into 2's lol