Btw there is no need to take a temp variable for swapping. Python allows a very easy swapping method, just use: num[ j ] , num[ j+1 ] = num[ j+1] , num[ j ]
This is why Phython called scripting language. The syntax you using is using sort function underneath .. basically its doing all steps there in bavkground.
Navin you are just exceptional. I have been cracking my head about this up until now I watched this tutorial. It strange when some tutors claim they teach to beginners and use complex words. Thank you Navin
i did it like this . it works def bubbleSwap(lst): x, y = 0, 0 h = 0 while h < len(lst): for i in range(1,len(lst)): if lst[i - 1] > lst[i]: x, y = lst[i - 1], lst[i] lst[i - 1] = y lst[i] = x h += 1 return lst
I want to express my heartfelt gratitude for all the effort you put into simplifying the topics. Your presence and the way you explain things in the videos bring us joy and radiate positive energy. I hope you continue in this way, and I wish you all the best in everything you do.
This is a really good explanation. I can suggest you to use while loop as well to break out of the for loop if the items in the list are already sorted maybe in the 2nd or 3rd iteration. In that case you will not need to loop through n times.
Finally I did with one loop.. Num=[3,6,2,16,11,17,8] Def sort(list): For i in range(len(num)-1): If num[I] > num[I+1]: Num[I],num[I+1] = num[I+1],num[I] Sort(num) Sort(num) Print (num)
def Bubble_sort(n): for i in range(len(n)-1,0,-1): for j in range(i): if n[j]>n[j+1]: n[j],n[j+1]=n[j+1],n[j] print(n) c=int(input("Enter the number of elements in list:")) n=[] for k in range(c): d=int(input("Enter an value:")) n.append(d) print(n) print("The sorted list is",Bubble_sort(n)) "small modification"
throw back to 4 years ago when you were learning from this video any outputs now? what are you doing now after 4 years are you a full fledged python developer
U have one glass of juice And a cup of tea If u want to shift (swap) both . U need a cup called temp U put juice in temp Then tea to empty juice glass And finally juice to cup .
lol he also knew that, its just in other languages u dnt have the luxary of swapping this way thats why, once u know the logic u can implement it anywhere you want
@@kaushalJainer It is but than you have to remember the variables which have been swapped which will become a more complicated thing. As you have to put b where you are putting a after swapping and vice versa.... If you are putting a simple logic it is ok and will definitely run on any python software...
Maybe in some cases it will work, but not always. What this "line of code" does is it reverses a list and you get a something like this : list = [3,2,5,1] - before sorting, and after reversing it, you get list = [1,5,2,3]. If i'm not right, please, let me know :)
@@bonysmoke9190 it works in all cases dude. its a python one liner. Because of the way python is build there are tons of one liner, In this case the in the video he use an extra variable temp and 3 lines of code, which can be done with the one liner.
He was probably trying to teach an algorithm that would apply to all programming languages where a temporary variable is required for swapping. Not all programming languages will support a,b=b,a.
Here's the easiest way: arr = [] a = int(input("Enter the no of elements")) for i in range(a): x = int(input()) arr.append(x) for i in range(a-1): for j in range(a-i-1): if arr[j] > arr[j+1]: arr[j], arr[j+1] = arr[j+1], arr[j] print("Sorted array is:", arr)
for the outer loop you can also do: for i in reversed(range(len(nums))): and for swapping you can do this: if nums[j] > nums[j+1]: nums[j], nums[j+1] = nums[j+1], nums[j]
#Some changes applied def sort(list): for i in range(len(list)-1,0,-1): for j in range(i): if list[j] > list[j+1]: #look here list[j],list[j+1] = list[j+1],list[j] nums = [4,7,3,6,2,3,1] sort(nums) print(nums)
def bubble_sort(seq): min_value = 0 for i in range(len(seq)-1): for j in range(i): if seq[j] > seq[j+1]: seq[j],seq[j+1] = seq[j+1],seq[j] return seq is this considered as bubble sort, as in this the sorting is done by taking out the minimum values just like selection sort but with more iterations.
Hi telusko. The outer loop and inner loop s range is a bit confusing for me. I just made the range as simple and it works but IDK whether is this right or not! def sort(nums): for i in range(len(nums)-1): for j in range(len(nums)-1): if(nums[j]>nums[j+1]): temp = nums[j] nums[j] = nums[j+1] nums[j+1] = temp return nums nums = [9,0,1,8,2,7,3,12,5] result = sort(nums) print(result)
We have covered swapping in an earlier lesson in detail. There we discussed 3 methods as I remembered. So it is time to put that knowledge to work, without using a 3rd variable. (I learnt this from you) nums[j], nums[j+1] = nums[j+1], nums[j]
Navin can we do it this way? - we create a empty list and do linear search for the greatest value or lowest value and append it to the empty list and remove it from the original list .....do this a few more times and the original list becomes empty and the empty list becomes sorted....i tried and it worked so i thought commenting would help other programmers get different ideas on how to solve the same problem...
why take second for loop. can't we use if function directly by direct puttind swap function. for i in range(len(list)): if list[i]>list[i+1]: list[i], list[i+1] = list[i+1], list[i]
if we use only one for loop, only one value sorted to last like below [3, 5, 6, 2, 4, 8] to repeat same thing we need to use second for loop. yes we can use swap a,b= b,a def sort(list): for i in range(len(list)-1,0,-1): for j in range(i): if list[j] > list[j+1]: list[j],list[j+1]=list[j+1],list[j] nums=[5,3,6,8,2,4] sort(nums) print(nums)
@@sagarrao79 Please explain this to me: for i in range(len(list)-1,0,-1): I am so confused as to how this works and can't seem to figure it out on my own. Thanks
@@alexw.e.1390we need to check the values from start till end. when i=5(in this program) j runs from 0 till 5 and the largest value, i.e 8 gets shifted to last place. now i becomes 4( since -1) and j runs from 0 to 4 and the next largest value gets shifted to second last place. this goes on until nums is sorted. i is taken in reverse so that j can check until last value. if this didn't help please debug the program. It will help you understand what is happening step by step.
def sort(self): for i in range(a-1,0,-1): for j in range(i): if list[j]>list[j+1]: temp=list[j] list[j]=list[j+1] list[j+1]=temp a=int(input("Enter the no. of elements you want to enter in the list ")) list=[] for i in range(a): li=int(input()) list.append(li) print("Unsorted list is: ",list) sort(list) print ("Sorted list is: ",list)
No need of temp..Directly swapping def sort(lst): for i in range(len(lst)-1,0,-1): for j in range(i): if lst[j]>lst[j+1]: lst[j],lst[j+1]=lst[j+1],lst[j] print[lst] lst=[3,2,6,4,9,7,5] sort(lst)
I've found more elegant to substitute lines 6, 7 and 8 with list[j], list[j + 1] = list[j + 1], list[j] (i've learned that from my favourite Python on-line teacher 😉)
array = [13,147,5,80] size = len(array) for i in range(0,size): for j in range(i+1,size): if array[j] < array[i]: min = array[j] array[j] = array[i] array[i] = min print(array)
in the function sort, nums is local variable and is valid only for that particular suite. So how does changes in nums in sort function will reflect in nums outside sort? Could you please explain this?
Sir I have a doubt I assumed that a=bin(7) type(a) o/p Then I assume a=0b111 type(a) o/p = How sir both a= bin(7) = 0b111 are equal. But how that is str it is int becz both of the value is 0b111
In Fact, We don't need to type complex logic: for i in range(len(nums)-1,0,-1): for j in range(i): if nums[j] > nums[j+1]: nums[j],nums[j+1] = nums[j+1],nums[j] Instead we can type like this: for i in range(len(nums)-1,0,-1): for j in range(i): if nums[j] > nums[j+1]: nums[j],nums[j+1] = nums[j+1],nums[j]
def sorter(list): b = len(list)-1 is_finished = False while not is_finished: a = 0 is_finished = True while a < b: if list[a] > list[a+1]: list[a], list[a+1] = list[a+1], list[a] is_finished = False a += 1 b -= 1 return list
def lsearch(a,b): m=0 global i for i,j in enumerate(a): m=m+1 if b==j: return True break else: return False a=[22,33,44,55,66,77,88,99] b=66 l=lsearch(a,b) if lsearch(a,b): print('found') print('position is ', i + 1) else: print('not found')
I am searching for all the sorting algorithms in java telusko. But it's getting python... Can u provide the Bubble sort link in java. Now I became an addict of telusko java learning. It's really Amazing
you could also have a variable = 0 at the start of every iteration and inciment it by 1 when you swap, therefore if at the end of an iteration the variable is 0 means the list is sorted and you don't have to try to sort the rest of the already sorted list
Yeh.. did it before watching ur code - def sort(nums): for e in range(len(nums)-1): for i in range(len(nums)-1): if nums[i]>nums[i+1]: temp=nums[i] nums[i]=nums[i+1] nums[i+1]=temp else: pass nums =[5,3,8,6,7,2] sort(nums) print(nums)
This guys should've been our university teacher. He's so good in explaining complex topics. Thanks a lot Navin Reddy.
he wouldve called his students aliens.
Btw there is no need to take a temp variable for swapping.
Python allows a very easy swapping method, just use:
num[ j ] , num[ j+1 ] = num[ j+1] , num[ j ]
👍
I Need help @nikita
he has mentioned this code in his series but he is going with old school approach for who are watching bubble sort directly
Thanx ❤️
This is why Phython called scripting language. The syntax you using is using sort function underneath .. basically its doing all steps there in bavkground.
Navin you are just exceptional. I have been cracking my head about this up until now I watched this tutorial. It strange when some tutors claim they teach to beginners and use complex words. Thank you Navin
we can directly swap the values
no need to temp variable
if arr[j] > arr[j+1] :
arr[j], arr[j+1] = arr[j+1], arr[j]
haha...>>
a,b=b,a
Yea, it can be done
but you can only do it in python
Himanshu Rajput this is a python course, that’s the whole point lol
I guess sir is trying to tell the common method of bubble sort rather than just telling how to do this in python
This is the best bubble explanation I have heard in my research. Keep it up . Good work 👍🏾
Did he just call me an Alien?
yup
i was thinking this in previous video.
Ya
But why???
He is calling us alien since his first day python lecture.
Your editing and speaking skills are amazing.
Straight to the point and really showcases what you need to understand.
Good job!
Telusko, you really deserve a thumbs up. DSA never gets easier than this!!!
def sort(nums):
for i in range(len(nums) - 1, 0, -1):
for j in range (i):
if nums[j] < nums[j + 1]:
continue
else:
nums[j+1], nums[j] = nums[j], nums[j+1]
nums = [5, 8, 9, 71, 10, 2,589,1,0]
sort(nums)
print(nums)
i did it like this . it works
def bubbleSwap(lst):
x, y = 0, 0
h = 0
while h < len(lst):
for i in range(1,len(lst)):
if lst[i - 1] > lst[i]:
x, y = lst[i - 1], lst[i]
lst[i - 1] = y
lst[i] = x
h += 1
return lst
I want to express my heartfelt gratitude for all the effort you put into simplifying the topics. Your presence and the way you explain things in the videos bring us joy and radiate positive energy. I hope you continue in this way, and I wish you all the best in everything you do.
Are you a girl?
It had been very very useful for my son during lockdown
Thank you sir😊☺️
This is a really good explanation. I can suggest you to use while loop as well to break out of the for loop if the items in the list are already sorted maybe in the 2nd or 3rd iteration. In that case you will not need to loop through n times.
It save me the day to learn this in 10mins! Million thanks!!
Finally I did with one loop..
Num=[3,6,2,16,11,17,8]
Def sort(list):
For i in range(len(num)-1):
If num[I] > num[I+1]:
Num[I],num[I+1] = num[I+1],num[I]
Sort(num)
Sort(num)
Print (num)
This is just sort not a bubble sort
you are amazing took me 7 mins to understand what i have been trying to understand for days
def Bubble_sort(n):
for i in range(len(n)-1,0,-1):
for j in range(i):
if n[j]>n[j+1]:
n[j],n[j+1]=n[j+1],n[j]
print(n)
c=int(input("Enter the number of elements in list:"))
n=[]
for k in range(c):
d=int(input("Enter an value:"))
n.append(d)
print(n)
print("The sorted list is",Bubble_sort(n))
"small modification"
thank you sir . tomorrow is my practicals and i have learned from from this video than i have learnt in my entire semister
throw back to 4 years ago when you were learning from this video
any outputs now? what are you doing now after 4 years are you a full fledged python developer
Instead of using 3rd variable directly we can swap two values nums[j],nums[j+1]=nums[j+1],nums[j]
Navin Reddy, you are my hero.
You are a savior in every single way! (I refer to all of your tutorials!) So grateful that you are doing these tutorials!
I do the same. He is amazing
Just outstanding. Love from Pakistan.
Nice sir.. waiting for your video..
Im dumb at coding so this is a life saver when I can't understand certain parts of the code.
why to make swapping complicated, when you can do this:
a,b=b,a
U have one glass of juice
And a cup of tea
If u want to shift (swap) both .
U need a cup called temp
U put juice in temp
Then tea to empty juice glass
And finally juice to cup .
@@nawazmalik5435 no you don't. in python, you can do it without an extra cup. Xanthunal Warrior's way is correct and better.
lol he also knew that, its just in other languages u dnt have the luxary of swapping this way thats why, once u know the logic u can implement it anywhere you want
@@kaushalJainer It is but than you have to remember the variables which have been swapped which will become a more complicated thing. As you have to put b where you are putting a after swapping and vice versa....
If you are putting a simple logic it is ok and will definitely run on any python software...
Due to the differences in Cs and Ip
python is one line code lang
so swap will be num[j],num[j+1]=num[j+1],num[j]
Maybe in some cases it will work, but not always. What this "line of code" does is it reverses a list and you get a something like this : list = [3,2,5,1] - before sorting, and after reversing it, you get list = [1,5,2,3]. If i'm not right, please, let me know :)
@@bonysmoke9190 it works in all cases dude. its a python one liner. Because of the way python is build there are tons of one liner, In this case the in the video he use an extra variable temp and 3 lines of code, which can be done with the one liner.
He was probably trying to teach an algorithm that would apply to all programming languages where a temporary variable is required for swapping. Not all programming languages will support a,b=b,a.
sir, pls make a video on insertion sort
vowwww.....I never understood it i college but u made my life easy.
*wowwww..... I never understood it in college but you made my life easy.
Eagerly waiting sir,
You are awesome,,
I'm going to give you the name "KNOWLEDGE STACK "
love from delhi
Best explanation ever❤
def Bubble(list_a):
indexing_length = len(list_a) - 1
sorted = False
while not sorted:
sorted = True
for i in range(0, indexing_length):
if list_a[i] > list_a[i + 1]:
sorted = False
list_a[i], list_a[i + 1] = list_a[i + 1], list_a[i]
return list_a
print(Bubble([5, 3, 8, 6, 7, 2]))
def sort(a):
for i in range(len(a)-1,0,-1):
for j in range(i):
if a[j] > a[i]:
a[i], a[j] = a[j], a[i]
this also works same : )
Here's the easiest way:
arr = []
a = int(input("Enter the no of elements"))
for i in range(a):
x = int(input())
arr.append(x)
for i in range(a-1):
for j in range(a-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
print("Sorted array is:", arr)
could u please explain the a-i-1 part please
sir you solve my doubt in just 7 minutues just like laminar flow of water
def sort(nums):
for index1,value1 in enumerate(nums):
for index2,value2 in enumerate(nums):
if value1
for the outer loop you can also do:
for i in reversed(range(len(nums))):
and for swapping you can do this:
if nums[j] > nums[j+1]:
nums[j], nums[j+1] = nums[j+1], nums[j]
#Some changes applied
def sort(list):
for i in range(len(list)-1,0,-1):
for j in range(i):
if list[j] > list[j+1]:
#look here
list[j],list[j+1] = list[j+1],list[j]
nums = [4,7,3,6,2,3,1]
sort(nums)
print(nums)
Thank you for showing in user defined module 😊
Mind blowing explanation!
we could use :am = [5,8,2,4,8,1]
aw = sorted(am)
print(aw)
4:00
def bubble_sort(seq):
min_value = 0
for i in range(len(seq)-1):
for j in range(i):
if seq[j] > seq[j+1]:
seq[j],seq[j+1] = seq[j+1],seq[j]
return seq
is this considered as bubble sort, as in this the sorting is done by taking out the minimum values just like selection sort but with more iterations.
Love from Bangladesh ❤❤
Thank you sir .....sir will you cover all data structure concepts????
No sir will not
@@tusharkaushik9554 lol
Hi telusko. The outer loop and inner loop s range is a bit confusing for me.
I just made the range as simple and it works but IDK whether is this right or not!
def sort(nums):
for i in range(len(nums)-1):
for j in range(len(nums)-1):
if(nums[j]>nums[j+1]):
temp = nums[j]
nums[j] = nums[j+1]
nums[j+1] = temp
return nums
nums = [9,0,1,8,2,7,3,12,5]
result = sort(nums)
print(result)
Perfect! Thank you so much!!
li=[5,3,8,6,7,2]
for i in range(len(li)):
for j in range(len(li)-1):
temp=0
if li[j]>li[j+1]:
li[j],li[j+1]=li[j+1],li[j]
print(li)
thank ypu so much sir yu explained each detail ^^
We have covered swapping in an earlier lesson in detail. There we discussed 3 methods as I remembered.
So it is time to put that knowledge to work, without using a 3rd variable. (I learnt this from you)
nums[j], nums[j+1] = nums[j+1], nums[j]
thank you very much Sir really, you helped me so much with this
thank you so much sir.i will be thankful to you.with the help of you i easily learn python
sir u r awsomee i love this python tutorials and i shared to my friends
Thanks a lot you made it so simple ❤❤
Navin can we do it this way? - we create a empty list and do linear search for the greatest value or lowest value and append it to the empty list and remove it from the original list .....do this a few more times and the original list becomes empty and the empty list becomes sorted....i tried and it worked so i thought commenting would help other programmers get different ideas on how to solve the same problem...
very helpful sir thank you
Sir you are giving my quarry answer
Thanks for that I have find my answer without your help
Also Using sorted function
N=[1,4,5,5,7, 8,7,5,9,7]
Print (sorted(N))
Python provide us inbuilt function
why take second for loop. can't we use if function directly by direct puttind swap function.
for i in range(len(list)):
if list[i]>list[i+1]:
list[i], list[i+1] = list[i+1], list[i]
if we use only one for loop, only one value sorted to last like below
[3, 5, 6, 2, 4, 8]
to repeat same thing we need to use second for loop.
yes we can use swap a,b= b,a
def sort(list):
for i in range(len(list)-1,0,-1):
for j in range(i):
if list[j] > list[j+1]:
list[j],list[j+1]=list[j+1],list[j]
nums=[5,3,6,8,2,4]
sort(nums)
print(nums)
@@sagarrao79 Please explain this to me: for i in range(len(list)-1,0,-1): I am so confused as to how this works and can't seem to figure it out on my own.
Thanks
@@alexw.e.1390we need to check the values from start till end. when i=5(in this program) j runs from 0 till 5 and the largest value, i.e 8 gets shifted to last place. now i becomes 4( since -1) and j runs from 0 to 4 and the next largest value gets shifted to second last place. this goes on until nums is sorted. i is taken in reverse so that j can check until last value.
if this didn't help please debug the program. It will help you understand what is happening step by step.
superb sir
def sort(self):
for i in range(a-1,0,-1):
for j in range(i):
if list[j]>list[j+1]:
temp=list[j]
list[j]=list[j+1]
list[j+1]=temp
a=int(input("Enter the no. of elements you want to enter in the list "))
list=[]
for i in range(a):
li=int(input())
list.append(li)
print("Unsorted list is: ",list)
sort(list)
print ("Sorted list is: ",list)
What's difference between bubble and insertion sorting
No need of temp..Directly swapping
def sort(lst):
for i in range(len(lst)-1,0,-1):
for j in range(i):
if lst[j]>lst[j+1]:
lst[j],lst[j+1]=lst[j+1],lst[j]
print[lst]
lst=[3,2,6,4,9,7,5]
sort(lst)
Ots good work sir, keep.making more videos on python
Awesome explanations 👍👌👏👏
Great topics and very good explanation ❤
How we are getting the sorted values as we are not returning values from sort function?
I've found more elegant to substitute lines 6, 7 and 8 with
list[j], list[j + 1] = list[j + 1], list[j]
(i've learned that from my favourite Python on-line teacher 😉)
It works man thanks, its like a short cut
Learn code academy
print(sorted(nums)) works just fine
This is perfect thank you
5:14 sir we are going from 5 to 0 right?
yes bro
0 is for index no. But here we consider as 1 for himan understanding
I absolutely love this dude idek why
sir you are great !!
Can you give tutorial of c++ for beginners
array = [13,147,5,80]
size = len(array)
for i in range(0,size):
for j in range(i+1,size):
if array[j] < array[i]:
min = array[j]
array[j] = array[i]
array[i] = min
print(array)
Can u explain the inner loop
Sir, you are the best teacher, but why do you tell programmers "Aliens"?
Pls explain
now i know why the swap video was inserted in middle 😁😁😂
in the function sort, nums is local variable and is valid only for that particular suite. So how does changes in nums in sort function will reflect in nums outside sort? Could you please explain this?
Num is passed in sort so we can use it as local variable
Sir I have a doubt
I assumed that
a=bin(7)
type(a)
o/p
Then I assume
a=0b111
type(a)
o/p =
How sir both a= bin(7) = 0b111 are equal. But how that is str it is int becz both of the value is 0b111
THANK YOU SO MUCH SIR
In Fact, We don't need to type complex logic:
for i in range(len(nums)-1,0,-1):
for j in range(i):
if nums[j] > nums[j+1]:
nums[j],nums[j+1] = nums[j+1],nums[j]
Instead we can type like this:
for i in range(len(nums)-1,0,-1):
for j in range(i):
if nums[j] > nums[j+1]:
nums[j],nums[j+1] = nums[j+1],nums[j]
Thanks a lot senor!
FOR SORTING IN ASCENDING ORDER
ls = [7, 3, 3, 6, 5, 9, 2]
ls.sort()
print(ls)
amazing
very good
This can be done through the normal sorting method which u explained in the lists concept
def sorter(list):
b = len(list)-1
is_finished = False
while not is_finished:
a = 0
is_finished = True
while a < b:
if list[a] > list[a+1]:
list[a], list[a+1] = list[a+1], list[a]
is_finished = False
a += 1
b -= 1
return list
Sir do you have live classes for ds in python or notes kindly reply
Clean 👌
def lsearch(a,b):
m=0
global i
for i,j in enumerate(a):
m=m+1
if b==j:
return True
break
else:
return False
a=[22,33,44,55,66,77,88,99]
b=66
l=lsearch(a,b)
if lsearch(a,b):
print('found')
print('position is ', i + 1)
else:
print('not found')
Thanks bro this video is very helpful for me. I am studying engineering Would you suggest some tips to enhance my coding and communication skills.
Thanks for sharing
For outer loop can we take range(0,len(nums)-1,1)?
It won't work that way
I will be waiting to our online class
You can even do nums[j],nums[j+1] = nums[j+1],nums[j] to swap!
Plz make a video on pygame. Plz sir🙏🙏🙏
Flask or django?
pygrame, I guess
It was amazing
Awsome
it was a n wonderful explanation by you sir,
I would appreciate you to create more on python like insertion sort etc.
Really good
Super
I am searching for all the sorting algorithms in java telusko. But it's getting python... Can u provide the Bubble sort link in java. Now I became an addict of telusko java learning. It's really Amazing
check in another YT playlist
you could also have a variable = 0 at the start of every iteration and inciment it by 1 when you swap, therefore if at the end of an iteration the variable is 0 means the list is sorted and you don't have to try to sort the rest of the already sorted list
Yeh.. did it before watching ur code -
def sort(nums):
for e in range(len(nums)-1):
for i in range(len(nums)-1):
if nums[i]>nums[i+1]:
temp=nums[i]
nums[i]=nums[i+1]
nums[i+1]=temp
else:
pass
nums =[5,3,8,6,7,2]
sort(nums)
print(nums)