简体   繁体   中英

Calculating time complexity by just seeing the algorithm code

I have currently learned the code of all sorting algorithms used and understood their functioning. However as a part of these, one should also be capable to find the time and space complexity. I have seen people just looking at the loops and deriving the complexity. Can someone guide me towards the best practice for achieving this. The given example code is for "Shell sort". What should be the strategy used to understand and calculate from code itself. Please help! Something like step count method. Need to understand how we can do asymptotic analysis from code itself. Please help.

int i,n=a.length,diff=n/2,interchange,temp;
while(diff>0) { 
    interchange=0;
    for(i=0;i<n-diff;i++) {
        if(a[i]>a[i+diff]) { 
            temp=a[i];
            a[i]=a[i+diff];
            a[i+diff]=temp;
            interchange=1;
        }
    }
    if(interchange==0) {
        diff=diff/2;
    }
} 

Since the absolute lower bound on worst-case of a comparison-sorting algorithm is O(n log n) , evidently one can't do any better. The same complexity holds here.

Worst-case time complexity :

1. Inner loop

Let's first start analyzing the inner loop:

for(i=0;i<n-diff;i++) {
    if(a[i]>a[i+diff]) {
        temp=a[i];
        a[i]=a[i+diff];
        a[i+diff]=temp;
        interchange=1;
    }
}

Since we don't know much (anything) about the structure of a on this level, it is definitely possible that the condition holds, and thus a swap occurs. A conservative analysis thus says that it is possible that interchange can be 0 or 1 at the end of the loop. We know however that if we will execute the loop a second time, with the same diff value.

As you comment yourself, the loop will be executed O(n-diff) times. Since all instructions inside the loop take constant time. The time complexity of the loop itself is O(n-diff) as well.

Now the question is how many times can interchange be 1 before it turns to 0 . The maximum bound is that an item that was placed at the absolute right is the minimal element, and thus will keep "swapping" until it reaches the start of the list. So the inner loop itself is repeated at most: O(n/diff) times. As a result the computational effort of the loop is worst-case:

O(n^2/diff-n)=O(n^2/diff-n)

2. Outer loop with different diff

The outer loop relies on the value of diff . Starts with a value of n/2 , given interchange equals 1 at the end of the loop, something we cannot prove will not be the case, a new iteration will be performed with diff being set to diff/2 . This is repeated until diff < 1 . This means diff will take all powers of 2 up till n/2 :

1 2 4 8 ... n/2

Now we can make an analysis by summing:

log2 n
------
 \
 /      O(n^2/2^i-n) = O(n^2)
------
i = 0

where i represents *log 2 (diff) of a given iteration. If we work this out, we get O(n 2 ) worst case time complexity.

Note (On the lower bound of worst-case comparison sort): One can proof no comparison sort algorithm exists with a worst-case time complexity of O(n log n) .

This is because for a list with n items, there are n! possible orderings. For each ordering, there is a different way one needs to reorganize the list.

Since using a comparison can split the set of possible orderings into two equals parts at the best, it will require at least log 2 (n!) comparisons to find out which ordering we are talking about. The complexity of log 2 (n) can be calculated using the Stirling approximation :

  n /\\ | | log(x) dx = n log n - n = O(n log n) \\/ 1 

Best-case time complexity: in the best case, the list is evidently ordered. In that case the inner loop will never perform the if-then part. As a consequence, the interchange will not be set to 1 and therefore after executing the for loop one time. The outer loop will still be repeated O(log n) times, thus the time complexity is O(n log n) .

Look at the loops and try to figure out how many times they execute. Start from the innermost ones.

In the given example (not the easiest one to begin with), the for loop (innermost) is excuted for i in range [0,n-diff] , ie it is executed exactly n-diff times.

What is done inside that loop doesn't really matter as long as it takes "constant time", ie there is a finite number of atomic operations.

Now the outer loop is executed as long as diff>0 . This behavior is complex because an iteration can decrease diff or not (it is decreased when no inverted pair was found).

Now you can say that diff will be decreased log(n) times (because it is halved until 0), and between every decrease the inner loop is run "a certain number of times".

An exercised eye will also recognize interleaved passes of bubblesort and conclude that this number of times will not exceed the number of elements involved, ie n-diff , but that's about all that can be said "at a glance".

Complete analysis of the algorithm is an horrible mess, as the array gets progressively better and better sorted, which will influence the number of inner loops.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM