R Programming
R ANOVA Tutorial: One way & Two way (with Examples)
What is ANOVA? Analysis of Variance (ANOVA) is a statistical technique, commonly used to studying...
Bubble Sort is a sorting algorithm used to sort list items in ascending order by comparing two adjacent values. If the first value is higher than second value, the first value takes the second value position, while second value takes the first value position. If the first value is lower than the second value, then no swapping is done.
This process is repeated until all the values in a list have been compared and swapped if necessary. Each iteration is usually called a pass. The number of passes in a bubble sort is equal to the number of elements in a list minus one.
In this Bubble Sorting in Python tutorial you will learn:
We will breakdown the implementation into three (3) steps, namely the problem, the solution, and the algorithm that we can use to write code for any language.
A list of items is given in random order, and we would like to arrange the items in an orderly manner
Consider the following list:
[21,6,9,33,3]
Iterate through the list comparing two adjacent elements and swapping them if the first value is higher than the second value.
The result should be as follows:
[3,6,9,21,33]
The bubble sort algorithm works as follows
Step 1) Get the total number of elements. Get the total number of items in the given list
Step 2) Determine the number of outer passes (n - 1) to be done. Its length is list minus one
Step 3) Perform inner passes (n - 1) times for outer pass 1. Get the first element value and compare it with the second value. If the second value is less than the first value, then swap the positions
Step 4) Repeat step 3 passes until you reach the outer pass (n - 1). Get the next element in the list then repeat the process that was performed in step 3 until all the values have been placed in their correct ascending order.
Step 5) Return the result when all passes have been done. Return the results of the sorted list
Step 6) Optimize Algorithm
Avoid unnecessary inner passes if the list or adjacent values are already sorted. For example, if the provided list already contains elements that have been sorted in ascending order, then we can break the loop early.
By default, the algorithm for bubble sort in Python compares all items in the list regardless of whether the list is already sorted or not. If the given list is already sorted, comparing all values is a waste of time and resources.
Optimizing the bubble sort helps us to avoid unnecessary iterations and save time and resources.
For example, if the first and second items are already sorted, then there is no need to iterate through the rest of the values. The iteration is terminated, and the next one is initiated until the process is completed as shown in the below Bubble Sort example.
Optimization is done using the following steps
Step 1) Create a flag variable that monitors if any swapping has occurred in the inner loop
Step 2) If the values have swapped positions, continue to the next iteration
Step 3) If the benefits have not swapped positions, terminate the inner loop, and continue with the outer loop.
An optimized bubble sort is more efficient as it only executes the necessary steps and skips those that are not required.
Given a list of five elements, the following images illustrate how the bubble sort iterates through the values when sorting them
The following image shows the unsorted list
First Iteration
Step 1)
The values 21 and 6 are compared to check which one is greater than the other.
21 is greater than 6, so 21 takes the position occupied by 6 while 6 takes the position that was occupied by 21
Our modified list now looks like the one above.
Step 2)
The values 21 and 9 are compared.
21 is greater than 9, so we swap the positions of 21 and 9
The new list is now as above
Step 3)
The values 21 and 33 are compared to find the greater one.
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.
The value 33 is greater than 3, so we swap their positions.
The sorted list at the end of the first iteration is like the one above
Second Iteration
The new list after the second iteration is as follows
Third Iteration
The new list after the third iteration is as follows
Fourth Iteration
The new list after the fourth iteration is as follows
The following code shows how to implement the Bubble Sort algorithm in Python.
def bubbleSort( 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)
Executing the above bubble sort program in Python produces the following results
[6, 9, 21, 3, 33]
The explanation for the Python Bubble Sort program code is as follows
HERE,
The following are some of the advantages of the bubble sort algorithm
The following are some of the disadvantages of the bubble sort algorithm
There are three types of Complexity are:
The sort complexity is used to express the amount of execution times and space that it takes to sort the list. The bubble sort makes (n – 1) iterations to sort the list where n is the total number of elements in the list.
The time complexity of the bubble sort is O(n2)
The time complexities can be categorized as:
The space complexity measures the amount of extra space that is needed for sorting the list. The bubble sort only requires one (1) extra space for the temporal variable used for swapping values. Therefore, it has a space complexity of O (1).
What is ANOVA? Analysis of Variance (ANOVA) is a statistical technique, commonly used to studying...
CRM (Customer Relationship Management) stores customer contact information like names, addresses,...
GitHub is a code hosting tool that is widely used for version control. The tool allows developers...
In this tutorial, you will learn to use Hadoop with MapReduce Examples. The input data used is...
In this tutorial, we will learn- What is a Pipe in Linux? 'pg' and 'more' commands The 'grep'...
In this tutorial, you will learn What is Cluster analysis? K-means algorithm Optimal k What is...