Efficiency of Bubble vs. Selection Sort

前端 未结 2 1316
-上瘾入骨i
-上瘾入骨i 2021-01-23 01:11

I understand that the big O values for Bubble Sort and Selection Sort are the same, (n)^2, but when I try to run both with an array of size 1000, the Bubble Sort takes 962037 sw

2条回答
  •  无人共我
    2021-01-23 01:57

    The Big O notation provides the asymptomatic cost, however, all additive values and constants are omitted. In addition, for a realistic comparison for small numbers, you need to look at the average number of comparisons. More specifically, Bubble sort requires, on average, n/4 swaps per entry , while Selection sort requires only 1, see this post for further details. The number of comparisons will also depend on the initial distribution, for example whether the data is semi-sorted or completely random.

提交回复
热议问题