Insertion sort better than Bubble sort?

24,838

Solution 1

The advantage of bubblesort is in the speed of detecting an already sorted list:

BubbleSort Best Case Scenario: O(n)

However, even in this case insertion sort got better/same performance.

Bubblesort is, more or less, only good for understanding and/or teaching the mechanism of sortalgorithm, but wont find a proper usage in programming these days, because its complexity

O(n²)

means that its efficiency decreases dramatically on lists of more than a small number of elements.

Solution 2

Following things came to my mind:

  1. Bubble sort always takes one more pass over array to determine if it's sorted. On the other hand, insertion sort not need this -- once last element inserted, algorithm guarantees that array is sorted.

  2. Bubble sort does n comparisons on every pass. Insertion sort does less than n comparisons: once the algorithm finds the position where to insert current element it stops making comparisons and takes next element.

  3. Finally, quote from wikipedia article:

Bubble sort also interacts poorly with modern CPU hardware. It requires at least twice as many writes as insertion sort, twice as many cache misses, and asymptotically more branch mispredictions. Experiments by Astrachan sorting strings in Java show bubble sort to be roughly 5 times slower than insertion sort and 40% slower than selection sort

You can find link to original research paper there.

Share:
24,838
Jonathan
Author by

Jonathan

Updated on November 23, 2020

Comments

  • Jonathan
    Jonathan over 3 years

    I am doing my revision for the exam.

    Would like to know under what condition will Insertion sort performs better than bubble sort given same average case complexity of O(N^2).

    I did found some related articles, but I can't understand them.

    Would anyone mind explaining it in a simple way?

  • Jonathan
    Jonathan about 12 years
    so for example a mostly sorted list: e.g. [ 2,3,4,5,1] bubble sort need 4 swaps and 4 comparisons Insertion sort need 4 swaps and 4 comparisons as well. so what is the difference?
  • MarcoS
    MarcoS about 12 years
    in that particular example there's no difference :)
  • Steve Jessop
    Steve Jessop about 12 years
    "bubblesort is only good for understanding and/or teaching the mechanism of sort algorithm" - not even that, I'd say. Insertion sort isn't any harder to understand and not much harder to code. Bubble sort has one very specific advantage, which is that it's provably the most efficient sort for a particular type of storage that doesn't have random access. Drum storage, I think, where the drum rotates at constant speed in one direction only. Then it beats insertion sort because insertion sort needs to "look backwards", which is very slow. This advantage is rarely of practical use these days!
  • Steve Jessop
    Steve Jessop about 12 years
    Bubble sort may be more suited to arrays than bubble sort is to linked lists, but bubble sort is not more suited to arrays than insertion sort is to arrays.
  • Jonathan
    Jonathan about 12 years
    thanks Victor. I found the first 2 points really useful. I understand now one fundamental difference between the 2 algorithms is the number of comparisons required. Cheers
  • b.buchhold
    b.buchhold about 12 years
    Yes, maybe I wasn't clear enough in the last paragraph. The thing is, bubble sort is conceptually trivial on arrays while insertion sort isn't ("move everything from x to right right"). Still it is true, that this doesn't make bubble sort faster.
  • Maduranga Siriwardena
    Maduranga Siriwardena almost 10 years
    Bubble sort can be optimized to run in O(n) running time for best case.
  • Maduranga Siriwardena
    Maduranga Siriwardena almost 10 years
    2nd point seems to be not correct. Yes some algorithms does that. But I think in the correct bubble sort algorithm, the inner loop runs n-1, n-2, n-3 .... times on each iteration of the outer loop.
  • Levent Divilioglu
    Levent Divilioglu about 8 years
    Both bubble and insertion has the same complexity for worst/average/best case performances which is O(n^2) and also space complexity is both O(n) for them.
  • Vineeth Chitteti
    Vineeth Chitteti over 5 years
    @LeventDivilioglu In the best case scenario Bubble Sort can perform at O(n). We can modify bubble sort in such a fashion that if no swaps happen during the first iteration, then we can stop checks as because the list is already sorted.