Efficency of Insertion Sort vs Bubble sort vs Selection sort?

后端 未结 3 438
野的像风
野的像风 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.

    0 讨论(0)
  • 2021-01-07 14:26

    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.

    0 讨论(0)
  • 2021-01-07 14:32

    You can compare sorting algorithms against the following criteria:

    1. Time Complexity (Big-O notation). You should note that best-case, worst-case and average run-time can have different time complexity. For example best-case for Bubble Sort is only O(n), making it faster than Selection Sort when the original list is mostly in order (not many elements out of place).
    2. Memory Complexity. How much more memory is required to sort a list as n grows?
    3. Stability. Does the sort preserve the relative ordering of elements that have equivalent sort values? (For example if you were sorting a list of catalog items by their price, some elements may have equal prices. If the catalog was originally sorted alphabetically by item name, will the chosen sort algortihm preserve the alphabetical ordering within each group of equal-priced items.)
    4. Best/Worst/Averavge number of comparisons required. Important when compare operations are expensive. (For example: comparing efficiencies of alternative designs where efficiency is calculated via some simulation or otherwise complex calculation).
    5. Best/Worst/Average number of swap operations required. Important when swap operations are expensive. (For example: sorting shipping containers that must be physically moved on the deck of a ship)
    6. Code size. Bubble-sort is known for its small code footprint.
    0 讨论(0)
提交回复
热议问题