C + + algorithm

Source: Internet
Author: User

C + + algorithm

Algorithm Concepts

The algorithm is a description of the specific problem solving steps

A finite sequence of instructions represented in a computer

Algorithm is an independent existence of a problem-solving methods and ideas.

For the algorithm, the language is not important, the important thing is thought.

Algorithm and data structure differences

Data structures only statically describe the relationships between the elements

Efficient programs need to design and select algorithms on the basis of data structures

program = data structure + algorithm

Summarize:

Algorithms are designed to solve real-world problems

Data structure is the problem carrier that the algorithm needs to deal with

Data structures and algorithms complement each other

Algorithm features

Input

The algorithm has 0 or more inputs

Output

The algorithm has at least 1 or more outputs

have poor sex

The algorithm automatically ends without an infinite loop after a limited number of steps

Certainty

Each step in the algorithm has a definite meaning and does not appear ambiguity

Feasibility

Every step of the algorithm is feasible.

Measurement of algorithm efficiency

1. post-mortem statistical method

Compare the running processing time of different algorithms for the same set of input data

Defects

The program must be written in order to obtain the running time of the different algorithms

Uptime relies heavily on hardware and environmental factors at run time

The selection of the test data of the algorithm is very difficult

Although the post-mortem method is intuitive, it is difficult to implement and many defects.

2. pre-analysis and estimation

Estimating the efficiency of the algorithm based on statistical method

The main factors affecting the efficiency of the algorithm

Strategies and methods adopted by the algorithm

Input scale of the problem

Code generated by the compiler

Computer Execution Speed

#define_crt_secure_no_warnings#include<iostream>#include<string>//the algorithm is eventually compiled into specific computer instructions//each instruction, run speed fixed on the specific computer//The complexity of the algorithm can be deduced by the specific n steps .LongSUM1 (intN) {    LongRET =0; int* Array = (int*)malloc(n *sizeof(int)); inti =0;  for(i=0; i<n; i++) {Array[i]= i +1; }     for(i=0; i<n; i++) {ret+=Array[i]; }     Free(array); returnret;}LongSUM2 (intN) {    LongRET =0; inti =0;  for(i=1; i<=n; i++) {ret+=i; }    returnret;}LongSUM3 (intN) {    LongRET =0; if(N >0) {ret= (1+ N) * N/2; }    returnret;}voidmytest () {printf ("%d\n", Sum1 ( -)); printf ("%d\n", Sum2 ( -)); printf ("%d\n", SUM3 ( -)); return;}intMain () {mytest (); System ("Pause"); return 0;}

intFuncintA[],intLen) {    inti =0; intj =0; ints =0;  for(i=0; i<len; i++) n { for(j=0; j<len; J + +) n {s+ = I*j;//N*n        }    }    returns;}//N*n

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.

2. Large O notation

Algorithmic efficiency relies heavily on the number of operations (operation)

Focus on the highest number of operations for the first time when judging

Estimation of the number of operations can be used as an estimate of time complexity

O (5) = O (1)

O (2n + 1) = O (2n) = O (n)

O (n2+ n + 1) = O (n2)

O (3n3+1) = O (3n3) = O (n3)

Common time complexity

Relationship

3, the spatial complexity of the algorithm

The spatial complexity of the algorithm is realized by computing the storage space of the algorithm.

S (n) = O (f (n))

where n is the problem scale and F (n) is the function that occupies the storage space when the problem size is n

The large O notation also applies to the spatial complexity of the algorithm

The space complexity is O (1) when the space required for the algorithm execution is constant.

Strategies for Space and time

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.

In the same vein, the complexity of space can be reduced by increasing the complexity of time.

#define_crt_secure_no_warnings#include<stdio.h>#include<stdlib.h>#include<string.h>/*problem: In an array consisting of some numbers in the natural number 1-1000, each number may appear 0 or more times. Design an algorithm to find the most frequently occurring numbers. *///Method 1: Sort, and then find the most frequently occurring number//sort, and then find the most frequently occurring number//Method 2: Cache the intermediate result of the number of occurrences of each number, and find the maximum value in the cached resultvoidSearchintA[],intLen) {    intsp[ +] = {0}; inti =0; intMax =0;  for(i =0; i < Len; i++)    {        intindex = a[i]-1; Sp[index]++; }     for(i =0; I < +; i++)    {        if(Max <Sp[i]) {Max=Sp[i]; }    }     for(i =0; I < +; i++)    {        if(max = =Sp[i]) {printf ("%d\n", i +1); }    }}voidmytest () {intArray[] = {1,1,3,4,5,6,6,6,2,3}; Search (Array,sizeof(array)/sizeof(array[0])); return;}intMain () {mytest (); System ("Pause"); return 0;}

C + + 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.