 Hi everyone, in this tutorial we're going to learn how to write bubble sort algorithm in Python. But before we do that I'm gonna show you how this bubble sort algorithm works. Here we have a list of few elements. Bubble sort is going to compare consecutive elements and if they are out of place this algorithm is gonna swap them. For example in this first iteration we're gonna compare numbers two and five. Number five is bigger than number two so we do not want to swap these two elements. Then we're gonna compare numbers five and three since number five is bigger than number three we're gonna swap these two elements. So in first iteration numbers three and five are swapped and this process is gonna be continued until at some point we swapped at first numbers four and eight and we're gonna swap nine and one. So as you can see in bubble sort the highest number will bubble its way to the right. In the next iterations we're gonna do the same thing we're gonna compare two consecutive elements and swap them if they are not in the right order. Now let's write a code for bubble sort. I'm gonna create a function called bubble sort. We're gonna take a list as an argument then we want to find the length of this list and then we want to have two iterations. In the first iteration we're gonna loop through all elements. So I'm gonna type four i in range from zero to n minus one. In the second loop I want to compare consecutive elements so this second loop is gonna be loop four comparing consecutive elements. I'm gonna type four j in range from zero to n minus one minus i. Minus i is here because we do not want to compare elements that are already sorted. Now let's compare consecutive elements. If the left element of this list is bigger than the right element of this same list we're gonna swap them. So here is swapping element at j position now it's gonna be at position j plus one and vice versa and now let's bubble sort our list and the last thing that I want to do is that I want to print this bubble sorted list. Let's run this code as you can see this our list is bubble sorted. That's all in this tutorial. Don't forget to check other videos on our tutorial. See you in the next tutorial. Bye-bye.