0% found this document useful (0 votes)
4 views9 pages

Bubble Sort With Python

The document explains the Bubble Sort algorithm, which sorts an array by repeatedly comparing adjacent values and swapping them if they are in the wrong order. It includes a manual walkthrough of the sorting process and provides a Python implementation of the algorithm, highlighting an improvement to stop the algorithm early if no swaps occur. The time complexity of Bubble Sort is O(n^2), making it less efficient for large datasets compared to other sorting algorithms like Quicksort.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

Bubble Sort With Python

The document explains the Bubble Sort algorithm, which sorts an array by repeatedly comparing adjacent values and swapping them if they are in the wrong order. It includes a manual walkthrough of the sorting process and provides a Python implementation of the algorithm, highlighting an improvement to stop the algorithm early if no swaps occur. The time complexity of Bubble Sort is O(n^2), making it less efficient for large datasets compared to other sorting algorithms like Quicksort.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1/3/26, 3:55 PM Bubble Sort with Python

 Tutorials  References  Exercises  Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

Bubble Sort with Python


❮ Previous Next ❯

Bubble Sort
Bubble Sort is an algorithm that sorts an array from the lowest value to the highest value.

Sort

Run the simulation to see how it looks like when the Bubble Sort algorithm sorts an array of
values. Each value in the array is represented by a column.

The word 'Bubble' comes from how this algorithm works, it makes the highest values 'bubble
up'.

[Link] 1/9
1/3/26, 3:55 PM Bubble Sort with Python

 Tutorials  References  Exercises  Sign In

How it works:
HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
1. Go through the array, one value at a time.
2. For each value, compare the value with the next value.
3. If the value is higher than the next one, swap the values so that the highest value
comes last.
4. Go through the array as many times as there are values in the array.

Manual Run Through


Before we implement the Bubble Sort algorithm in a programming language, let's manually
run through a short array only one time, just to get the idea.

Step 1: We start with an unsorted array.

[7, 12, 9, 11, 3]

Step 2: We look at the two first values. Does the lowest value come first? Yes, so we don't
need to swap them.

[7, 12, 9, 11, 3]

Step 3: Take one step forward and look at values 12 and 9. Does the lowest value come first?
No.

[7, 12, 9, 11, 3]

Step 4: So we need to swap them so that 9 comes first.

[7, 9, 12, 11, 3]

Step 5: Taking one step forward, looking at 12 and 11.

[Link] 2/9
1/3/26, 3:55 PM Bubble Sort with Python

[7, 9,Tutorials
12, 11, 3]
 References  Exercises  Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
Step 6: We must swap so that 11 comes before 12.

[7, 9, 11, 12, 3]

Step 7: Looking at 12 and 3, do we need to swap them? Yes.

[7, 9, 11, 12, 3]

Step 8: Swapping 12 and 3 so that 3 comes first.

[7, 9, 11, 3, 12]

Repeat until no more swaps are needed and you will get a sorted array:

Bubble Sort

[ 7, 12, 9, 11, 3 ]

Implement Bubble Sort in Python


To implement the Bubble Sort algorithm in Python, we need:

1. An array with values to sort.


2. An inner loop that goes through the array and swaps values if the first value is higher
than the next value. This loop must loop through one less value each time it runs.
3. An outer loop that controls how many times the inner loop must run. For an array with
n values, this outer loop must run n-1 times.

The resulting code looks like this:

Example Get your own Python Server

[Link] 3/9
1/3/26, 3:55 PM Bubble Sort with Python

Create a Bubble Sort algorithm in Python:


 Tutorials  References  Exercises  Sign In
mylist = [64, 34, 25, 12, 22, 11, 90, 5]
HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

n = len(mylist)
for i in range(n-1):
for j in range(n-i-1):
if mylist[j] > mylist[j+1]:
mylist[j], mylist[j+1] = mylist[j+1], mylist[j]

print(mylist)

Run Example »

Bubble Sort Improvement


The Bubble Sort algorithm can be improved a little bit more.

Imagine that the array is almost sorted already, with the lowest numbers at the start, like this
for example:

mylist = [7, 3, 9, 12, 11]

In this case, the array will be sorted after the first run, but the Bubble Sort algorithm will
continue to run, without swapping elements, and that is not necessary.

If the algorithm goes through the array one time without swapping any values, the array
must be finished sorted, and we can stop the algorithm, like this:

Example
Improved Bubble Sort algorithm:

mylist = [7, 3, 9, 12, 11]

n = len(mylist)
for i in range(n-1):
swapped = False
[Link] 4/9
1/3/26, 3:55 PM Bubble Sort with Python

for j in range(n-i-1):
 ifTutorials 
mylist[j] >References 
mylist[j+1]: Exercises  Sign In
mylist[j], mylist[j+1] = mylist[j+1], mylist[j]
HTML
 CSS JAVASCRIPT
swapped = True SQL PYTHON JAVA PHP HOW TO [Link] C
if not swapped:
break

print(mylist)

Run Example »

Bubble Sort Time Complexity


The Bubble Sort algorithm loops through every value in the array, comparing it to the value
next to it. So for an array of n values, there must be n such comparisons in one loop.

And after one loop, the array is looped through again and again n times.

This means there are n ⋅ n comparisons done in total, so the time complexity for Bubble Sort
is: O(n 2
)

The graph describing the Bubble Sort time complexity looks like this:

As you can see, the run time increases really fast when the size of the array is increased.

Luckily there are sorting algorithms that are faster than this, like Quicksort, that we will look
at later.

[Link] 5/9
1/3/26, 3:55 PM Bubble Sort with Python

❮ Previous
Tutorials  References Sign
 in to track progress
Exercises Next ❯ In
Sign

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

COLOR PICKER

 

REMOVE ADS

[Link] 6/9
1/3/26, 3:55 PM Bubble Sort with Python

Python - Get Started - [Link]


 Tutorials  References  Exercises  Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

Click on ► to watch the video

[Link] 7/9
1/3/26, 3:55 PM Bubble Sort with Python

 Tutorials  References  Exercises  Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

-->
 PLUS SPACES

GET CERTIFIED FOR TEACHERS

FOR BUSINESS CONTACT US

Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
[Link] Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial

Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
[Link] Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
AngularJS Reference
jQuery Reference

Top Examples Get Certified


HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
SQL Examples SQL Certificate
Python Examples Python Certificate
[Link] Examples PHP Certificate
Bootstrap Examples jQuery Certificate
[Link] 8/9
1/3/26, 3:55 PM Bubble Sort with Python
PHP Examples Java Certificate

 Tutorials  Java Examples


References 
XML Examples
Exercises  C++ Certificate
C# Certificate
Sign In
jQuery Examples XML Certificate
HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

    

FORUM ABOUT ACADEMY


W3Schools is optimized for learning and training. Examples might be simplified to improve reading and
learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full
correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies
and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by [Link].

[Link] 9/9

You might also like