Python: BubbleSort sorting algorithm

preview_player
Показать описание
Tutorial on how to implement the BubbleSort sorting algorithm in Python 3 with animated demo and code implementation example.

PYTHON SORTING ALGORITHMS

#Python
Рекомендации по теме
Комментарии
Автор

This is the most authentic, easy, concise bubble sort video/algo I've ever seen. Straight forward chute.

TheBhartiyaTrainee
Автор

Intro music sounds like something straight out of Counter Strike.

great video btw!!

WakeUpSid
Автор

Thank you sooo muchhhh!!!! Have my comp exam tomorrow!!! Really saved my life!!!!

fangirlforlife
Автор

Short and straight forward explanation. Thank you

MMm-uxwg
Автор

As a beginner programmer, I really appreciate your explanations in your videos. They're concise, and that's a good thing! tyvm!

stevieray
Автор

the starting music gives the feeling of a movie :-D

sukantkumar
Автор

Thanks for this video! I decided to implement the bubble sort function using while statements.

def bubbleSort ( alist ) :
i, j = 0, 0
while ( i < (len(alist) - 1) ) :
while (j < (len(alist) - 1 - i) ) :
if (alist[j] > alist[j+1]) :
alist[j], alist[j+1] = alist[j+1], alist[j] #- swap values
j = j + 1
i, j = (i+1), 0 # increment i, reset j to zero!
return alist

jalw
Автор

Thank you so much. Very simple and easy explanation.

marawanalhazel
Автор

One enhancement would be breaking out of first loop when we know that we already have sorted array. If we go through one iteration with 0 swaps done, that means we already have sorted array and don't need to iterate through rest.

sandeepsagi
Автор

I have a question, if the list is incrementing negatively, how do we get the iterator to start from the left side of the list.

moost
Автор

pls i need to know what "myList[j], myList[j+1] = myList[j+1], myList[j]" specifically the ", " mean

jawadt
Автор

I have a question. In range in python range(0, 4) means a number from 0 to 3 why do we need to minus 1

hujake
Автор

Hey, but what is the "i" purpose here ? instead of for j in range(0, len(my_list) - 1 - i ) you could say just for j in range(i - 1) and then when you compare you could say if my_list[j] > my_list[i]: my_list[ j ], my_list[ i ] = my_list[ i ], my_list[ j ]. These gives me more hints about the function itself and have some logic, like j is the previous number and i is the next number to be compared to j.
Does it make sense ?

florinc
Автор

How about we iterate i from (0, len(list-1)) and j from (i+1, len(list)) and compare list[i] with list[j] ?
Won't it work the same way?

Edit: Tried it, it works. For anyone checking up this is the code:
s=[1, 8, 5, 4, 6, 3, 2]

def bsrt(s):
for i in range(0, len(s)-1):
for j in range(i+1, len(s)):
if s[j]<s[i]:
s[i], s[j]=s[j], s[i]
pass
return s


print(bsrt(s))

ayans
Автор

I am not taking any university courses for this so this might be an easy question, but I don't quite get how the for loops work to set values for the comparisons to work.

nahumcollicott
Автор

my teacher had us watch this video and wants us to make a list and have it read from last to first or in descending order, how would i go about that?

ryannocent
Автор

The solution shown in this video won't sort this array a = [4, 3, 5, 10, 1, 7]. Because the function has a return.

rohaanshahid
Автор

how would you do this if you did not know that the last number was correct

liondoeseverything
Автор

You should post the code in the description :-) ... I copied it down still. Just for future reference...

ManOnTheMoon
Автор

Everywhere it is "myList" but the actual array is "theList".

GollumADS