4

i m calculating running time for this algorithm?

                              Cost      No Of Times

for(j=1;j<=n-1;j++){          c1       n(loop will run for n-1 times +1 for failed cond              

    for(i=0;i<=n-2;i++){      c2       n*(n-1) (n-1 from outer loop and n for inner 

        if(a[i]>a[i+1]){      c3       (n-1)*(n-1)

            Swap              c4       (n-1)*(n-1) {in worst case }
        }
    }

in worst case T(n)= c1*n + c2*(n-1)n + c3(n-1)(n-1) + c4*(n-1)(n-1) which is O(n^2)

In Best case:

T(n)=c1*n + c2*(n-1)n + c3(n-1)(n-1) which is O(n^2).

BUT actually in best case bubble sort has time complexity O(n). Can anyone explain?

Ravi Bisla
  • 405
  • 1
  • 4
  • 10
  • Yes, which turns out as `O(n^2)` which is the cost of bubble sort which you're doing here... which you could have found by googling the name of the algorithm and going into the first result. http://en.wikipedia.org/wiki/Bubble_sort – Benjamin Gruenbaum Jun 29 '13 at 13:07
  • i have checked that but i had a doubt in deriving that, that's why i posted. – Ravi Bisla Jun 29 '13 at 13:12

3 Answers3

3

Bubble Sort has O(n) time complexity in the best case because it is possible to pass an already sorted list to it.

You have to check if you did any swaps after the second nested loop. If no swaps were done, the list is sorted and there's no need to continue, so you can break the loop.

For an already-sorted list, you'd have iterated over all n elements once in this case.

user123
  • 8,970
  • 2
  • 31
  • 52
2

your algo for implementing bubble sort is correct but not efficient,

// n is the total number of elments

do{  

  swp = false // swp checks whether or not any variable has been swapped  
                      in the inner loop  

         for(i=0;i<=n-2;i++){  

                  if(a[i]>a[i+1])

                  {
                        swap(a[i],a[i+1])

                        sw = true
                   }
        n = n-1             
    }while(sw == true && n>0)

swp is a variable which checks whether there has been any swap in the inner loop or not,
if there has not been any swap this means that our array is sorted.

The best case for bubble sort is when the elements are already sorted in ascending order(in this case)
for which the inner loop just runs once but the if condition(in the inner loop) is never satisfied and swp remains false and thus we exit from the outer loop after one iteration which gives bubble sort O(n) complexity.

RYO
  • 121
  • 6
0

You can compute the number of iterations (what's inside the loop is irrelevant because it's of constant time) using Sigma Notation:

enter image description here

Bubble Sort with a best case running time is actually an enhanced version of this sorting algorithm.

During the first parse (outer loop), if no swap was performed, that is a decisive information that the array is sorted, and it is pointless to cover all cases.

Therefore, the outer loop would iterate once, and the inner loop would iterate n times: that's n + 1 iterations overall ==> O(n).