0

I did the below code to check the number of iteration and swaps required for bubble sort and insertion sort. Even though (referring to below code) insertion sort did literally half iterations and same number of swaps as compared to bubble sort then how come both have same BIG-O complexity

static void bubbleSortExample()
    {
        int iterationCount=0;
        int swaps=0;
        int [] arr={2,6,1,4,8,7,10,3};
        int temp=0;
        for(int i=0; i< arr.length; i++)
        {
            iterationCount=iterationCount+1;
            for(int j=0; j<arr.length-1; j++)
            {
                iterationCount=iterationCount+1;
                if(arr[j]> arr[j+1])
                {
                    swaps= swaps+1;
                    temp= arr[j+1];
                    arr[j+1]= arr[j];
                    arr[j]= temp;
                }
            }
        }
        System.out.println("Bubble Sort----Iteration count are : " + iterationCount + " and swaps are : " + swaps);
    }
    //Bubble Sort Example Ends


    //Insertion Sort Example Starts
    static void insertionSortExample()
    {
        int iterationCount=0;
        int swaps=0;
        int [] arr={2,6,1,4,8,7,10,3};

        for(int i=1;i< arr.length;i++)
        {
            iterationCount=iterationCount+1;
            int key=arr[i];// this is the number that needs to be inserted in its correct position
            for(int j=i-1; j >= 0;  j--)
            {
                iterationCount=iterationCount+1;
                if(key < arr[j])
                {
                    swaps= swaps+1;
                    int t= arr[j];
                    arr[j]=key;
                    arr[j+1]=t;

                }
            }
        }
        System.out.println("Insertion Sort----Iteration count are : " + iterationCount + " and swaps are : " + swaps);
    }

OUTPUT

Bubble Sort----Iteration count are : 64 and swaps are : 9
Insertion Sort----Iteration count are : 35 and swaps are : 9
Amit
  • 6,839
  • 21
  • 56
  • 90
  • 7
    Big-O describes behaviour as the size of the input increases. Your test includes only an array with fixed size, and so is irrelevant to Big-O analysis. Furthermore, constant factors are ignored in Big-O analysis, so it is quite possible for insertion sort to do "literally half" the number of operations while still having the same Big-O complexity as bubble sort. – Mankarse Jul 09 '13 at 10:10

3 Answers3

3

Whoa!Whoa! Wait.You are confusing two things.
One is running time which is the actual running time of a program on an instance of input.
Second is time complexity which is how the running time grows as input size grows.

A program which is O(N^2) can run much faster than a code which is O(NlogN) in practise.This is because the inputs may be mostly average cases, however the Big-Oh analysis is meant only for worst case analysis.This is because Big-Oh does not tell about actual running time(which may depend on nature of input(best case/worst case), details of actual implementation).Big-Oh only gives us a guarentee that an algorithm will run no worse than a constant times that function.

You can read my answers here to clarify these.

So when we say bubble sort/insertion sort is O(N2), we mean to say that that the running time in the worst case scenario is no larger than a constant times N^2.Realize that this is indeed the case for the two algorithms.

If you still have confusion please feel free to ask.

Community
  • 1
  • 1
Aravind
  • 3,169
  • 3
  • 23
  • 37
  • you explained it well, thanks for that. Can you point me to some articles/book where i can read more about Algorithms and calculating its complexity – Amit Jul 09 '13 at 11:00
  • I have had trouble finding good articles, the best way is try to solve it yourself, and if you have trouble, you can ask here.That's how I learned.Or if you are mathematically inclined you can read sections from CLRS(Title of book:Introduction to Algorithms) – Aravind Jul 09 '13 at 11:11
  • 1
    Actually, you often find that people analyse Worst Case, Best Case and Average Case through the notation. Good clarification though. – Janis F Jul 09 '13 at 11:29
  • Big-Oh is a general notation that can be used on describing any case of input, but one should understand what it means when they are using it.It's true though that it's a notation that is often misunderstood. – Aravind Jul 09 '13 at 11:34
1

Keep in mind that the notation just expresses how the algorithm behaves as n grows. A linear factor is always dropped from that. So it really doesn't state whether an algorithm is fast, it just states by what factor it will take more time to complete as you increase n.

Janis F
  • 2,637
  • 1
  • 25
  • 36
0

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.

Amir Bilal
  • 447
  • 3
  • 7
  • I am not debating that insertion sort is faster, i am asking if its faster with similar number of swaps then how come both have similar BIG-O – Amit Jul 09 '13 at 10:21
  • It might baffle you because you have a wrong idea of what BIG-O denotes. – Janis F Jul 09 '13 at 12:22