You are on page 1of 10

Time Complexity What is the exact time complexity T(n) of the nested loops below?

For simplicity, you may assume that n is a power of 2. That is, n = 2k for some positive integer k.
Program in Java
for (i=1; i <= n/4; i++) { j = n/2; while ( j >= 1 ) { if ( i > j ) k++; j=j/2 ; } }

Program in Pseudo-Code
for i 1 to n/4 do begin jn/2; while ( j >= 1 ) do begin if ( i > j ) then kk +1; jj div 2 ; end; end;

i=1 n=2k i=2 ... i=n/4

j = n/2, n/4, n/8, ..., n/n 2k-1, 2k-2, ..., 20 k iterations k iterations ... k iterations

k+k+k++k n/4 T(n) = (n/4) * k T(n) = (n/4) * log2n

Complexity = O(nlogn)

Pseudo Code
a) for (i = 1; i <= n/8; i++) for (j = n/2; j<n; j++) { if (i+j <x) x = x - 1; } a) for i1 to n/8 do for jn/2 to n-1 do begin if (i+j <x) then x end;

x-1;

Complexity Calculation
T(n) = (n/8)(n/2)
22

O(n /16) = O(n )


b) i = n/4; while ( i < n) { s = s + 1; i = i + 1; } for (j = 1; j<=n; j++) if (h <x) x = x - 1; b) i n/4; while ( i < n) do begin ss + 1; ii + 1; end for j 1 to n do if (h <x) then x = x - 1;

n/2 = a halfn/4 = a quarter n/4n : from a quarter to reach n. + + = (n). n=12. n/4= 3n=9 (12) = 3 * 12/4 = 3 * 3 = 9

T(n) = (3/4)n + n = (7/4)n O(n) = (7/4)n

It is clear that how fast computers become or how cheap memory gets, efficiency will always remain an

Sequential Search Versus Binary Search important consideration.


1. Sequential Search

Sequential search does an exhaustive search starting from the first element. So, if the list S has n elements the algorithm does at most n operations to decide that an element is in S or is not in S.
1 2 3 4 5 6 7 n

We need at most n comparisons to decide whether x is in S or not Generally Sequential search is used when the data are not sorted. Question: Write a sequential search algorithm to check whether x is an element of S or not.

Binary search technique is used when the data are sorted. It is similar to thumbing back and forth in a phone book. We are searching for an element x into a list S of n elements sorted in non-decreasing order.
1 2 3 4 5 6 7 8 9 32

The algorithm first compares x with the middle item of the array; namely, S[16]. If x = S[16] then x is found we stop. If x < S[16] then x is in the first half of the array S[1..15].

If x > S[16] then x is in the second half of the array S[17..32]. This procedure is repeated until x is found or it is determined that x is not in the array.

Binary Search Algorithm


Problem: Determine whether x is in the sorted array S of n keys (elements). Inputs: positive integer n, sorted (non-decreasing order) array of keys S indexed from 1 to n, a key x. Outputs: location, the location of x in S (0 if x is not in S.)

void BinSearch(int n, array S[1..n], key x, int& location) { int low, high, mid; low = 1; high = n; location= 0; while ( low<=high && location == 0) { mid = (low + high) / 2; if ( x==S[mid] ) location = mid; else if (x<S[mid]) high = mid-1; else low = mid + 1; } }

If S has 32 elements and x is not in S, the number of comparisons that we need to decide that x is not
1 in S is 6. st S[16] 2nd 3rd 4th 5th 6th S[24] S[28] S[30] S[31] S[32] (1 + 32) (17 + 32) (25 + 32) (29 + 32) (31 + 32) (32 + 32) / / / / / / 2 2 2 2 2 2 = = = = = = 16 24 28 30 31 32

log2 32 + 1 = 6 If the size of S is doubled, S = 64, we need only one extra comparison. Because, the first comparison cuts the array in half, resulting in a sub-array of size 32.log2 64 + 1 = 7 In general, each time we double the size of the array we add only one comparison. Therefore, if n is a power of 2 (n=2 ) and x is larger than all the items of S of n elements, the number of comparisons is log2 n + 1

The number of comparisons (NC) done by Sequential Search (SS) and Binary Search (BS) when x is larger than all the array items.

When the array contains around 8 billion items (more than the number of people in the world), Binary Search does only 34 comparisons whereas Sequential Search compares x with all 8 billion items. Even if the computer was capable of completing one pass through the while loop in a nanosecond (one billionth of a second, 10 s), Sequential Search would take 8 seconds to determine that x is not in the array, whereas Binary Search would make that determination almost instantaneously.
-9

This difference would be significant in an on-line application or if we needed to search for many items several times. Complexity: Sequential Search Binary Search O(n) O(log2 n)

Buying a software based on algorithms of O(log2 n) is definitely better than a software based on algorithms with O(n).

You might also like