Data structure 1: Data structure and algorithm analysis

Source: Internet
Author: User

Problem leads

Suppose there is a problem: there is a set of n numbers and to determine which of the k largest, we call the choice question, then how is this program written? Most intuitively, there are at least two ways to think:

1, the n number of read into an array, and then through a simple algorithm, such as bubble sort, in descending order to sort the array, the K-position element is the element we need

2. Slightly better, read the k elements into the array and sort them in descending order, then read the next element one by one, and when the new element is read, if it is smaller than the K element in the array, it is omitted, otherwise it is placed in the correct position in the array, and an element in the array is extruded into the array when the algorithm terminates , the element located in the K position is returned as an answer

Both of these algorithms are very simple, but assuming that we have 10 million elements of random files and k=5000000 to simulate will find that two algorithms, although the final can give the correct answer, but in a reasonable time will not end. Therefore, neither of these algorithms can be considered a good algorithm because, from a practical point of view, they cannot process the input data within a reasonable time.

The analysis of data structure and algorithm

One of the most important ideas in many issues is that it is not enough to write a working procedure . If the program is running on a huge data set, then the runtime becomes an important issue, and we'll see in the next article how to estimate how long the program will run for a large number of inputs, especially how to compare the elapsed time of two programs without specific coding. We'll also see ways to radically improve program speed and identify program bottlenecks that will enable us to discover those snippets of code that need our focus on optimization.

So, first, let's look at what data structures and algorithmic analysis are (specifically, the following examples are written in Java code).

Data

The data structure is the way that the computer stores and organizes the data , and it refers to the collection of data elements that have one or more specific relationships with each other. Typically, well-chosen data structures can lead to higher operational or storage efficiencies (which is why we want to study data structures), and data structures are often associated with efficient retrieval algorithms and indexing techniques.

Common data structures are arrays, stacks, queues, lists, trees, hashes, and so on, these data structures will be the focus of the classification of this data structure of the object.

Algorithm analysis

An algorithm is a set of simple instructions that need to be followed to solve a problem that is clearly specified . For a problem, once an algorithm is given and (in some way) determined to be correct, then an important asynchronous is to determine how much of the algorithm will need to inject time or space, and so on the amount of resources. If:

1, a problem to solve the algorithm should take up to a year, then this algorithm is very difficult to use

2, a problem requires a number of GB of memory algorithm, on most of the current machine is also not available

Fundamentals of Mathematics

Whether it is data structure or algorithm analysis, have used a lot of mathematical basis, the following will be the basis of these mathematics briefly summed up:

1. Index

(1) XAXB = Xa+b

(2) XA/XB = Xa-b

(3) (XA) B = Xabsi

(4) XN + XN = 2xn≠x2n

(5) 2N + 2N = 2N + 1

2. Logarithm

(1) XA = b when and only if LOGXB = A

(2) Logab = LOGCB/LOGCA

(3) Logab = Loga + logb,a>0 and b>0

3. Series

(1) ∑2i = 2n+1-1

(2) ∑ai = (an+1-1)/(A-1), if 0<a<1, there is ∑ai≤1/(1-a)

4. Modulo operation

If n is evenly divisible by a and n, then it is said that a is equal to B modulo n, which is recorded as a≡b (mod n). Intuitively, this means that either a or B is removed by N, the remainder is the same, so if there is a≡b (mod N), then:

(1) A + c≡b + C (mod N)

(2) AD≡BD (mod N)

Complexity of Time

In computer science, the time complexity of an algorithm is a function that quantitatively describes the time it takes to run the algorithm. This is a function of the length of the string representing the input value of the algorithm, time complexity commonly used large o notation, does not include the function of the lower order and the first coefficient, when using this method, the time complexity can be called progressive, he examines when the input value is approaching infinity when the size of the case.

So first look at a simple example, here is a simple program fragment for calculating ΣI3:

1  Public Static voidMain (string[] args)2 {3System.out.println (SUM (5));4 }5 6  Public Static intSumintN)7 {8     intpartialsum;9     TenPartialsum = 0; One      for(inti = 0; I <= N; i++) APartialsum + = i * I *i; -      -     returnpartialsum; the}

The analysis of this program fragment is simple:

1, the declaration does not enter the time

2, the 10th row and all 14 lines each occupy one time unit

3, the 12th line each execution occupies 4 time unit (two multiplication, one addition and one assignment), and executes n times occupies 4N time unit

4. Line 11th in initialization I, Test i≤n and the self-increment of I imply overhead, all of which are initialized with 1 time units, all of which are n+1 time units, all of which are added to n time units, a total of 2n+2 time units

Ignoring the overhead of calling methods and return values, the resulting total is 6n+4 units of time, and the initial definition does not include the lower order and first coefficients of the function , so we say that the time complexity of the method is O (N). Then, by the way, we come to a number of general rules:

Rule one----for loop

The run time of a For loop is at most the number of times the run time of those statements (including tests) in the For loop is multiplied by iterations, so if a for loop iterates N times, its time complexity should be O (N)

Rule two----nested for loop

Analyzing these loops from the inside out, the total run time of a statement inside a set of nested loops is the product of the run time of the statement multiplied by the size of all the for loops in the group, so if you have the following code:

1  Public Static intMutlisum (intN)2 {3     intK = 0;4      for(inti = 0; I < n; i++)5     {6          for(intj = 0; J < N; J + +)7         {8k++;9         }Ten     } One      A     returnK; -}

The time complexity should be O (N2)

Rule three----sequential statements

Sum the elapsed time of each statement, for example with the following code:

1  Public Static intSumintN)2 {3     intK = 0;4     5      for(inti = 0; I < n; i++)6k++;7      for(inti = 0; I < n; i++)8     {9          for(intj = 0; J < N; J + +)Ten         { Onek++; A         } -     } -  the     returnK; -}

The time complexity of the first for loop is N, the time complexity of the second nested for loop is N2, and the time complexity of the sum method is O (N2) combined.

The common time complexity and time efficiency relationship has the following rules of thumb:

O (1) < O (log2n) < O (n) < O (n * log2n) < O (N2) < O (N3) < 2n < 3n < n!

As for each time complexity corresponding to which data structure and algorithm, the following will be said, from the above experience rule: the first four algorithm efficiency is higher, the middle two is passable, the latter three are poor (as long as n is larger, the algorithm will not move).

Data structure 1: Data structure and algorithm analysis

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.