Algorithm insertion sorting

Source: Internet
Author: User

Algorithm insertion sorting
There is a sorting method for the background. Here we use insertion sorting as an example to analyze the process of generating an algorithm. The Problem description has such an n-number input, and we want the output to be an ordered set. Algorithm Description insert sorting algorithm is an effective algorithm for sorting a small number of elements. The principle of insertion sorting is similar to that of playing cards. When we started to touch the cards, our left hand was empty, and there were cards on the table that we could not see the numbers. Then we put the new cards on the left hand again and again (sorted) in the correct position, compare the new card with the card in the left hand from right to left (or from left to right), and place it in the appropriate position until the card on the table is completed. In this case, we have an ordered set in the desired order in the left hand. The pseudo-code implementation follows the above explanation and uses the pseudo-code representation in the following form: // The array to be sorted is, key is the value to be compared for j = 2-> length [A] do key = A [j] I = j-1 while (I> 0 & A [I]> key) do A [I + 1] = A [I] I = I-1 A [I + 1] = key to confirm the correctness of the algorithm. In general, the loop-free formula is used to help us understand the correctness of the algorithm. It is through the establishment of three properties to ensure the correctness of the algorithm. They are initialization, persistence, and termination. A simple description of the nature of this is: the initial value is set up, the process in the middle is set up, and the process is set up at the end of the process, it can prove that the entire algorithm is no problem. This process is surprisingly similar to mathematical induction. If you want to know more about mathematical induction, you can look at it (http://baike.so.com/doc/6143202.html ). Algorithm analysis refers to the consumption of computer resources by an algorithm, which is generally evaluated in terms of time complexity and space complexity. A good algorithm will consider how to achieve the highest efficiency in the current environment. Of course, ideally, when both time complexity and space complexity are small, the efficiency is the highest. In more cases, we will discard one method to achieve the desired effect, which needs to be determined based on the actual situation. Generally, we do not have a good metric for spatial complexity, but the time complexity can be clearly expressed as a criterion for judgment. The time complexity is generally related to the input size. A simple understanding is that the number of input elements. Time Complexity refers to the total time consumed by each operation. Another important factor is the time complexity of input data. For sorting algorithms, if the input values are sorted in descending order, the two conditions are certainly different. Obviously, the one is the best case, one is the worst case. When designing an algorithm, you should have a certain understanding of the best, worst, and average conditions to better assess whether the algorithm will be used. Insert sorting algorithm practice (JS version) var arr = [,]; // you can generate some complex data var key, j; console. time ('time'); for (var I = 1; I <arr. length; I ++) {key = arr [I]; j = I-1; while (j> = 0 & arr [j]> key) {arr [j + 1] = arr [j]; j --;} arr [j + 1] = key;} console. timeEnd ('time'); console. log (arr );

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.