19
for (int front = 1; front < intArray.length; front++)
{
    for (int i = 0; i  < intArray.length - front; i++)
    {
        if (intArray[i] > intArray[i + 1])
        {
            int temp = intArray[i];
            intArray[i] = intArray[i + 1];
            intArray[i + 1] = temp;
        }
    }
}

The inner loop is iterating: n + (n-1) + (n-2) + (n-3) + ... + 1 times.

The outer loop is iterating: n times.

So you get n * (the sum of the numbers 1 to n)

Isn't that n * ( n*(n+1)/2 ) = n * ( (n^2) + n/2 )

Which would be (n^3) + (n^2)/2 = O(n^3) ?

I am positive I am doing this wrong. Why isn't O(n^3)?

Marome
  • 47
  • 1
  • 11
ordinary
  • 5,943
  • 14
  • 43
  • 60
  • You are counting the outer `n` twice. Your inner loop itself is O(n). – H H Jul 13 '12 at 20:20
  • 10
    Not to nitpick but the algorithm you show is a [Selection sort](http://en.wikipedia.org/wiki/Selection_sort) not a [Bubble sort](http://en.wikipedia.org/wiki/Bubble_sort) – Frank Boyne Jul 13 '12 at 20:33
  • 1
    Last week, I have written article about asymptotic complexity and by coincidence, I use bubble sort as an example. Give it a shot :-) (http://en.algoritmy.net/article/44682/Asymptotic-complexity). Your mistake is, as it was correctly said by Henk, that the inner loop is O(n). O(n^2) - the sum of arithmetic order is complexity of both loops together. – malejpavouk Jul 13 '12 at 20:39
  • I agree, this is not bubble sort – whamsicore Jul 15 '15 at 02:21
  • adding a comment because the algo shown here IS bubble sort after the edits. i read the comments and was confused, but believe it's resolved now. – Switch386 Sep 24 '21 at 03:20

6 Answers6

22

You are correct that the outer loop iterates n times and the inner loop iterates n times as well, but you are double-counting the work. If you count up the total work done by summing the work done across each iteration of the top-level loop you get that the first iteration does n work, the second n - 1, the third n - 2, etc., since the ith iteration of the top-level loop has the inner loop doing n - i work.

Alternatively, you could count up the work done by multiplying the amount of work done by the inner loop times the total number of times that loop runs. The inner loop does O(n) work on each iteration, and the outer loop runs for O(n) iterations, so the total work is O(n2).

You're making an error by trying to combine these two strategies. It's true that the outer loop does n work the first time, then n - 1, then n - 2, etc. However, you don't multiply this work by n to to get the total. That would count each iteration n times. Instead, you can just sum them together.

Hope this helps!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • 8
    Might be worth adding that Big O describes the _growth rate_ of an algorithm proportional to the size of the inputs which is not necessarily the same thing as the exact amount of iterations taken for the algorithm to run. –  Jul 13 '12 at 20:29
  • Would it be accurate to say that BubbleSort completes (n-1)*(n-1) iterations? therefore N^2 iterations. This is the time complexity. Am I right in assuming this? – Tiny Dec 11 '15 at 22:26
  • 1
    Bubblesort does not do (n-1)*(n-1), it does Outer loop (n-1) : inner loop [(n-1),(n-2),(n-3),...,(2),(1)] So you can say buble sort iterates for inner loop [(n-1),(n-2),(n-3),...,(2),(1)] times. Which is n(n-1)/2 times, which is not N^2 times, But as user "user849425" suggested in comment Above, Big O is not a number of iterations. – Pankaj Jul 07 '18 at 10:14
  • Big O notations are so confusing. Inner loop Does O(n-i) and NOT O(n) – Pankaj Jul 07 '18 at 10:18
10

Your inner loop is iterating, IN TOTAL, as you said n + (n-1) + (n-2) + (n-3) + ... + 1 times. So it is O(n + (n-1) + (n-2) + (n-3) + ... + 1) = O(n(n+1)/2) = O(n^2)

GL770
  • 2,910
  • 1
  • 14
  • 9
3
k=1(sigma k)n = n(n+1)/2
because:
  s = 1 +  2    + ... + (n-1) + n
  s = n + (n-1) + ... + 2     + 1
+)
===================================
  2s = n*(n+1)
   s = n(n+1)/2
in bubble sort, 
(n-1) + (n-2) + ... + 1 + 0 times compares 
which means, k=0(sigma k)n-1
, k=0(sigma k)n-1 equals [k=1(sigma k)n] - n
therefore, n(n+1)/2 - n = n(n-1)/2
which is 1/2(n^2-n) => O(1/2(n^2-n))
in big O notation, we remove constant, so
O(n^2-n)
n^2 is larger than n
O(n^2)
winuxguy
  • 31
  • 1
1

The inner loop iterates n times(in worst case):

for(int i = front; i < intArray.length; i++)

The outer loop iterates n times:

for(int front = 0; front < intArray.length; front++)

Therefore O(n^2)

NominSim
  • 8,447
  • 3
  • 28
  • 38
1

How you basically calculate N...

  • Each line: +1
  • Each Loop *N

    So you start adding numbers get to your first loop now you have N+1, you keep going and you eventually get N*N or N^2 for the time plus some number. Pulling off the number as it is generally insignificant compared to N.

Pretty much N is a representation of all the items in the loop kind of like 1,2,3...N. So it is simply representing a number not how many times a loop, loops.

Travis Pessetto
  • 3,260
  • 4
  • 27
  • 55
0

This is another version to speed up bubble sort, when we use just a variable swapped to terminate the first for loop early. You can gain better time complexity.

#include <stdio.h>
#include <stdbool.h>
#define MAX 10

int list[MAX] = {1,8,4,6,0,3,5,2,7,9};

void display(){
   int i;
   printf("[");

   for(i = 0; i < MAX; i++){
      printf("%d ",list[i]);
   }

   printf("]\n");
}

void bubbleSort() {
   int temp;
   int i,j;

   bool swapped = false;       

   // 1st loop
   for(i = 0; i < MAX-1; i++) { 
      swapped = false;

      // 2nd loop
      for(j = 0; j < MAX-1-i; j++) {
         printf("     Compare: [ %d, %d ] ", list[j],list[j+1]);

         if(list[j] > list[j+1]) {
            temp = list[j];
            list[j] = list[j+1];
            list[j+1] = temp;

            swapped = true;
         }

      }

      if(!swapped) {
         break;
      }

      printf("Loop number %d#: ",(i+1)); 
      display();                     
   }

}

main(){
   printf("Before: ");
   display();
   printf("\n");

   bubbleSort();
   printf("\nAfter: ");
   display();
}
Giang Nguyen
  • 450
  • 8
  • 17