# Why this is a bad bubble sort algorithm?

## Question:

I started studying *Data Structures and algorithms,* and tried to implement Bubble sort:

```
def BubbleSort(list):
for a in range(len(list)):
for b in range(len(list)):# I could start this loop from 1
if list[a]< list[b]: # to avoid comparing the first element twice
temp=list[a]
list[a]=list[b]
list[b]=temp
return list
```

I browsed the net and books – but found no Python implementation of bubble sort.

What’s wrong with the above?

## Answers:

Several things:

- the algorihm will not always sort correctly;
- syntactically it seems to sort the opposite way;
- it takes twice the amount of time necessary to perform bubble sort;
- it is
*not*bubblesort; and - you better never use variables in Python named
`list`

,`dict`

, etc.

*BubbeSort* sorts by comparing two **adjacent** elements: the so-called “bubble”. If checks if the left item is indeed less than the right one. If this is not the case, it swaps the elements. The algorithm iterates maximum *n* times over the list, after which it is guaranteed to be sorted.

So a very basic implementation would be:

```
def BubbleSort(data):
for _ in range(len(data)): # iterate n times
for i in range(len(data)-1): # i is the left index of the bubble
if data[i+1] > data[i]: # if the left item is greater
# perform a swap
temp = data[i]
data[i] = data[i+1]
data[i+1] = temp
return data
```

Now we can improve the algorithm (approximately let the algorithm work in half the time) by stopping at `len(data)-1-j`

, since after each iteration, the right most element over which the bubble has moved is guaranteed to be the maximum:

```
def BubbleSort(data):
for
```**j** in range(len(data)): # iterate n times
for i in range(len(data)-1**-j**): # i is the left index of the bubble
if data[i+1] > data[i]: # if the left item is greater
# perform a swap
temp = data[i]
data[i] = data[i+1]
data[i+1] = temp
return data

But using bubblesort is – except for some very rare cases – inefficient. It is better to use faster algorithms like *QuickSort*, *MergeSort*, and *TimSort* (the builtin sorting algorithm of Python).

Here’s a short list of books which implement `bubble sort:`

- Fundamentals of Python: Data Structures, 1st ed.: Data Structures
- Introduction to Numerical Programming: A Practical Guide for Scientists and Engineers Using Python and C/C++ (Series in Computational Physics)
- Fundamentals of Python: First Programs
- Python 3 for Absolute Beginners (Expert’s Voice in Open Source)
- Python Data Structures and Algorithms

You can change the for loop from index a + 1 instead of 0 which will avoid comparing the first element to itself. Makes it a bit faster.

Use the swap function to swap the values of the list[a] and list[b] elements, rather than using a temporary variable.

Use the sorted function to check if the list is already sorted, and return the list immediately if it is.