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
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.
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.
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. ❤💖
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
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.
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
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 !!!
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.
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.
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
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))
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
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 ?
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
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
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
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
sir, this exercise solution is not opening. How can i check my answer?????
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.
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.
I am happy this was helpful to you.
Me to bro
Your approach to problem solving is genius.
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. ❤💖
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
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.
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
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
@@joacimodhiambo1794😂
Thank you, Mr. Dhawal, I can't believe this content is free.
👍😊
I loved the way you optimised bubble sort from O(n^2) to O(n) using swapped flag
Amazing step by step explanation! Thank You Sir!
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 !!!
i have seen many bubble sort but yours is best
Hi Sir, I was having difficulty in understanding bubble sort, from your video it is crystal clear now. Thanks
Explained so well, love the way you implement DSA efficiently
holy hell. heard a lot about bubble sort. but never knew why it's called so.
thankyou once again sir for continuing with the series. Take care sir!
👍😊
very good explanation, i have been searching for a video just like this, thank You sir
Glad it helped
i love your exercises!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Loving this !!! Thank you!
excellent sort algo tutorials from bubble to selection. thank you
#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)
Sir please make videos on dynamic programming
Sure
Crystal clear tutorial!
Great Playlist - Thank you😊😊
Glad you enjoyed it
nicely explained
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.
Thank you for this excellent tutorial!
You're very welcome!
Your explanation is fantastic, sir. Respect!
Amazing tut! Keep it up :D
This is for what I pay my internet bills for
may you please share the code to the exercise the solution is empty
You were very grave at the end of the video! I miss the coronavirus prank at each video-endding! Hahaha
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'))
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)
what IDE are u using?
seems so efficient
Pycharm community edition. It is free
how to check for differnet array sizes the time complexity using time function
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.
Can you tell me why are we doing this
range(len(elements)-1-i)
Why are we doing minus i?
Solution link for the exercise problem is given by you., is not responding.. Pls provide the solution.. Thank you sir
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
yes I will add videos on that
The solution to the exercise is available, please check.
Sir, please continue with deep learning series
Yes I am making it
Hello Sir, can you please create a video developing of project using only DSA ?
Thank You
You're welcome
Bhae Thanku
why it is not if swapped, instead of if not swapped?
Interesting on the way how teach it
Can I have solutions to the assignments
exercise solution is not there
thanks
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))
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
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 ?
It's possible
Hi, can you maybe do a video on space complexity? appreciate it!
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
Keya ye Hindi main b hai
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()
i cant find solution ..help me
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
what is 'not swapped' mean? swapped is a boolean? get stucked here
we can do this is one line.why you are using long code.its useless(sorted(element,key=lambda x:x['transaction_amount']))
condition is we have to do with out using built in functions
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
Hello sir apke Hindi channel pr video upload kijiye