Efficency of Insertion Sort vs Bubble sort vs Selection sort?

后端 未结 3 439
野的像风
野的像风 2021-01-07 13:41

I have written down that Insertion Sort is faster than Selection Sort, which is faster than Bubble Sort, and that their running time for all 3 are O(n^2), but what can I say

3条回答
  •  不知归路
    2021-01-07 14:19

    There are several ways to see that insertion/selection/bubble sort all run in n^2 time.

    • They use nested loops: n outer-loops, and each with n/2 inner-loops on average
    • They compare all pairs of elements: there are n*(n-1)/2 pairs

    Here are some detailed analysis on the running of insertion/selection/bubble sort.

提交回复
热议问题