Array--Insert Sort method

Source: Internet
Author: User

1. What is the insertion sort:

Divides the column to be sorted into two parts: ordered data and the element to be inserted.

The basic operation of inserting a sort is to insert the data into a set of ordered data, compare the number to be inserted with each data, and when it finds the first element that is larger than the number of inserts, the element is inserted.

1 namespaceConsoleApplication12 {3     class Program4     {5         Static voidBubble_sort (int[] unsorted)6          {7             //this layer for loop, determines the receive variable (last one does not participate)8               for(inti =1; I < unsorted. Length; i++)9              {Ten                  intInsert=unsorted[i];//declare a variable that holds the inserted value One                  intj=0;//declare a variable that records the subscript (you can get the starting point of a bit backward to the subscript element) A                   while(J < i && insert >Unsorted[j]) -                  { -J + +; the                  } -                  //use a For loop for a post-move operation -                   for(; J < i; i--) -                  {  +unsorted[i]=unsorted[i-1]; -                  } +                  //to insert an interchange AUNSORTED[J] =Insert; at              } -          } -   -          Static voidMain (string[] args) -          { -              int[] x = {6,2,4,1,5,9,3,7};//declares an array of integers and assigns values in              //array before sorting -Console.Write ("before sorting:"); to              foreach(varIteminchx) +              { -Console.Write (item+" "); the               } *Bubble_sort (x);//call the bubbling Sort method $              //array after sortingPanax NotoginsengConsole.Write ("\ n after sorting:"); -              foreach(varIteminchx) the              { +Console.Write (item+" "); A              } the console.readline (); +          } -     } $}

Array--insert Sort method

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.