36

It is written on Wikipedia that "... selection sort almost always outperforms bubble sort and gnome sort." Can anybody please explain to me why is selection sort considered faster than bubble sort even though both of them have:

  1. Worst case time complexity: $\mathcal O(n^2)$

  2. Number of comparisons: $\mathcal O(n^2)$

  3. Best case time complexity :

    • Bubble sort: $\mathcal O(n)$
    • Selection sort: $\mathcal O(n^2)$
  4. Average case time complexity :

    • Bubble sort: $\mathcal O(n^2)$
    • Selection sort: $\mathcal O(n^2)$
Juho
  • 22,905
  • 7
  • 63
  • 117
RYO
  • 501
  • 1
  • 5
  • 8

7 Answers7

43

All complexities you provided are true, however they are given in Big O notation, so all additive values and constants are omitted.

To answer your question we need to focus on a detailed analysis of those two algorithms. This analysis can be done by hand, or found in many books. I'll use results from Knuth's Art of Computer Programming.

Average number of comparisons:

  • Bubble sort: $\frac{1}{2}(N^2-N\ln N -(\gamma+\ln2 -1)N) +\mathcal O(\sqrt N)$
  • Insertion sort: $\frac{1}{4}(N^2-N) + N - H_N$
  • Selection sort: $(N+1)H_N - 2N$

Now, if you plot those functions you get something like this: plot plot2

As you can see, bubble sort is much worse as the number of elements increases, even though both sorting methods have the same asymptotic complexity.

This analysis is based on the assumption that the input is random - which might not be true all the time. However, before we start sorting we can randomly permute the input sequence (using any method) to obtain the average case.

I omitted time complexity analysis because it depends on implementation, but similar methods can be used.

Bartosz Przybylski
  • 1,617
  • 1
  • 13
  • 21
13

The asymptotic cost, or $\mathcal O$-notation, describes the limiting behaviour of a function as its argument tends to infinity, i.e. its growth rate.

The function itself, e.g. the number of comparisons and/or swaps, may be different for two algorithms with the same asymptotic cost, provided they grow with the same rate.

More specifically, Bubble sort requires, on average, $n/4$ swaps per entry (each entry is moved element-wise from its initial position to its final position, and each swap involves two entries), while Selection sort requires only $1$ (once the minimum/maximum has been found, it is swapped once to the end of the array).

In terms of the number of comparisons, Bubble sort requires $k\times n$ comparisons, where $k$ is the maximum distance between an entry's initial position and its final position, which is usually larger than $n/2$ for uniformly distributed initial values. Selection sort, however, always requires $(n-1)\times(n-2)/2$ comparisons.

In summary, the asymptotic limit gives you a good feel for how the costs of an algorithm grow with respect to the input size, but says nothing about the relative performance of different algorithms within the same set.

Pedro
  • 1,116
  • 5
  • 14
6

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. The compare time and other running time can be ignored. So swap times is the critical bottleneck of the problem.

simonmysun
  • 181
  • 3
0

$T(n)=O(n^2)$ means that for all $n\ge N$,

$$T(n)\le cn^2$$ for some $N$ and $c$.

In no way does

$$T_a(n)\le c_a n^2$$ and $$T_b(n)\le c_b n^2$$

allow you to draw conclusions on which of $T_a$ and $T_b$ is smaller.

0

I'm surprised that no one mentioned it, but there is a very good metric to measure the running time of the insertion sort: it's the number of inversions, i.e. the pairs $(i,j)$ such that $i<j$ and $a_i > a_j$. Denoting the number of inversions as $I$, insertion sort does $I$ swaps and $O(n) + I$ comparisons.

On the other hand, $I$ is the lower bound on the number of swaps for any algorithm which swaps adjacent elements, so bubble sort can't do better than that. Also, it's easy to construct an example when any implementation of bubble sort will require $n (n-1) / 2$ comparisons (e.g. swap the smallest and the largest elements in the smallest arrays), while the number of inversions is $O(n)$.

Finally, there are some optimizations that you can do in the insertion sort:

  • You can implement swaps more efficiently. When you want to insert $k$ into the sorted prefix of the array, you just need to store $k$, shift all elements that are greater than $k$ to the right, and place $k$ in the freed-up position.
  • You can guarantee that there are at most $O(n \log n)$ comparisons by using binary search.
Dmitry
  • 347
  • 1
  • 4
  • 12
0

Compare bubblesort with a selection sort that looks for the largest element and moves it to the end.

You will see that both actually make the exact same comparisons, finding the largest array element. Bubblesort also moves elements around so the indices it compares constantly change, but the values compared are the same. This extra movement after most comparisons makes bubblesort slower. Most of its n^2/2 comparisons are followed by a swap, and selection sort doesn’t need to do that. Selection sort can manage with one single exchange per element.

So: n^2/2 compare + swap operations vs n2/2 compare operations plus a tiny number of swaps.

gnasher729
  • 32,238
  • 36
  • 56
-1

It is very informal, but intuitively in case of bubble sort on each step you do a smallest possible step towards the goal (swap elements next to each other) while in selection sort you can do long jumps towards the goal.