# Is the bubble sort algorithm more efficient for sorting and how to implement it?

I don't know which algorithm is more efficient for sorting, but I want to use Bubble sort and how to implement this algorithm.

• More efficient than what, exactly? Jan 22 at 19:42

## 2 Answers

No, it is not efficient at all. Its main advantage is that it is short to write, but that's all.

See here for a comparison on comparison sorts.

If you still want to implement Bubble Sort, check here for a description.

• There is a case where bubblesort is optimal: if you started with a sorted array A, and changed all keys by a tiny amount to get an array A’, and need to sort A’. Depends on the array size and the size of the changes. Jan 23 at 7:37
• Good answer for including what benefit Bubble sort does have: quick to write, hard to get wrong. Jan 23 at 8:00
• @gnasher729 You are right, but even in that case I'd rather use an insertion sort. Jan 23 at 9:21
• @gnasher729: is that an intuition or a known fact ? Jan 24 at 8:22

I think a good starting point is to look at a concrete sorting problem and then check the most voted solutions on LeetCode.

If you want to know how good bubble sort works on the problem you can just filter the submitted solutions for "bubble sort" and compare the runtime of it with the other (e.g. higher rated) solutions using other sort algorithms (but: you need to register to be able to run solutions and compare their runtimes)

• The votes on LeetCode should be considered with a strong critical mind. Better read some theory about sorting. Jan 24 at 8:24