What is an algorithm:Algorithm (algorithm): A computational process that solves the problemTwo characteristics of recursion:
Call itself
End condition
Recursive example:def func (x):ifx==0: Print ("my little carp.", end="') Else: Print ("hold the", end="') func (x-1) Print ("of Me", end="") func (5)Recursive example one: my little carp" "1 1 2 3 5 8 - + theFibonacci sequence with output length n" "#方式一: whilecycle def fibo (num): a=1b=1I=1 whileiNum:print (A,end=" ")
Complexity of time complexity of space
Time complexity is a common method in algorithm analysis. He gave a mathematical method to analyze the efficiency of the algorithm's execution. This paper discusses how to calculate the time
put on N3 above
N4 N4
N3 N3
N2 N2 N2 N2 N2 N2
N1 N1 N1 N1 N1 N1 N1
I finally bought the last one! I found that only 0.5 is needed to stack this book on N3, and 0.55 is needed to stack it on N4! Of course, I will not choose the one that costs too much.
N4 N4 N5
N3 N3
N2 N2 N2 N2 N2 N2
N1 N1 N1 N1 N1 N1 N1
So I bought the book. I bought three, four, three, two, and one. The purchase method is the same as that in the teaching material.
Iv. algorithm time
I. Question
How to sort n numbers requires time complexity O (N) and space complexity O (1)
Ii. Answers
Key: hash table, meaning of 1 in space complexity O (1) (as long as it is a constant)It seems that any knownAlgorithmIf anyone does, all sorting methods: quicksort, shellsort, heapsort, bubblesort, and so on
Title: to 1, 2, ..., an unordered array of N, sorted, requiring a time complexity of O (N), and spatial complexity of O (1).Idea: The problem uses array elements and arrays of the difference between the subscript 1, the Java code is as follows:Importjava.util.Arrays; Public classMain { Public Static voidOnesort (int[] Array) { for(inti = 0; i ) {
Time complexity and spatial complexity of commonly used sorting algorithms
Sorting method
Worst-time analysis
Average Time complexity
Degree of stability
Comp
Time Complexity a simple understanding of time complexity is the number of statements executed. If loops and Recursion exist, ignore simple statements and calculate the number of cycles and Recursion statement executions. For example: [java] int x = 1; // the time
Complexity of Time
N^2 represents the square of N, and select sort is sometimes called direct select sort or simple select sort
Sorting methods
Average Time
Best time
Worst time
Bucket sequencing (unstable)
O (N)
O (N)
O (N)
Complexity of Time:1 Find the basic statement: the most executed statement in the algorithm is the basic statement, usually the most inner loop of many loops.2 Calculate the order of magnitude of the base statement:You only need to calculate the number of executions of the base statement. Ensure that the highest power is correct.Ignore the coefficients of the lower power and the highest power, simplify the
" definition of algorithmic time complexity"At the time of the algorithm analysis, the total number of executions of the statement T (N) is a function of the problem size n, which then analyzes the change of T (n) with N and determines the order of magnitude of T (N). The time comp
It seems that no known algorithm can be implemented. If anyone does, all the sorting methods, such as quicksort, shellsort, heapsort, and bubblesort, can be discarded, what are these algorithms. But in fact, when there is a limit on the number range, there is an algorithm like this. You only need to use an array to record the number of occurrences of each number. Assume that your number ranges from 0 to 65535 and defines an array count [65536] (This space is a constant and has nothing to do with
The relationship between ratings [I + 1] and ratings [I] is as follows:
1. Equal. When the value is equal, ratings [I + 1] has 1 candy.
2. Ratings [I + 1]> ratings [I]. In this case, you need to find the incremental Sequence starting with ratings [I.
3. Ratings [I + 1]
For a random ascending sequence [2 3 4 5 6], the corresponding number of Sweets is [1 2 3 4 x].
For a random descending sequence [6 5 4 3 2], the corresponding number of Sweets is [x 4 3 2 1].
X indicates the number of sweets cor
tmparray to be sorted; The TMP is a temporary array, and the array of arrays is saved;
Note: The count ordering is strict with the INPUT element because the array element value is used as the subscript for the Tmparray array, so if the array element value is 100, then the Tmparray array will apply 101 (including 0, which is Mix-min + 1).
Code Implementation
Operation Result:
Complexity of Time
I. Time Complexity
During algorithm analysis, the total number of statement executions T (n) is a function about the problem scale N. Today, T (n) changes with N and T (n) is determined). The time complexity of the algorithm, that is, the time measurement of the algorithm.
Algorithm efficiency measurement methods: Ex-post statistical methods, ex ante analysis and estimation methods.
The time complexity of the algorithm: in the analysis of the algorithm, the total execution times of the statement T (N) is about the problem scale n function, and then analyze T (n) with N and determine the order of magnitude T (N). The time
"Time Complexity")
The amount of memory space required to run the algorithm. (called "Spatial complexity")
The standard of good algorithm is: In accordance with the requirements of the algorithm itself, the use of the program to write programs run short time, the running process occupies less memory space, yo
Question: except two numbers in an integer array, the other numbers appear twice. Write a program to find the numbers that appear only once. The time complexity is O (n) and the space complexity is O (1 ).
Idea: The question requires that the time complexity be O (N) and the
Estimation Example 3:The three instance operation times were:2n+5 times, n+3 times, 3 timesAs the size of the problem increases, the number of their operands will be more and more different!when judging an algorithm's efficiency, it is often necessary to focus on the highest number of times of the operand, and other times and constants can be ignored. So the "Big O notation" was drawn up:1, the efficiency of the algorithm depends heavily on the number of operations (operation)2, in the judgm
Description:
How to sort n numbers requires time complexity O (N) and space complexity O (1)
Resolution:
Use the Count sorting method to set an int array of 65536 in the range of a [0] ~ A [65535], and the initial value is 0, and then the number of N is traversed. Assume that the number of N is in the array [0... in n-1], the I value ranges from 0 to n-1 and
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.