Classic sorting algorithm-insert sorting insertion sort

Source: Internet
Author: User

Classic sorting algorithm-insert sorting insertion sort
Insert sorting means that each step inserts a data to be sorted to an appropriate position in the sorted data according to its size until all data is inserted.
The insert sorting method is divided into two types: Direct insertion sorting and semi-insertion sorting. Here we only introduce direct insertion sorting, and semi-insertion sorting is left in the "Search" content.
Figure 1 demonstrates the process of directly inserting and sorting the four elements. Three inserts are required: (a), (B), and (c.

The following code is for reference only.

/// <Summary> /// insert sorting /// </Summary> /// <Param name = "unsorted"> </param> static void insertion_sort (INT [] unsorted) {for (INT I = 1; I <unsorted. length; I ++) {If (unsorted [I-1]> unsorted [I]) {int temp = unsorted [I]; Int J = I; while (j> 0 & unsorted [J-1]> temp) {unsorted [J] = unsorted [J-1]; j --;} unsorted [J] = temp ;}} static void main (string [] ARGs) {int [] x = {6, 2, 4, 1, 5, 9 }; insertion_sort (x); foreach (VAR item in X) {If (item> 0) console. writeline (item + ",");} console. readline ();}

Back to main directory [classic Sorting Algorithm] [Collection]

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: 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.