-->

Insertion sort better than Bubble sort?

2019-06-16 16:42发布

问题:

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?

回答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.



回答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.



回答3:

I guess the answer you're looking for is here:

Bubble sort may also be efficiently used on a list that is already sorted except for a very small number of elements. For example, if only one element is not in order, bubble sort will take only 2n time. If two elements are not in order, bubble sort will take only at most 3n time...

and

Insertion sort is a simple sorting algorithm that is relatively efficient for small lists and mostly sorted lists, and often is used as part of more sophisticated algorithms



回答4:

Could you provide links to the related articles you don't understand? I'm not sure what aspects they might be addressing. Other than that, there is a theoretical difference which might be that bubble sort is more suited for collections represented as arrays (than it is for those represented as linked lists), while insertion sort is suited for linked lists.

The reasoning would be that bubble sort always swaps two items at a time which is trivial on both, array and linked list (more efficient on arrays), while insertion sort inserts at a place in a given list which is trivial for linked lists but involves moving all subsequent elements in an array to the right.

That being said, take it with a grain of salt. First of all, sorting arrays is, in practice, almost always faster than sorting linked lists. Simply due to the fact that scanning the list once has an enormous difference already. Apart from that, moving n elements of an array to the right, is much faster than performing n (or even n/2) swaps. This is why other answers correctly claim insertion sort to be superior in general, and why I really wonder about the articles you read, because I fail to think of a simple way of saying this is better in cases A, and that is better in cases B.



回答5:

In the worst case both tend to perform at O(n^2)

In the best case scenario, i.e., when the array is already sorted, Bubble sort can perform at O(n).