Bubble Sort - Data Structures & Algorithms Tutorial Python #14

Поделиться
HTML-код
  • Опубликовано: 14 дек 2024

Комментарии • 90

  • @codebasics
    @codebasics  2 года назад +2

    Do you want to learn python from me with a lot of interactive quizzes, and exercises? Here is my project-based python learning course: codebasics.io/courses/python-for-beginner-and-intermediate-learners

    • @parassirohi8305
      @parassirohi8305 2 года назад

      sir, this exercise solution is not opening. How can i check my answer?????

  • @alexlu2
    @alexlu2 10 месяцев назад +6

    Hi, if you prefer to make the code a bit shorter, there is actually no need to use a tmp variable.
    Instead of:
    tmp = elements[j]
    elements[j] = elements[j+1]
    elements[j+1] = tmp
    Use:
    elements[j], elements[j+1] = elements[j+1], elements[j]
    This is a neat trick in python that allows the value of two variables to be swapped.

  • @naveenchahar8074
    @naveenchahar8074 3 года назад +9

    for many months I delayed learning DS but since I have found your channel, now DS seems really easy. Specially the exercise part I like most. Thankyou Sir from the depth of my heart. Saviour of students like me.

  • @iwuomasolomonuched518
    @iwuomasolomonuched518 7 месяцев назад

    Your approach to problem solving is genius.

  • @tharindusathsara3414
    @tharindusathsara3414 Год назад +6

    Really very happy about finding such an obvious and understood funny video series about data structures and algorithms. Everything is 100% clear with deeply explained theories and well-understood practicals. Also, the exercise series with the videos are highly appreciated. Dear sir thank you so much for the fantastic video series. ❤💖

  • @rajmalla5310
    @rajmalla5310 Год назад

    U are true guru, people only explain how to do it but they do not explain why. And only a true teacher would explain both how and why. I didn't understand why that -i part, but u made it clear. thankyou so much SIrr

  • @danieltaylor7040
    @danieltaylor7040 3 года назад +11

    The swapped flag is a good catch on making sure best case is O(n). Most other implementations I've seen do not have this flag, and thus don't take advantage of a pre-sorted array.

  • @jagadeeshchowdary481
    @jagadeeshchowdary481 3 года назад +3

    easy solution for above exercise question
    def bubblesort(elements, key="name"):
    size = len(elements)
    for i in range(size-1):
    for j in range(size-1):
    if (elements[j])[key] > (elements[j+1])[key]:
    tmp = elements[j]
    elements[j] = elements[j+1]
    elements[j+1] = tmp
    return elements

    • @joacimodhiambo1794
      @joacimodhiambo1794 2 года назад

      def bubble_sort(val, key='name'):
      size = len(val)
      for i in range(size - 1):
      for j in range(size - 1 - i):
      if val[j][key] > val[j + 1][key]:
      tmp = val[j][key]
      val[j][key] = val[j + 1][key]
      val[j + 1][key] = tmp
      return val

    • @Manikanta-lc5zu
      @Manikanta-lc5zu 3 месяца назад

      ​@@joacimodhiambo1794😂

  • @LokeshKumar-dd2qw
    @LokeshKumar-dd2qw 4 года назад +1

    Thank you, Mr. Dhawal, I can't believe this content is free.

  • @hrushikeshkulkarni7353
    @hrushikeshkulkarni7353 2 года назад +3

    I loved the way you optimised bubble sort from O(n^2) to O(n) using swapped flag

  • @gaganbhattacharya9890
    @gaganbhattacharya9890 Год назад

    Amazing step by step explanation! Thank You Sir!

  • @奶油雞雞
    @奶油雞雞 3 года назад +2

    sir, thanks a lot!! It's solved my n-1-i problem.
    some books said it was actually i+1 and written as n-1-i in code, and i was so confused.
    now i know its cause un-efficient for loop to continue, so we need n-1-i to shorten the loop when the last digit was found , thank you !!!

  • @techsavvyinfo
    @techsavvyinfo 2 года назад

    i have seen many bubble sort but yours is best

  • @miteshbarnwal8239
    @miteshbarnwal8239 Год назад

    Hi Sir, I was having difficulty in understanding bubble sort, from your video it is crystal clear now. Thanks

  • @ij953
    @ij953 2 года назад

    Explained so well, love the way you implement DSA efficiently

  • @sukurcf
    @sukurcf 3 года назад +3

    holy hell. heard a lot about bubble sort. but never knew why it's called so.

  • @tripathi5174
    @tripathi5174 4 года назад +1

    thankyou once again sir for continuing with the series. Take care sir!

  • @trendinggameplay99
    @trendinggameplay99 2 года назад +1

    very good explanation, i have been searching for a video just like this, thank You sir

  • @anirbanc88
    @anirbanc88 2 года назад

    i love your exercises!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  • @murciadaniel
    @murciadaniel 4 года назад +3

    Loving this !!! Thank you!

  • @treksis
    @treksis 3 года назад

    excellent sort algo tutorials from bubble to selection. thank you

  • @Codingiisgreat
    @Codingiisgreat 3 года назад +1

    #Bubble Sort
    def bubble_sort(elements,key):
    size = len(elements)
    for i in range(size-1):
    swapped = False
    for j in range(size-1-i):
    if elements[j][key] > elements[j+1][key]:
    elements[j], elements[j+1] = elements[j+1], elements[j]
    swapped = True
    if not swapped:
    break
    if __name__ == '__main__':
    elements = [
    {'name': 'kathy', 'transaction_amount': 200, 'device': 'vivo'},
    {'name': 'dhaval', 'transaction_amount': 400, 'device': 'google pixel'},
    {'name': 'aamir', 'transaction_amount': 800, 'device': 'iphone-8'},
    {'name': 'mona', 'transaction_amount': 1000, 'device': 'iphone-10'},
    ]
    bubble_sort(elements,'transaction_amount')
    print(elements)

  • @bigbang3235
    @bigbang3235 4 года назад +6

    Sir please make videos on dynamic programming

  • @skoushik3799
    @skoushik3799 3 года назад

    Crystal clear tutorial!

  • @ranjithkumarneeruganti4710
    @ranjithkumarneeruganti4710 3 года назад +1

    Great Playlist - Thank you😊😊

  • @RahulSharma-lw2ss
    @RahulSharma-lw2ss Год назад

    nicely explained

  • @sayanjitb
    @sayanjitb 3 года назад

    Dear sir, I have done the last exercise on my own as follows and worked successfully:
    -----------------------------------------------------------------------------------------------
    def bubble_sort(arr,key='name'):
    u=[ ] #empty list
    size=len(arr)
    for k in range(len(arr)):
    u.append(arr[k].get(key))
    for i in range(size-1):
    swapped=False
    for j in range(size-1-i):
    if u[j] > u[j+1]:
    temp=u[j]
    u[j]=u[j+1]
    u[j+1]=temp
    swapped=True
    if not swapped:
    break
    return u
    If you kindly give any suggestion, it would be very much helpful to me.

  • @nan4061
    @nan4061 3 года назад

    Thank you for this excellent tutorial!

  • @a.human.
    @a.human. 2 года назад

    Your explanation is fantastic, sir. Respect!

  • @thamidurandilbandara415
    @thamidurandilbandara415 3 года назад

    Amazing tut! Keep it up :D

  • @codewithprinsa5030
    @codewithprinsa5030 2 дня назад

    This is for what I pay my internet bills for

  • @vation_roofing
    @vation_roofing 3 года назад +1

    may you please share the code to the exercise the solution is empty

  • @johnk9621
    @johnk9621 3 года назад

    You were very grave at the end of the video! I miss the coronavirus prank at each video-endding! Hahaha

  • @Schadenfreude596
    @Schadenfreude596 3 месяца назад

    My solution for exercise
    def bubble_sort(elements, key):
    size = len(elements) - 1
    for _ in range(size):
    swapped = False
    for i in range(size):
    if elements[i][key] > elements[i+1][key]:
    elements[i][key], elements[i+1][key] = elements[i+1][key], elements[i][key]
    swapped = True
    if not swapped:
    break
    return elements
    elements = [
    { 'name': 'mona', 'transaction_amount': 1000, 'device': 'iphone-10'},
    { 'name': 'dhaval', 'transaction_amount': 400, 'device': 'google pixel'},
    { 'name': 'kathy', 'transaction_amount': 200, 'device': 'vivo'},
    { 'name': 'aamir', 'transaction_amount': 800, 'device': 'iphone-8'},
    ]
    print(bubble_sort(elements, key='name'))

  • @gauravgirase8011
    @gauravgirase8011 3 года назад

    def bubblesort(elements,param):
    for i in range(len(elements)-1):
    for j in range(i+1,len(elements)):
    data=elements[i]
    transaction_amount=data[param]
    data2=elements[j]
    transaction_amount2=data2[param]
    if transaction_amount>transaction_amount2:
    elements[i],elements[j]=elements[j],elements[i]
    return elements
    elements = [
    { 'name': 'mona', 'transaction_amount': 1000, 'device': 'iphone-10'},
    { 'name': 'dhaval', 'transaction_amount': 400, 'device': 'google pixel'},
    { 'name': 'kathy', 'transaction_amount': 200, 'device': 'vivo'},
    { 'name': 'aamir', 'transaction_amount': 800, 'device': 'iphone-8'},
    ]
    param="name"
    result=bubblesort(elements,param)
    print(result)

  • @zihaojiao1304
    @zihaojiao1304 4 года назад +1

    what IDE are u using?
    seems so efficient

    • @codebasics
      @codebasics  4 года назад

      Pycharm community edition. It is free

  • @danyalshah7009
    @danyalshah7009 3 года назад

    how to check for differnet array sizes the time complexity using time function

  • @mattcowan2150
    @mattcowan2150 2 года назад

    Looks like the solution for the exercise is no longer available. Can you please fix this? I like being able to compare the solution I come up with to your solution and see ways to improve my program.

  • @AbrarKhan-cc7te
    @AbrarKhan-cc7te 3 года назад

    Can you tell me why are we doing this
    range(len(elements)-1-i)
    Why are we doing minus i?

  • @Rsn8247
    @Rsn8247 3 года назад

    Solution link for the exercise problem is given by you., is not responding.. Pls provide the solution.. Thank you sir

  • @pythonenthusiast9292
    @pythonenthusiast9292 4 года назад

    plans on competetive programming for interviews? This is channel focusses on data science but your videos are simply awesome .. please try to make that also if possible

    • @codebasics
      @codebasics  4 года назад +1

      yes I will add videos on that

  • @joacimodhiambo1794
    @joacimodhiambo1794 2 года назад

    The solution to the exercise is available, please check.

  • @akshaykumaredayilliam8708
    @akshaykumaredayilliam8708 4 года назад

    Sir, please continue with deep learning series

  • @meralmaradia4774
    @meralmaradia4774 2 года назад

    Hello Sir, can you please create a video developing of project using only DSA ?

  • @namratavidolkar4426
    @namratavidolkar4426 3 года назад

    Thank You

  • @lovelytyagi1041
    @lovelytyagi1041 3 года назад

    Bhae Thanku

  • @joxa6119
    @joxa6119 Год назад

    why it is not if swapped, instead of if not swapped?

  • @AMM2012
    @AMM2012 3 года назад

    Interesting on the way how teach it

  • @mxolisimafu9902
    @mxolisimafu9902 3 года назад

    Can I have solutions to the assignments

  • @vivekkumar-fe1xr
    @vivekkumar-fe1xr 3 года назад

    exercise solution is not there

  • @madhurireddy7418
    @madhurireddy7418 3 года назад

    thanks

  • @jyotiphulwariya6723
    @jyotiphulwariya6723 4 года назад +1

    def sorti(lst):
    size=len(lst)
    while lst!=sorted(lst):
    swap=False
    for i in range(size - 1):
    if lst[i] > lst[i + 1]:
    s = lst[i]
    lst[i] = lst[i + 1]
    lst[i + 1] = s
    swap=True
    if not swap:
    break
    return lst
    a=[9,2,12,4,45,56,1,2,3,90,0]
    print(sorti(a))

    • @naveenchahar8074
      @naveenchahar8074 3 года назад

      hey Jyoti I can understand if your using sorted(lst) then what' the point of using bubble sort. In that case your are sorting list with sorted and then you comparing sorted list with array/list. which results in double time complexity

  • @ranu__12
    @ranu__12 4 года назад +2

    Is there any Problem in using a,b= b,a concept in python for swapping two values ?? I am asking about internal memory management of python ... Would this operation require more memory than creating a temp variable or less ?

  • @soufyanr3745
    @soufyanr3745 4 года назад +3

    Hi, can you maybe do a video on space complexity? appreciate it!

  • @HaidarKaramat
    @HaidarKaramat 10 дней назад

    def bubble_sort(elements, keys=None):
    if keys is not None:
    for element in elements:
    if keys in element:
    print(f"key values of elements:{element[keys]}")
    else:
    print(f"The values of element:{element}")
    size = len(elements)
    for i in range(size-1):
    swapped = False
    for j in range(size-1-i):
    if elements[j][keys] > elements[j+1][keys]:
    elements[j], elements[j+1] = elements[j+1], elements[j]
    swapped = True
    if not swapped:
    break
    return elements
    if __name__ == '__main__':
    elements = [
    {'name': 'mona', 'transaction_amount': 1000, 'device': 'iphone-10'},
    {'name': 'dhaval', 'transaction_amount': 400, 'device': 'google pixel'},
    {'name': 'kathy', 'transaction_amount': 200, 'device': 'vivo'},
    {'name': 'aamir', 'transaction_amount': 800, 'device': 'iphone-8'}
    ]
    sorted_element_by_transaction = bubble_sort(elements,keys='transaction_amount')
    print(sorted_element_by_transaction)
    sorted_element_by_name = bubble_sort(elements,keys='name')
    print(sorted_element_by_name)
    sorted_element_by_device = bubble_sort(elements,keys='device')
    print(sorted_element_by_device)
    Thats my solution gives me feedback

  • @syedtajamalihsanattari7158
    @syedtajamalihsanattari7158 Год назад

    Keya ye Hindi main b hai

  • @KhelGyanOfficial
    @KhelGyanOfficial 3 года назад +1

    Solution page is not opening.So I am pasting my code here :-
    def bubble_sort(elements,key):
    size=len(elements)
    IsSwapped=0
    for i in range(size-1):
    for j in range(size-1-i):
    if elements[j][key] > elements[j+1][key]:
    tmp=elements[j][key]
    elements[j][key]=elements[j+1][key]
    elements[j+1][key]=tmp
    IsSwapped=1
    if IsSwapped==0:
    break
    def main():
    elements = [
    { 'name': 'mona', 'transaction_amount': 1000, 'device': 'iphone-10'},
    { 'name': 'dhaval', 'transaction_amount': 400, 'device': 'google pixel'},
    { 'name': 'kathy', 'transaction_amount': 200, 'device': 'vivo'},
    { 'name': 'aamir', 'transaction_amount': 800, 'device': 'iphone-8'}]
    bubble_sort(elements,'transaction_amount')
    print(elements)

    bubble_sort(elements,'name')
    print(elements)

    if __name__ == "__main__": main()

  • @shefalishetty6263
    @shefalishetty6263 3 года назад

    i cant find solution ..help me

    • @manchitmadan5599
      @manchitmadan5599 3 года назад

      def bubble_sort_by_key(arr, key='transaction_amount'):
      size = len(arr)
      for i in range(size-1):
      swapped = False
      for j in range(size-1-i):
      if arr[j][key] > arr[j+1][key]: #modified this line only
      temp = arr[j]
      arr[j] = arr[j+1]
      arr[j+1] = temp
      swapped = True
      if not swapped:
      break

  • @zhengxijiang4087
    @zhengxijiang4087 2 года назад

    what is 'not swapped' mean? swapped is a boolean? get stucked here

  • @sobersabin
    @sobersabin 3 года назад

    we can do this is one line.why you are using long code.its useless(sorted(element,key=lambda x:x['transaction_amount']))

    • @Rajkumar-vc2pg
      @Rajkumar-vc2pg 3 года назад

      condition is we have to do with out using built in functions

  • @ravichandra9920
    @ravichandra9920 Месяц назад

    u need to socialize with everyone at work..this concept works only someone who wants to do more than work..hope u get my point..create a business model which works for everyone..may be u are not married and dont have kids going school.its crazy idea but seems odd

  • @satwikgawas5582
    @satwikgawas5582 4 года назад

    Hello sir apke Hindi channel pr video upload kijiye