[Algorithm learning notes] basic algorithm knowledge

Source: Internet
Author: User

[Algorithm learning notes] basic algorithm knowledge
The five elements of basic knowledge algorithms are poor: algorithms must be completed within a limited number of steps. Certainty: each step of the algorithm must have a definite definition. Input and Output Feasibility: each step of the algorithm must be able to be divided into basic executable operations, and each step must be able to complete Loops within a limited time.

Loops in a loop can help us understand the correctness of the algorithm. To prove the correctness of the algorithm, we must prove the three properties of the non-variant loop:
1. Initialization: the non-variable loop is correct before the loop starts.
2. Persistence: the non-changing cycle is correct before each iteration of the loop starts.
3. Termination: at the end of the loop, a non-variant nature is provided to help determine whether the algorithm is correct.

Insert Sort Analysis

The following is a simple c ++ Implementation of insert sorting.

Void insert_sort (std: vector
  
   
& Vec) {for (int I = 1; I <vec. size (); I ++) {int key = vec [I]; // insert vec [I] to the sorted vec [0... in I-1] int j = I; while (j> 0) {if (key> = vec [j-1]) break; vec [j] = vec [j-1]; -- j;} vec [j] = key ;}}
  

Here there are two non-variant loops. One is the I <vec. size () of the outer loop. We can prove the three properties of the non-variant loop:
1. Initialization: before the first iteration starts, I = 1, because vec has no more than one element (otherwise there will be no sort), it is obviously true.
2. Persistence: Before the loop ends, I is obviously smaller than vec. size. Www.bkjia.com
3. Termination: After all elements are sorted, I = vec. size ()-1, And I ++ is executed, the cycle ends, indicating that the order has been sorted.

The second loop is the internal while loop. We can also prove the three properties of the non-variant loop:
1. Initialization: At the beginning, I = j = 1, so j> 0 is obviously true, if the key <vec [J-1], it enters the loop. Unchanged j> 0 & key <vec [J-1] was established.
2. Hold: the key <vec [J-1] Must be true if it is not ranked first in vec (j = 0) before it is found where the key should be inserted.
3. Stop: After the cycle ends, the cycle is not changed and j points to the position where the key should be inserted.

By looking at the two loops, we can see that the entire algorithm is correct.

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.