How to implement Bubble Sort in Python - NareshIT
Naresh i Technologies
Only Institute to offer the 'Most Comprehensive eLearning Platform to suit the self-learning needs of all CMS and LMS
Introduction:
1.?????As we know that Python is a more powerful language that offers great tools for data crunching and preparation, as well as for complex scientific data analysis and modeling.
2.?????Python has multiple implementations including Jython,?scripted in Java language for Java Virtual Machine.
3.?????Most of the Python modules work on community development models and are open-source and free.
4.?????When we are going to use the python for programming approach, then there are several things we need to know.
5.?????One of the important techniques is Sorting. In Sorting, we are going to arrange the data in an increasing or decreasing order according to some linear relationship among the elements.
Here I am going to discuss the working approach of Bubble sort. I am also going to discuss the step-wise discussion below.
What is Bubble sort??
1.?????The Bubble sort is the most common technique which is being used for arranging the elements in either increasing or decreasing order. It is also known as the sinking sort.
2.?????In this technique the list elements to be getting sorted, are used to compare each pair of adjacent items.
3.?????During the comparison depending on the condition specified they are allowed to swap if they are not in the correct order.
4.?????The above steps mentioned are repeated until no more swaps are needed. which is used to yield the final sorted list.
Steps for performing a Bubble Sort:
During the process of bubble sorting, the following steps are used to arrange the elements. These steps are mentioned below.
1. At the beginning of the process, we need to pick the first and second elements in the list compare and allow them to get swapped depending upon the condition specified if they are not in the correct order.
Bubble Sort Algorithm:
As we have already discussed above that the Bubble Sort?is a sorting algorithm technique that is basically used to sort the list items in either ascending or descending order. If we need to arrange the elements of the list in ascending order then it can be done by comparing two adjacent values.
During the process If we are going for ascending order then if the first value is higher than the second value, then swapping will take place and the first value takes the second value position. Otherwise swapping will not take place if the first element is lesser than the second value.
The basic algorithm used for such cases is discussed below.
Step 1)?At first we need to get the total number of elements. Get the total number of items in the given list.
Step 2)?We need to determine the number of outer passes (n - 1) to be done. If the number of elements in the list is n then it is the n-1.
Step 3)?Here we need to perform the inner passes (n - 1) times for outer pass 1. During this process, the first element is get compared with the second element and If the second value is less than the first value, then swap the positions.
Step 4)?Repeat step 3 until we reach the outer pass (n - 1).
To understand the mechanism in a better manner let us consider the following example below.
Example:
Let us consider the list as
A=[21,6,9,33,3]
Step-1: The values 21 and 6 are compared to check which one is greater than the other.
[21,6,9,33,3]
Since 21 is greater than 6, so 21 takes the position occupied by 6 while 6 takes the position that was occupied by 21. So now the new list will be
[6, 21,9,33,3]
Step 2: The values 21 and 9 are compared.
[6, 21,9,33,3]
Here since 21 is greater than 9, so we swap the positions of 21 and 9. So the new list will be
[6,9, 21,33,3]
Step-3: The values 21 and 33 are compared to find the greater one.
?[6,9, 21,33,3]
领英推荐
Here since The value 33 is greater than 21, so no swapping takes place.
Step-4: The values 33 and 3 are compared to find the greater one.
[6,9, 21,33,3]
Here The value 33 is greater than 3, so we swap their positions. So the new list is
[6,9, 21,3,33]
The sorted list at the end of the first iteration is like the one above, But during the second iteration the new list is as follows
[6,9, 3,21,33]
After the third iteration, the new list is as follows
[6,3,9,21,33]
After the fourth iteration, the new list is as follows
[3,6,9,21,33]
The above is the final sorted list.
Python Program to implement Bubble Sort:
def bubble sort( theSeq ):
???n = len( theSeq )
???for i in range( n - 1 ) :
???????flag = 0
for j in range(n - 1) :
???????????if theSeq[j] >theSeq[j + 1] :
tmp = theSeq[j]
theSeq[j] = theSeq[j + 1]
theSeq[j + 1] = tmp
???????????????flag = 1
???????if flag == 0:
break
???return theSeq
el = [21,6,9,33,3]
result = bubbleSort(el)
print (result)
?In the above code, we compare the adjacent numbers and swap them if they are not in the correct order.
Scope @ NareshIT:
1.?????At Naresh IT you will get a good Experienced faculty who will guide you, mentor you and nurture you to achieve your dream goal.
2.?????Here you will get good hand on practice in terms of the practical industry-oriented environment which will definitely help you a lot to shape your future.
3.?????During the designing process of the application, we will let you know about the other aspect of the application too.
4.?????Our Expert trainer will let you know about every ins and out’s of the problem scenario.
Achieving your dream goal is our motto. Our excellent team is working restlessly for our students to click their target. So, believe in us and our advice, and we assured you about your sure success.?
?You can contact us anytime for your Python Training online as well as call us directly, or you can give us a missed call. And one of our customer care representatives will contact you asap.
Follow us for More Updates:?https://bit.ly/NITLinkedIN??