Insertion sort vs Bubble Sort Algorithms

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

    Number of swap in each iteration

    • Insertion-sort does at most 1 swap in each iteration.
    • Bubble-sort does 0 to n swaps in each iteration.

    Accessing and changing sorted part

    • Insertion-sort accesses(and changes when needed) the sorted part to find the correct position of a number in consideration.
    • When optimized, Bubble-sort does not access what is already sorted.

    Online or not

    • Insertion-sort is online. That means Insertion-sort takes one input at a time before it puts in appropriate position. It does not have to compare only adjacent-inputs.
    • Bubble-sort is not-online. It does not operate one input at a time. It handles a group of inputs(if not all) in each iteration. Bubble-sort only compare and swap adjacent-inputs in each iteration.

提交回复
热议问题