In this tutorial, we will try to sort a list using a sorting technique, which is
bubble sort, sometimes referred to as
sinking sort, there are different sorting techniques available, but bubble sort works well and is the easiest one.
The figure below illustrates the working of bubble sort algorithm:
Bubble sort in Python
Bubble sort compares and swaps each pair of adjacent items if they are in the wrong order. The list is passed through until no swaps are needed, meaning that the list is sorted. The algorithm is a comparison sort, and it’s named for the
"bubble" of smaller elements at the top of the list. Although the algorithm is easy, even when compared to insertion, it is too slow and impractical for most problems.
I’ve enabled so what we’ll do is we take some values here to understand how bubble sort works in theory. so let’s take some values
[5 3 8 6 7 2]
Now we’ll sort these values, and there are a lot of sorting techniques available right almost in every sorting technique, we have one thing common, which is swapping.
So basically you swap elements, so it and that’s how you will make sure that your all the values are sorted. So swapping is one of the important concepts, so make sure that you know the concept of swapping.
so what we normally do is we take a third variable here so example
a → b a → t b → a t → b
If you have a and b and if you want to swap them. We will take a third variable. Let’s say t, and you will keep the value of a and t, then you will copy the value from b to a, and then you will copy the value from t to b that’s how you sort two values, and we’ll be doing that in this tutorial.
We’ll compare the first two values. In this case, it is five and three. Right now you have to make sure that the smallest one comes first,
If 5 > 3 Swapped
if the first value is greater than the second value swap, in this case, it is greater, so five is greater than three. so what you will do, you will swap it so now after swapping the values are
[3 5 8 6 7 2]
Now you will go forward if you have to make sure that if the first value is greater than the second value. You then swap otherwise skip. In this case, it is smaller.
We don’t have to swap now. What you will do is, you will iterate at this point.
[3 5 8 6 7 2]
Next you will compare eight and six. In this case, eight is greater than six. You have to swap as usual, after swapping you will get six and eight, so the final value you will get
[3 5 6 8 7 2]
you have to do the same thing till the end so let’s pick it quick so now you will compare eight and seven right again we know eight is greater than seven we will swap the new value we’ll get
[3 5 6 7 8 2]
the last comparison we have eight and two and of course we know we have to swap so at the end you will get
[3 5 6 7 2 8]
Now, after this iteration, which is eight at the end, you’ve got the biggest value at the end, but if anybody’s is not sorted, we have to reiterate the same thing, so it’s all about comparing and swapping.
Now you will start again, and you will compare to first two values with three and five three smaller don’t have to swap then will compare five and six no need to swap then you will compare 6 and 7, or then you will compare 7 & 2 we have to swap here then it becomes 2 & 7 right, so the final values are
[3 5 6 2 7 8]
There is one thing which is important for the second iteration we are not checking for 8 because it is done, so the maximum value you have is at the end and after psychiatrists in the second last value is the second big element if you can compare the list now 7 and 8 those are the maximum values, so after second iteration you got two big values in ascending order that means after all the iterations, so the number of elements. We have 6 sorts of the fifth iteration.
[2 3 5 6 7 8]
You will get all the values sorted if you repeat the same step. That’s what bubble sort is, and that’s why we acquire two loops one for the iteration, which will make sure that you will get the biggest element at the end, and the second loop, which is the outer loop, will before do the same thing repeatedly and that’s what you have to do in this code.
Python Program for Bubble Sort
def sort(nums): for i in range(len(nums)-1,0,-1): for j in range(i): if nums[j]>nums[j+1]: temp = nums[j] nums[j] = nums[j+1] nums[j+1] = temp nums = [5,3,8,6,7,2] sort(nums) print(nums)