0% found this document useful (0 votes)
125 views

Bubble Sort - Algrithm and Data Structure

Bubble sort is a sorting algorithm that compares adjacent elements and swaps them if they are in the wrong order. It iterates through the list, swapping adjacent elements into place until no swaps are needed, meaning the list is sorted. The algorithm makes multiple passes through the list, pushing larger elements toward the end on each pass until everything is in order. An optimized version checks if any swaps occurred during an iteration, and stops early if not, since the list must already be sorted. Bubble sort has a worst-case time complexity of O(n^2) but has a simple implementation making it suitable for short lists or as an introduction to sorting algorithms.

Uploaded by

Orang Biasa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
125 views

Bubble Sort - Algrithm and Data Structure

Bubble sort is a sorting algorithm that compares adjacent elements and swaps them if they are in the wrong order. It iterates through the list, swapping adjacent elements into place until no swaps are needed, meaning the list is sorted. The algorithm makes multiple passes through the list, pushing larger elements toward the end on each pass until everything is in order. An optimized version checks if any swaps occurred during an iteration, and stops early if not, since the list must already be sorted. Bubble sort has a worst-case time complexity of O(n^2) but has a simple implementation making it suitable for short lists or as an introduction to sorting algorithms.

Uploaded by

Orang Biasa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Bubble Sort

Data Structure and Algorithm


What is Bubble Sort
Bubble sort is a sorting algorithm that compares two adjacent elements
and swaps them until they are in the intended order.
Just like the movement of air bubbles in the water that rise up to the
surface, each element of the array move to the end in each iteration.
Therefore, it is called a bubble sort.
Working of Bubble Sort
1. First Iteration (Compare and Swap)

2. Remaining Iteration (Continue from first iteration)


1. First Iteration (Compare and Swap)
Suppose we are trying to sort the elements in ascending order.

1. Starting from the first index, compare the first and the second elements.
2. If the first element is greater than the second element, they are swapped.
3. Now, compare the second and the third elements. Swap them if they are not in
order.
4. The above process goes on until the last element
Compare the Adjacent Elements
2. Remaining Iteration
The same process goes on
for the remaining iterations.

After each iteration, the


largest element among the
unsorted elements is
placed at the end.
2. Remaining Iteration
In each iteration, the
comparison takes place up
to the last unsorted
element.
2. Remaining Iteration
The array is sorted when all
the unsorted elements are
placed at their correct
positions.
Bubble Sort Algorithm
Bubble Sort in
Python
Optimized Bubble Sort Algorithm
In the above algorithm, all the comparisons are made even if the array is already
sorted.

This increases the execution time.

To solve this, we can introduce an extra variable swapped. The value of swapped is
set true if there occurs swapping of elements. Otherwise, it is set false.

After an iteration, if there is no swapping, the value of swapped will be false. This
means elements are already sorted and there is no need to perform further
iterations.
Optimized Bubble Sort Algorithm
Optimized Bubble
Sort in Python
Bubble Sort Complexity
Bubble Sort Applications
● complexity does not matter
● short and simple code is preferred
● Before perform other algorithms
References
● Python.org
● The Python Book
● w3school.com
● programiz.com
Thank You

You might also like