Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does bubble sort compare to selection sort?

Which sorting technique is faster: bubble or selection sort, and why? Are both equally efficient?

like image 761
algo-geeks Avatar asked Dec 30 '10 09:12

algo-geeks


People also ask

What is the difference between the bubble sort and selection sort?

In bubble sort, two adjacent elements are compared. If the adjacent elements are not at the correct position, swapping would be performed. In selection sort, the minimum element is selected from the array and swap with an element which is at the beginning of the unsorted sub array.

Why bubble sort is better than selection sort?

Bubble sort uses more swap times, while selection sort avoids this. When using selecting sort it swaps n times at most. but when using bubble sort, it swaps almost n*(n-1). And obviously reading time is less than writing time even in memory.

Which is faster between bubble sort and selection sort?

Bubble sort takes an order of n time whereas selection sort consumes an order of n2 time. Bubble sort is a stable algorithm, in contrast, selection sort is unstable. Selection sort algorithm is fast and efficient as compared to bubble sort which is very slow and inefficient.


1 Answers

Wikipedia says (emphasis added):

Among simple average-case Θ(n2) algorithms, selection sort almost always outperforms bubble sort and gnome sort, but is generally outperformed by insertion sort. Insertion sort is very similar in that after the kth iteration, the first k elements in the array are in sorted order. Insertion sort's advantage is that it only scans as many elements as it needs in order to place the k + 1st element, while selection sort must scan all remaining elements to find the k + 1st element.

Simple calculation shows that insertion sort will therefore usually perform about half as many comparisons as selection sort, although it can perform just as many or far fewer depending on the order the array was in prior to sorting. It can be seen as an advantage for some real-time applications that selection sort will perform identically regardless of the order of the array, while insertion sort's running time can vary considerably. However, this is more often an advantage for insertion sort in that it runs much more efficiently if the array is already sorted or "close to sorted."

While selection sort is preferable to insertion sort in terms of number of writes (Θ(n) swaps versus Ο(n2) swaps), it almost always far exceeds (and never beats) the number of writes that cycle sort makes, as cycle sort is theoretically optimal in the number of writes. This can be important if writes are significantly more expensive than reads, such as with EEPROM or Flash memory, where every write lessens the lifespan of the memory.

Finally, selection sort is greatly outperformed on larger arrays by Θ(n log n) divide-and-conquer algorithms such as mergesort. However, insertion sort or selection sort are both typically faster for small arrays (i.e. fewer than 10-20 elements). A useful optimization in practice for the recursive algorithms is to switch to insertion sort or selection sort for "small enough" sublists.

And, Wikipedia on bubble sort (emphasis added):

Bubble sort has worst-case and average complexity both О(n2), where n is the number of items being sorted. There exist many sorting algorithms with substantially better worst-case or average complexity of O(n log n). Even other О(n2) sorting algorithms, such as insertion sort, tend to have better performance than bubble sort. Therefore, bubble sort is not a practical sorting algorithm when n is large.

The only significant advantage that bubble sort has over most other implementations, even quicksort, but not insertion sort, is that the ability to detect that the list is sorted is efficiently built into the algorithm. Performance of bubble sort over an already-sorted list (best-case) is O(n). By contrast, most other algorithms, even those with better average-case complexity, perform their entire sorting process on the set and thus are more complex. However, not only does insertion sort have this mechanism too, but it also performs better on a list that is substantially sorted (having a small number of inversions).

like image 76
Cody Gray Avatar answered Sep 23 '22 18:09

Cody Gray