tags:

views:

855

answers:

5

I am trying Bubble sort. There are 5 elements and array is unsorted. Worst case for bubble sort shuold be O(n^2).

As an exmaple I am using

A = {5, 4, 3, 2, 1}

In this case the comparison should be 5^2 = 25. Using manual verification and code, I am getting comparison count to be 20. Following is the bubble sort implemenation code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SortingAlgo
{
class Program
{
    public static int[] bubbleSort(int[] A)
    {
        bool sorted = false;
        int temp;
        int count = 0;
        int j = 0;
            while (!sorted)
            {
                j++;
                sorted = true;
                for (int i = 0; i < (A.Length - 1); i++)
                {
                    count++;
                    if(A[i] > A[i+1])
                    {
                        temp = A[i];
                        A[i] = A[i+1];
                        A[i+1] = temp;
                        sorted = false;
                    }

                    Console.Write(count + ". -> ");
                    for(int k=0; k< A.Length; k++)
                    {
                        Console.Write(A[k]);
                    }
                    Console.Write("\n");

                }                
            }
      return A;

    }

    static void Main(string[] args)
    {
        int[] A = {5, 4, 3, 2, 1};
        int[] B = bubbleSort(A);
        Console.ReadKey();
    }
   } 
  }

Output is following

  1. -> 45321
  2. -> 43521
  3. -> 43251
  4. -> 43215
  5. -> 34215
  6. -> 32415
  7. -> 32145
  8. -> 32145
  9. -> 23145
  10. -> 21345
  11. -> 21345
  12. -> 21345
  13. -> 12345
  14. -> 12345
  15. -> 12345
  16. -> 12345
  17. -> 12345
  18. -> 12345
  19. -> 12345
  20. -> 12345

Any idea why the maths its not coming out to be 25?

+2  A: 

Remember that O(N^2) is simplified from the actual expression of C * N(2); that is, there is a bounded constant. For bubble sort, for example, C would be roughly 1/2 (not exactly, but close).

Your comparison count is off too, I think, it should be 10 pairwise comparisons. But I guess you could consider swapping of elements to be another. Either way, all that does is change the constant, not the more important part.

StaxMan
O(N^2) states that there exists a C which provides such an upper bound for all N. When you say "C would be roughly 1/2", you seem to be talking about the smallest possible number which satisfies the conditions for C. In fact, for a given algorithm there may not exist a smallest possible C - the set of real numbers satisfying the property might be open below. Bubble sort is worst case n(n-1)/2 comparisons, isn't it? In which case the least possible C does exist, and is exactly 1/2.
Steve Jessop
Yes, I think your summary is correct -- I was specifically referring to case of bubble sort, and why it's not exactly n*n (since only half of that many are needed in worst case; and less for optimal case, which is n, for already sorted array).
StaxMan
+20  A: 

Big-O notation doesn't tell you anything about how many iterations (or how long) an algorithm will take. It is an indication of the growth rate of a function as the number of elements increases (usually towards infinity).

So, in your case, O(n2) simply means that the bubble sort's computational resources grows by the square as the number of elements. So, if you have twice as many elements, you can expect it to take (worst case) 4-times as long (as an upper bound). If you have 4-times as many elements, the complexity increases by a factor of 16. Etc.

For an algorithm with O(n2) complexity, five elements could take 25 iterations, or 25,000 iterations. There's no way to tell without analyzing the algorithm. In the same vein, a function with O(1) complexity (constant time) could take 0.000001 seconds to execute or two weeks to execute.

Robert Cartaino
The most important part is that when you double `N`, the expected number of operations increases four-fold. Big-O is less about the exact number of operations and more about how the number changes with respect to problem size.
D.Shawley
Almost. When you double N, provided N > N_0, the number of operations increases no more than four-fold.
Ken
@Ken - actually, that's not true. Big O notation *only* talks about the limit as N tends to infinity.
Stephen C
+1  A: 

If an algorithm takes n^2 - n operations, that's still simplified to O(n^2). Big-O notation is only an approximation of how the algorithm scales, not an exact measurement of how many operations it will need for a specific input.

Bill the Lizard
A: 

Consider: Your example, bubble-sorting 5 elements, takes 5x4 = 20 comparisons. That generalizes to bubble-sorting N elements takes N x (N-1) = N^2 - N comparisons, and N^2 very quickly gets a LOT bigger than N. That's where O(N^2) comes from. (For example, for 20 elements, you are looking at 380 comparisons.)

John R. Strohm
A: 

Bubble sort is a specific case, and its full complexity is (n*(n-1)) - which gives you the correct number: 5 elements leads to 5*(5-1) operations, which is 20, and is what you found in the worst case.

The simplified Big O notation, however, removes the constants and the least significantly growing terms, and just gives O(n^2). This makes it easy to compare it to other implementations and algorithms which may not have exactly (n*(n-1)), but when simplified show how the work increases with greater input.

It's much easier to compare the Big O notation, and for large datasets the constants and lesser terms are negligible.

Adam Davis
There is no 'full' big O and 'simplified' big O - by the definition of big O, O(n^2) is equivalent to O(n(n-1)). If you're talking about exactly how many iterations it will take, you're not working with big-O notation anymore.
bdonlan
That's true, but for beginners the difference is murky. I'll edit it to be more formally correct.
Adam Davis