 Hi everyone. In this tutorial, we're going to talk about the bubble sort algorithm in Python language. First, we're going to talk about the bubble sort algorithm in generally, and then we're going to implement bubble sort in Python language. Bubble sort works in a way that it compares each pair of adjacent items and swap them if they are in the wrong order. For example, suppose that we have a list with five elements 5, 4, 1, 3 and 2, and in this example, we want to sort this list in ascending numerical order to get as the solution list, which is going to contain numbers 1, 2, 3, 4 and 5. Bubble sort works by comparing adjacent elements and swapping them if they are in the wrong order. Let's see how bubble sort algorithm works. First, we're going to compare numbers 5 and 4. In this case, number 5 is greater than number 4, so we're going to swap them. Then we're going to compare number 5 with number 1. Again, here we have a situation that number 5 is greater than number 1, so we're going to swap them. Then we're going to compare numbers 5 and 3. They are in the wrong order. We're going to swap them. And finally, we need to compare numbers 5 and 2. And again, we're going to swap them because number 5 is greater than number 2. And that's the finish of round 1. After this first round or first iteration, we have one number that is sorted. In this case, it's number 5, so we do not need to work with it anymore. Now let's start again in the next iteration with comparing these two numbers. And again, number 4 is greater than number 1, so we need to swap them. Again, we're going to swap here numbers 3 and 4 and numbers 2 and 4. I made this example for this video to be a little bit difficult so that we can see more easily how bubble sort actually works. And again, it's the end of the second iteration in which we've sorted another element. And now we're going to check rest elements and compare them in order to sort them. Now, in this case, we're going to compare numbers 1 and 3. And they are not in the wrong order because number 3 is greater than number 1, so we do not need to swap them in this case. But what about numbers 3 and 2? They are in the wrong order, so we're going to swap them. And again, the third number is sorted. And finally, in the fourth iteration or in the fourth round, we need to compare these two numbers. Number 2 is greater than number 1, so we do not need to swap them. And that's it. Our list has been swapped in ascending numerical order. List with which we're working in this example has five elements. And the bubble sort algorithm works that in the case when we're having five elements, first we need to have four comparisons to compare first and second element, second and third and so on. And then we're going to have one element which is sorted. And then we need to have three comparisons. And in the next iteration, we have two elements that are sorted and we need to have two comparisons. And finally, we need to have just one comparison and then all elements are going to be sorted. Our list has five elements. I'm gonna start to type the bubble sort code and I'm going to use this same list in this example. When we're implementing the bubble sort algorithm, we need to have two loops, inner loop and outer loop. The outer loop is going to have four iterations because we're going to change four times the number of comparisons. As you remember, at first we had four comparisons, then three, two and finally one outer loop tells us how many times we want to change the number of comparisons of list elements. I'm gonna continue implementing bubble sort code. I'm gonna write, for example, function, let's call it bubble sort, which is going to have an argument in this argument. We're going to put a list. We need to have two four loops. The outer loop is going to tell us how many times we want to change the number of comparisons of list. So I'm going to use a variable, for example, number of comparisons. And if we're having five elements, we need to have four different comparisons. I'm gonna get the number of items in our list by using the function lan. The length of the list is going to have five elements. And we need to subtract number one, because we're going to have four times changing the number of comparisons of the list. Then again, the inner loop is going to have again five minus one or four iterations. In the first iteration, we're gonna have four comparisons. In the second, we're going to have three in the third two. And in the last, we're going to have just one comparison. Let's back to our code. So I can use this outer list to get at the same time the number of comparisons in our inner list. So I'm going to loop this list in opposite direction. Okay, now let's write our inner for loop for item in range number of comparisons. Then we're just going to check if two adjacent elements are in the wrong order. And if they are, we're just going to swap them. I'm gonna ask if adjacent elements are in the wrong order. If they are, we're just going to swap them. This is the code for swapping two elements of the list. And that's all code. Now I'm going to check if our algorithm is working by trying to sort our list one. And I'm gonna print the result our algorithm is working. Also, I do not recommend you to use this bubble sort in practice, because it's very slow. You can use it just in few cases. One of them is that if you know that almost all elements in your list are sorted, and just few elements of this list are not in the right order. In the right order. And if they are not in the right order, that they are very close one to another. That's all in this video. Don't forget to subscribe.