def bubble_sort(a):
for i in range(0, len(a)):
for j in range(0, len(a) - 1):
if(a[i] < a[j]):
a[i], a[j] = a[j], a[i] #can someone explain this part of the code for me
#i tried doing (im more of a c++ programmer):
temp = 0
temp = a[i]
a[i] = a[j]
a[j] = temp
#but that didn't work well
fishsticks1907
0
Newbie Poster
Recommended Answers
Jump to PostI am also not pyhton guy but I think at line 5 the values of a[i] and a[j] are being swap if a[i] is less then a[j] and process goes on until array is sorted.
Its was really weird experience for me to look at python code for first time. …
Jump to PostYou have the range endings reversed and you want to start at +1 on the inner loop.
for x in range(0, len(a)-1): for j in range(x+1, len(a)):
Also you swap every time there is a difference instead of once per loop.
import random …
Jump to Post@wooeee: you are doing selection sort not bubble sort. On the other hand bubble sort is so inefficient, that you should use insertion or selection sort.
______For bubble sort it is customary to set flag for if swaps were made and stop sort when no swaps were done in …
All 11 Replies
Sahil89
0
Newbie Poster
woooee
814
Nearly a Posting Maven
TrustyTony
888
pyMod
Team Colleague
Featured Poster
woooee
814
Nearly a Posting Maven
TrustyTony
888
pyMod
Team Colleague
Featured Poster
Lardmeister
461
Posting Virtuoso
Lardmeister
461
Posting Virtuoso
TrustyTony
888
pyMod
Team Colleague
Featured Poster
Lardmeister
461
Posting Virtuoso
Lardmeister
461
Posting Virtuoso
TrustyTony
888
pyMod
Team Colleague
Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.