The time complexity of a program or algorithm - Algorithms - Notes

One: Concepts and classification

Concept:
time efficiency of a program or algorithm, also known as the time complexity, the complexity of the
number of execution statistics rank computational complexity that the largest number of fixed operations.

Category: bad average complexity and complexity

Note: If the complexity and function of multiple of n, n only concerned with the growth of the fastest growth of the function
| Name | complexity |
| - | - |
| constant complexity | o (1) |
| logarithmic complexity | O (log (n)) |
| linear complexity | O (n) |
| polynomial complexity | O (k-th power of n) |
| exponential complexity | o (a power of n ) |
| factorial complexity | o (n) |!
| insertion sort, selection sort, bubble sort | o (n side) |
| Quick Sort | o (n * log (the n-)) |
| binary search | o ( log (n)) |

II: calculated on complexity

problem

A thought number between 1-10000, B to guess, you can ask questions, A can only be answered
yes or no.
How to guess the number of questions asked at least?

Non-binary

Is 1? 2 is it? . . . . That is 999? To ask the average 500 times

dichotomy

Greater than 500? Greater than 750? Greater than 625? Every half to narrow the scope of speculation last requires only 10 times

Binary search function

A write function BinarySeach, lookup element contained in the p size elements, small to large a int array if they are found the element index is returned, if not found, returns -1. Complexity requirements o (log (n ))

BinarySearch

int BinarySearch(int a[],int size,int p)
{
 int L=0;//查找区间的左端点
 int  R=size-1;//查找区间的右端点
 while(l<R){//如果查找区间不为空就继续查找
  int mid=L+(R-L)/2;//取查找区间正中元素的下标
  if (p==a[mid])
   return mid;
  else if(p>a[mid])
   L=mid+1;//设置新的查找区间的左端点
  else 
   R=mid-1;//设置新的查找区间的右端点
}
 return -1;
}

Write a function LowerBound, look for the small integer ratio of elements in the expression to contain elements of size, from small to large in the int array a.

int LowerBound(int a[],int size,int p)//复杂度o(log(n))
{
 int L=0;//查找区间的左端点
 int R=size-1;//查找区间的右端点
 int lastPos=-1;//到目前为止找到的最优解
 while(L<R){//如果查找区间不为空就继续查找
  int mid=L+(R-L)/2;//取查找区间中元素的下表
  if(a[mid]>=p)
   R=mid-1;
  else{
   lastPos=mid;
   L=mid+1;
  }
 }
 return lastPos;
}

note

Note: int mid = (L + R ) / 2; // Find subscripts take the middle section elements
to prevent the (L + R) is too large overflow int mid = L + (RL) / 2;

Three: the dichotomy seeking roots of the equation

problem:

Seeking below a root of an equation: f (x) = x ^ 3-5x ^ 2 + 10x-80 = 0;
calculated root position a, the required | f (a) | <= 10 ^ -6;
Solution : demand for f (x) conduction equation f (x) by a known quadratic formula for finding roots of
the derivative function 0 = no solution, so f (x) to 0. the derivative function rival so f (x) monotonously increases
f (x) in the [0.100] has a root, it is considered that dichotomy.

Seeking roots of the equation binary method

double EPS=1e-6;
double f(double x){return x*x*x-5*x*x+10*x-80;}
int main(){
 double root,x1=0,x2=100,y;
 root=x1+(x2-x1)/2;
 int triedTimes=1;//记录一共尝试多少次
 y=f(root);
 while(fabs(y)>EPs){
  if(y>0) x2=root;
  else x1=root;
  root=x1+(x2-x1)/2;
  y=f(root);
  triedTimes++;
 }
 printf("%0.8f\n",root);
 printf("%d",triedTimes);
 return 0;
}

Four: Case: complexity and use an algorithm

problem

Input n (n <= 100000) integer, to find the two numbers in which they sum of
an integer m (assuming solvability affirmative). All questions can be expressed as an integer int

A Solution

A double loop, enumerated All load method, the complexity is 0 (n2) of.

for(int i=0;i<n-1;++i)
  for(int j=i+1;j<n;++j)
   if(a[i]+a[j]==m)
    break;

100 000 * 100000 = 10 billion, committed or participated in programs on various OJ
design competition, this complexity will certainly timeout!

Solution two

1) sort the array, the complexity of O (n- log (n-))
2) For each element a [i] array, in an array of binary search ma [i], see if we can
find. Complexity log (n), but also to find the worst n-2 times, so look for the complexity of this part is also O (n-
log (n-))
the complexity of such a solution is o (n * log (n) )

Solution three

1) sort the array, the complexity of O (n- log (n-))
2) Find the time, the initial value is provided two variables i and j, i is 0, j is the initial value n-1 to see a [i] + a [j], if m is greater than, let j minus 1, m is less than let i is incremented by one until a [i] + a [j ] = m;
the complexity of such a solution is O (n-
log ( n))

problem

John farmer built a long stall, which comprises N (2 <= N <= 100000)
the location of these compartments cubicle is x0, x1 ..... xN1 (0 = <xi <1000000000, are integers different)
John of C (2 <= C <= N) assigned to a compartment per head of cattle, beef hope point far enough away from each other, interfering with each province, how to make any of the two cows the minimum distance as large as possible, the number of the largest minimum distance is it.

A Solution

The first compartment after ordering to get the coordinates x0 .... xN-1;
from 1000000000 / C 1 to try this "the biggest closest distance D" in turn, the first viable answer is found.

Try Method:
1) on a first cow X0
2) If the first cow k on xi, xi + 1 is found to xN-1 is located in the first [xi + D, 1000000000] xj in
the k + 1 head of cattle on xj. in no such xj, then D = D-1) to try
if all the cattle can be put down, that is, the answer is D

Complexity: 10000000000 / C * N, i.e., timeout 1000000000
Solution two:

After the first compartment coordinates x0 .... xN-1 give ordering
in [L, R] dichotomy try "Maximum closest distance" D = (L + R) / 2, (L, R is the initial value [ 1,1000000 / C])
If D is feasible, remember that D, then the new [L + R] continue to try (L = D + 1)
if D is not possible, the new [L, R] in continue to try (R = D-1)

Complexity log (1000000000 / C) * N

Guess you like

Origin www.cnblogs.com/available/p/12529429.html