Insertion sort vs Bubble Sort Algorithms

后端 未结 11 912
名媛妹妹
名媛妹妹 2020-12-07 08:51

I\'m trying to understand a few sorting algorithms, but I\'m struggling to see the difference in the bubble sort and insertion sort algorithm.

I know both are O(n

11条回答
  •  难免孤独
    2020-12-07 09:35

    The main advantage of insert sort is that it's online algorithm. You don't have to have all the values at start. This could be useful, when dealing with data coming from network, or some sensor.

    I have a feeling, that this would be faster than other conventional n log(n) algorithms. Because the complexity would be n*(n log(n)) e.g. reading/storing each value from stream (O(n)) and then sorting all the values (O(n log(n))) resulting in O(n^2 log(n))

    On the contrary using Insert Sort needs O(n) for reading values from the stream and O(n) to put the value to the correct place, thus it's O(n^2) only. Other advantage is, that you don't need buffers for storing values, you sort them in the final destination.

提交回复
热议问题