Insertion sort vs Bubble Sort Algorithms

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

    Another difference, I didn't see here:

    Bubble sort has 3 value assignments per swap: you have to build a temporary variable first to save the value you want to push forward(no.1), than you have to write the other swap-variable into the spot you just saved the value of(no.2) and then you have to write your temporary variable in the spot other spot(no.3). You have to do that for each spot - you want to go forward - to sort your variable to the correct spot.

    With insertion sort you put your variable to sort in a temporary variable and then put all variables in front of that spot 1 spot backwards, as long as you reach the correct spot for your variable. That makes 1 value assignement per spot. In the end you write your temp-variable into the the spot.

    That makes far less value assignements, too.

    This isn't the strongest speed-benefit, but i think it can be mentioned.

    I hope, I expressed myself understandable, if not, sorry, I'm not a nativ Britain

    0 讨论(0)
  • 2020-12-07 09:21

    Bubble sort is almost useless under all circumstances. In use cases when insertion sort may have too many swaps, selection sort can be used because it guarantees less than N times of swap. Because selection sort is better than bubble sort, bubble sort has no use cases.

    0 讨论(0)
  • 2020-12-07 09:26

    Bubble Sort is not online (it cannot sort a stream of inputs without knowing how many items there will be) because it does not really keep track of a global maximum of the sorted elements. When an item is inserted you will need to start the bubbling from the very beginning

    0 讨论(0)
  • 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.
    0 讨论(0)
  • 2020-12-07 09:33

    insertion sort:

    1.In the insertion sort swapping is not required.

    2.the time complexity of insertion sort is Ω(n)for best case and O(n^2) worst case.

    3.less complex as compared to bubble sort.

    4.example: insert books in library, arrange cards.

    bubble sort: 1.Swapping required in bubble sort.

    2.the time complexity of bubble sort is Ω(n)for best case and O(n^2) worst case.

    3.more complex as compared to insertion sort.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题