Data structure algorithm

Source: Internet
Author: User

#include <iostream>using namespacestd;/*Algorithm algorithm concept algorithm is the description of specific problem solving steps in the computer, the finite sequence algorithm represented by the instruction is an independent existence of a problem-solving method and idea. For the algorithm, the language is not important, the important thing is thought. Algorithms and data structures that differentiate data structures are just static descriptions of the relationships between the elements of a highly efficient program that needs to be designed and selected on the basis of the structure of the algorithm = data structure + algorithm summary: The algorithm is designed to solve the actual problem data structure is the algorithm needs to deal with the problem of vector data structure and algorithm complement each other algorithm features 1. The input algorithm has 0 or more inputs 2. The output algorithm has at least 1 or more output 3. A poor algorithm automatically ends after a finite number of steps without an infinite loop of 4. Each step in the deterministic algorithm has a definite meaning and does not appear ambiguity 5. The feasibility of each step of the algorithm efficiency of the measurement of the efficiency of the main factor 1. algorithm Strategies and methods adopted 2. The input scale of the problem 3. The compiler produces code 4. The computer performs a large O-notation algorithm, which relies heavily on the number of operations (operation) to determine the maximum number of operations that are first concerned with the number of actions, the estimate can be used as a time-complexity estimate on a single computer, The time at which the CPU performs each operation is determined by the following procedure*///Complexity of time 2n+4 space 4n+8LongSUM1 (intN) {    LongRET =0;//Operation 1 times 4 bytes    int* Array = (int*)malloc(n *sizeof(int));//Operation 1 times 4*n bytes    inti =0;//Operation 1 times 4 bytes     for(i =0; i<n; i++)//Operation N times in the CPU, no memory consumption{Array[i]= i +1; }     for(i =0; i<n; i++)//Operation N times in the CPU, no memory consumption{ret+=Array[i]; }     Free(array);//Operation 1 times does not consume memory    returnret;}//Complexity of time n+2 space complexity 8LongSUM2 (intN) {    LongRET =0;//Operation 1 times 4 bytes    inti =0;//Operation 1 times 4 bytes     for(i =1; I <= N; i++)//Operation N Times{ret+=i; }    returnret;}//Complexity of Time 2 space complexity 4LongSUM3 (intN) {    LongRET =0;//Operation 1 times 4 bytes    if(N >0)//The if statement executes only one branch{ret= (1+ N) * N/2;//Operation 1 Times    }    returnret;}/*Note 1: When judging the efficiency of an algorithm, it is often only necessary to pay attention to the highest number of operations, other minor items and constant items can be ignored. Note 2: In the absence of special instructions, the time complexity of the algorithm we are analyzing refers to the worst time complexity.  For Sum1 ()--2n+4, the time complexity of the SUNM1 is related to the N value, the greater the complexity of the N value is written in the large O notation O (2n+4)-->o (n) for sum2 ()--n+2, the time complexity of sunm2 is related to the N value, the greater the N value the greater the complexity  Writing O (n+2)-->o (n) with the large O notation for sum2 ()--2 shows that the time complexity of SUNM2 is independent of the N value and is a constant value written in O (2)-->o (1) Spatial complexity for SUM1 ()--4n+8 It is indicated that the time complexity of SUNM1 is related to n value, the larger the N value is the greater the complexity is written in large O notation O (4n+8)-->o (n) for sum2 ()--8 The time complexity of sunm2 is not related to the N value, is a constant value written O (8)-->o with large O notation (1 For sum2 ()--4, the time complexity of the sunm2 is independent of the n value, which is a constant value in the large O notation for the O (2)-->o (1) Space and time strategy in most cases, the time it takes to execute the algorithm is more interesting if necessary, You can reduce the complexity of time by increasing the complexity of the space, or you can reduce the complexity of space by increasing the complexity of time. */intMain () {printf ("%d\n", Sum1 ( -)); printf ("%d\n", Sum2 ( -)); printf ("%d\n", SUM3 ( -)); return 0;}

Data structure algorithm

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.