Open Class, Introduction to algorithms (for personal use)

Source: Internet
Author: User

I. algorithms are the fundamental elements of software, just as currency can be used to buy commodities.
Algorithms can be improved in other aspects of the software.

Algorithm learning should begin with analyzing existing algorithms,

Ii. Sorting is the most traditional basic algorithm in the algorithm

2.1 Insert sorting

A [J]
Moves the J position element forward to determine the insert position, so that the first half of the array is ordered.
The value of J starts from the second element until the last one.

For example, a [,]
First cycle: A [2, 8,...]
Second time: A [2, 4, 8,...]
...

Then we will make calculation and analysis,

2.2. Time Complexity
The first factor comes from the input: (if it is already ordered, there will be little work to be done (Reverse Order is the most troublesome case)
The size of the input array.
Maximum evaluation time.

2.3 case classification Discussion

Worst case
Average
Best case
Discuss assumptions about Statistical Distribution

Evaluation condition, relative computing capability (not all machines can run algorithms well)
Bug idea Overview

Progressive analysis: regardless of the computing power of a single machine, the relational functions of computing power and algorithm efficiency (increase and decrease correlation)

Progressive symbol (theta): removes the lower order and constant factor.

For example, 3n ^ 3 + 90n ^ 2-5n + 6046 = theta (N ^ 3) (the third-order growth rate of N)
The progressive symbol is not related to the computing capability. (Different computing capabilities are only constant factors in the formula)

In actual design and analysis, it is necessary to combine computing power, and low-speed algorithms are not necessarily useless.

 

How can we analyze pseudocode and obtain a formula?
1. How many times have you actually accessed a variable? -- The cycle is the sum symbol, and the computation in the loop is analyzed and expressed using the progressive symbol.
After the formula in the example is written, it is found that the formula is an arithmetic series, so the time complexity is n ^ 2.

 

3.1 Merge Sorting

Divide the original array into two subsets, recursively use Merge Sorting in the two subsets, and divide them into two subsets again .... Until the element

The number is 1.

Then, for the sorted subset (the number of elements in the first two subsets is 1), the minimum number is compared, and the small value is placed into the new number.

Group.

Time Complexity Analysis: T (n) = {0 (1), when n = 1 ;}
{2 T (n/2), when n> 1;) the instructor uses an intuitive recursive tree.

Where 0 (1) is progressive, that is, the lower order is removed, and the formula of the constant factor
2 T (n/2) means two times the amount of time that is half the number of elements in the array.

 

The height of the 2 branch recursive tree is lg (N), because the height is 1 starting from N, the height is 2 is n/2, and then n/4 ....
The final number of leaf nodes is of course N (number of array elements), or 2 Branch * tree height 2 * lgn = n.

Since the computing time complexity of each layer is CN, all of them are Cn * lgn, so t (n) = O (N * lgn)

 

 

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.