17 algorithm complexity

problem

Explain algorithm complexity

answer

  • Algorithms generally include the following metrics: accuracy, readability, robustness, good time and space efficiency, time and space efficiency which is a measure of the complexity of the algorithm.
  • Efficiency of the algorithm is generally determined by the following factors:
    (1) algorithm which strategy adopted sorting algorithm to quickly sort it faster than bubble sort
    scale (2) the problem, look for prime numbers less than 10 is clearly faster than the find prime numbers less than 1000
    ( 3) written in a program language, assembly language is clearly higher than the efficiency java
    quality machine code (4) generated by the compiler, the faster the machine code execution higher mass
    velocity (5) machine executing instructions, the execution is clearly faster I7 on I5.
    Consider the above factors, of course, in the case of other factors constant go on thinking about.
  • Including the complexity of the algorithm time complexity and space complexity. Because java garbage collection mechanism, if not in the loop repeatedly stated variables can not be released, memory is necessarily stable. So, we are more concerned about the time complexity, ie CPU time, CPU time and can be converted into the number of times the statement is executed. Therefore, we analyze the time complexity, only to find out the relationship with a key problem size n number of executions code. Consider the following code:
private static void bubble(int[] n)
	{
		for (int i = 0; i < n.length - 1; i++)
		{
			for (int j = 0; j < n.length - i - 1; j++)
			{
				if(n[j] > n[j + 1])
				{
					int temp = n[j];
					n[j] = n[j + 1];
					n[j + 1] = temp;
				}
			}
		}
	}

This code is the code key exchange logic. The best case is n = {1,2,3,4,5,6,7,8,9,10}; worst case is n = {10,9,8,7,6,5,4 , 3,2,1}. For the former, the number of executions of the key code is 0; for the latter, the key code is executed the number of times 90, i.e., 10 * (10-1), further converted into n * (n-1), and then converted to n squared -n, the square is clearly larger than the effects of n, so take big small homes, this time complexity of bubble sort is n squared.

Published 358 original articles · won praise 0 · Views 2748

Guess you like

Origin blog.csdn.net/langli204910/article/details/105228250