Sort (1): Insert sort, sort insert

Source: Internet
Author: User

Sort (1): Insert sort, sort insert

Preface

The data structure and algorithm will soon be crushed into slag, and it is decided to start population.

References: Introduction to Algorithms (MIT open course-Introduction to Algorithms), Data Structures and Algorithms in Python, Data Structures and Algorithms

 

Insertion-sort)

The basic idea of insert sorting: Insert a sorted record to the appropriate position in the subtable that has been sorted in the previous order according to its keyword size

Until all records are inserted.

For example, 8 letters are sorted by insert. Each row corresponds to an outer iteration loop, and each copy in each row corresponds to an inner iteration loop,

The elements in the inserted array are highlighted.

Code Implementation (python ):

1 def insertion_sort (A): 2 for k in range (1, len ()): 3 cur = A [k] # current element to be inserted4 j = k # find correct index j for currect5 while j> 0 and A [J-1]> cur: 6 A [j] = A [J-1] 7 j-= 18 A [j] = cur # cur is now in the rimo-place

Unfinished...

Link recommendations:

MIT open course Introduction to algorithms Study Notes: http://www.cnblogs.com/diliwang/p/3352946.html

Introduction to algorithms Reading Notes: http://www.cnblogs.com/Anker/category/436374.html

 

.........

Note: Please indicate the source for reprinting.

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.