how to calculate Bubble sort Time Complexity

前端 未结 7 1243
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-05 12:29

I was trying to understand the Data Structure and different algorithm, then i got confused to measure the Bubble sort time complexity.

for (c = 0; c < ( n         


        
相关标签:
7条回答
  • 2020-12-05 12:33

    Best case: This time complexity can occur if the array is already sorted. That means no swapping occurs and only 1 iteration of n elements will be there.

    So time complexity is O(n).

    Worst case: This time complexity can occur if the array is already sorted but is descending order.

    In 1st iteration, number of comparison = n-1
    In 2nd iteration, number of comparison = n-2
    .......................................................................
    .......................................................................
    .......................................................................
    In (n-2)th iteration, number of comparison = 2
    In (n-1)th iteration, number of comparison = 1

    for n elements total number of iteration= n-1
    Total number of comparison S = (n-1)+ (n-2) +........ +    2    +    1
    We can write this also          S =      1 +      2 + ........+(n-2) + (n-1)
    ................................................................................................................................                                           2S =       n +     n + ......... +     n +       n .... [Adding both line]
                                              2S = n(n-1) ..... [as total no of iteration = n-1]
                                               S = n(n-1)/2
    In polynomial function, highest order of n is considered as time complexity.
    So, Time Complexity is O(n^2)

    0 讨论(0)
  • 2020-12-05 12:37

    So you've noticed that the total number of comparisons done is (n - 1) + ... + 2 + 1. This sum is equal to n * (n - 1) / 2 (see Triangular Numbers) which is equal to 0.5 n^2 - 0.5 n which is clearly O(n^2).

    0 讨论(0)
  • 2020-12-05 12:37

    it does comparison between two elements. so in 1st Phase - n-1 comparison. i.e, 6 2nd phase - n-2 comparison. i.e, 5 and so on till 1. and thus, sum = n(n-1)/2 i.e O(n^2).

    if there is any error you can correct.....

    0 讨论(0)
  • 2020-12-05 12:40

    Let's go through the cases for Big O for Bubble Sort

    Case 1) O(n) (Best case) This time complexity can occur if the array is already sorted, and that means that no swap occurred and only 1 iteration of n elements

    Case 2) O(n^2) (Worst case) The worst case is if the array is already sorted but in descending order. This means that in the first iteration it would have to look at n elements, then after that it would look n - 1 elements (since the biggest integer is at the end) and so on and so forth till 1 comparison occurs. Big-O = n + n - 1 + n - 2 ... + 1 = (n*(n + 1))/2 = O(n^2)

    In your example, it may not examine these many elements in each phase as the array is not in descending order.

    0 讨论(0)
  • 2020-12-05 12:44

    For n number of numbers, total number of comparisons done will be (n - 1) + ... + 2 + 1. This sum is equal to (n-1) * n / 2 (see Triangular Numbers) which equals to 0.5 n^2 - 0.5 n i.e. O(n^2)

    0 讨论(0)
  • 2020-12-05 12:49

    Explaining for worst case here :

    elements = raw_input("enter comma separated elements : ")
    elements = elements.split(',')
    elements = map(int, elements)
    length = len(elements)
    
    for i in xrange(length - 1):
        print "outer pass : ", i
       for j in xrange(length - i - 1):
           print "inner pass : ", j
           if elements[j] > elements[j + 1]:
               elements[j + 1], elements[j] = elements[j], elements[j + 1]
           print "elements : ", elements
    print elements
    

    Output :

    enter comma separated elements : 5,4,3,2,1

    outer pass : 0

    inner pass : 0

    elements : [4, 5, 3, 2, 1]

    inner pass : 1

    elements : [4, 3, 5, 2, 1]

    inner pass : 2

    elements : [4, 3, 2, 5, 1]

    inner pass : 3

    elements : [4, 3, 2, 1, 5]

    outer pass : 1

    inner pass : 0

    elements : [3, 4, 2, 1, 5]

    inner pass : 1

    elements : [3, 2, 4, 1, 5]

    inner pass : 2

    elements : [3, 2, 1, 4, 5]

    outer pass : 2

    inner pass : 0

    elements : [2, 3, 1, 4, 5]

    inner pass : 1

    elements : [2, 1, 3, 4, 5]

    outer pass : 3

    inner pass : 0

    elements : [1, 2, 3, 4, 5]

    [1, 2, 3, 4, 5]

    Thus first iteration all n elements are scanned, it would scan n - 1 elements in the next iteration. So on for all the elements.

    n + n - 1 + n - 2 ... + 1 = (n * (n + 1))/2 = O(n^2)

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