---
title: "Bubble Sort"
description: "Python programming for sorting numbers using bubble sort algorithm"
url: "https://www.geekboots.com/python/bubble-sort"
markdown_url: "https://www.geekboots.com/python/bubble-sort/md"
category: "Python Programming"
author: "Geekboots"
published: "2016-06-06"
updated: "2026-11-29T12:11:42.320Z"
tags: ["Python", "python programming", "bubble sort", "sort number", "sorting algorithm"]
---

# Bubble Sort (Python Programming)

> Python programming for sorting numbers using bubble sort algorithm

## Source Code

### bubble-sort.py

```python
#!/usr/bin/evn python

# Function for bubble sort
def bubbleSort(data):
    # Loop the process within range of number list
    for i in range(len(data)-1,0,-1):
        for j in range(i):
            # Compare with next location value
            if data[j]>data[j+1]:
                #Swap numbers as per order
                temp = data[j]
                data[j] = data[j+1]
                data[j+1] = temp

numList = [5,8,1,6,3,7,2,4,9]
print('Before sort:')
print(numList)
# Calling 'bubbleSort' function by passing number array
bubbleSort(numList)
print('After sort:')
print(numList)


# ***** Output *****
# Before sort:
# [5, 8, 1, 6, 3, 7, 2, 4, 9]
# After sort:
# [1, 2, 3, 4, 5, 6, 7, 8, 9]
```

## Related Code Examples

- [Heap Sort](https://www.geekboots.com/python/heap-sort) ([Markdown](https://www.geekboots.com/python/heap-sort/md))
- [Merge Sort](https://www.geekboots.com/python/merge-sort) ([Markdown](https://www.geekboots.com/python/merge-sort/md))
- [Quick Sort](https://www.geekboots.com/python/quick-sort) ([Markdown](https://www.geekboots.com/python/quick-sort/md))
- [Selection Sort](https://www.geekboots.com/python/selection-sort) ([Markdown](https://www.geekboots.com/python/selection-sort/md))
- [Insertion Sort](https://www.geekboots.com/python/insertion-sort) ([Markdown](https://www.geekboots.com/python/insertion-sort/md))
- [Delete file & directory](https://www.geekboots.com/python/delete-file-directory) ([Markdown](https://www.geekboots.com/python/delete-file-directory/md))
- [File Operation](https://www.geekboots.com/python/file-operation) ([Markdown](https://www.geekboots.com/python/file-operation/md))
- [Directory](https://www.geekboots.com/python/directory) ([Markdown](https://www.geekboots.com/python/directory/md))

---
Source: [Geekboots](https://www.geekboots.com/python/bubble-sort)
