-1
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ More efficient than what, exactly? $\endgroup$
    – kviiri
    Jan 22 at 19:42

2 Answers 2

2
$\begingroup$

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.

$\endgroup$
4
  • $\begingroup$ 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. $\endgroup$
    – gnasher729
    Jan 23 at 7:37
  • $\begingroup$ Good answer for including what benefit Bubble sort does have: quick to write, hard to get wrong. $\endgroup$
    – kutschkem
    Jan 23 at 8:00
  • $\begingroup$ @gnasher729 You are right, but even in that case I'd rather use an insertion sort. $\endgroup$
    – Nathaniel
    Jan 23 at 9:21
  • $\begingroup$ @gnasher729: is that an intuition or a known fact ? $\endgroup$ Jan 24 at 8:22
0
$\begingroup$

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)

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.