• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Python » Bubble sort in Python
Next →
← Prev

Bubble sort in Python

By Dinesh Thakur

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 pythonBubble 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

Swapping

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)

Output:

Python Program for Bubble Sort

You’ll also like:

  1. What is Bubble Sort
  2. What is bubble sort in C with example?
  3. Array C++ Bubble sort
  4. What is bubble sort in Java with example?
  5. C Program sorting of an int array using Bubble Sort
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Python

Python Tutorials

  • Python - Home
  • Python - Features
  • Python - Installation
  • Python - Hello World
  • Python - Operators Types
  • Python - Data Types
  • Python - Variable Type
  • Python - Switch Case
  • Python - Line Structure
  • Python - String Variables
  • Python - Condition Statement
  • Python - if else Statement
  • Python - for-loop
  • Python - while loop
  • Python - Command Line
  • Python - Regular Expression

Python Collections Data Types

  • Python - List
  • Python - Sets
  • Python - Tuples
  • Python - Dictionary

Python Functions

  • Python - Functions
  • Python - String Functions
  • Python - Lambda Function
  • Python - map() Function

Python Object Oriented

  • Python - Oops Concepts
  • Python - File Handling
  • Python - Exception Handling
  • Python - Multithreading
  • Python - File I/O

Python Data Structure

  • Python - Linked List
  • Python - Bubble Sort
  • Python - Selection Sort
  • Python - Linear Search
  • Python - Binary Search

Python Programs

  • Python - Armstrong Number
  • Python - Leap Year Program
  • Python - Fibonacci Series
  • Python - Factorial Program

Other Links

  • Python - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2023. All Rights Reserved.