Insertion sort vs Bubble Sort Algorithms

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

    After i iterations the first i elements are ordered.

    In each iteration the next element is bubbled through the sorted section until it reaches the right spot:

    sorted  | unsorted
    1 3 5 8 | 4 6 7 9 2
    1 3 4 5 8 | 6 7 9 2
    

    The 4 is bubbled into the sorted section

    Pseudocode:

    for i in 1 to n
        for j in i downto 2
            if array[j - 1] > array[j]
                swap(array[j - 1], array[j])
            else
                break
    

    Bubble Sort

    After i iterations the last i elements are the biggest, and ordered.

    In each iteration, sift through the unsorted section to find the maximum.

    unsorted  | biggest
    3 1 5 4 2 | 6 7 8 9
    1 3 4 2 | 5 6 7 8 9
    

    The 5 is bubbled out of the unsorted section

    Pseudocode:

    for i in 1 to n
        for j in 1 to n - i
             if array[j] > array[j + 1]
                 swap(array[j], array[j + 1])
    

    Note that typical implementations terminate early if no swaps are made during one of the iterations of the outer loop (since that means the array is sorted).

    Difference

    In insertion sort elements are bubbled into the sorted section, while in bubble sort the maximums are bubbled out of the unsorted section.

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

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

    well bubble sort is better than insertion sort only when someone is looking for top k elements from a large list of number i.e. in bubble sort after k iterations you'll get top k elements. However after k iterations in insertion sort, it only assures that those k elements are sorted.

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

    In bubble sort in ith iteration you have n-i-1 inner iterations (n^2)/2 total, but in insertion sort you have maximum i iterations on i'th step, but i/2 on average, as you can stop inner loop earlier, after you found correct position for the current element. So you have (sum from 0 to n) / 2 which is (n^2) / 4 total;

    That's why insertion sort is faster than bubble sort.

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

    Though both the sorts are O(N^2).The hidden constants are much smaller in Insertion sort.Hidden constants refer to the actual number of primitive operations carried out.

    When insertion sort has better running time?

    1. Array is nearly sorted-notice that insertion sort does fewer operations in this case, than bubble sort.
    2. Array is of relatively small size: insertion sort you move elements around, to put the current element.This is only better than bubble sort if the number of elements is few.

    Notice that insertion sort is not always better than bubble sort.To get the best of both worlds, you can use insertion sort if array is of small size, and probably merge sort(or quicksort) for larger arrays.

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