Insertion sort vs Bubble Sort Algorithms

后端 未结 11 911
名媛妹妹
名媛妹妹 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:38

    Insertion sort can be resumed as "Look for the element which should be at first position(the minimum), make some space by shifting next elements, and put it at first position. Good. Now look at the element which should be at 2nd...." and so on...

    Bubble sort operate differently which can be resumed as "As long as I find two adjacent elements which are in the wrong order, I swap them".

提交回复
热议问题