C # insert sorting

Source: Internet
Author: User

Algorithm Description

In general, insert sorting is implemented on the array using in-place. The specific algorithm is described as follows:
 

 

1. Starting from the first element, this element can be considered to have been sorted
 

 

2. Retrieve the next element and scan the sorted element sequence from the back to the front.
 

 

3. If the element (sorted) is greater than the new element, move the element to the next position.
 

 

4. Repeat Step 3 until you find the position where the sorted elements are smaller than or equal to the new elements.
 

 

5. Insert new elements to this position
 

 

6. Repeat Step 2.

The Code is as follows: Please reply promptly if any BUG exists. Thank you.

Code 1 using System. Text;
2
3 protected void Page_Load (object sender, EventArgs e)
4 {
5 InsertSort ();
6}
7
8 private void InsertSort ()
9 {
10 int [] inputIntArray = new int [8] {8, 7, 6, 5, 4, 3, 2, 1 };
11 for (int I = 1; I <inputIntArray. Length; I ++)
12 {
13 // foreach the int array: index of array is 0-7
14 if (inputIntArray [I] <inputIntArray [I-1])
15 {// if the last one is bigger than the privious
16 int temp = inputIntArray [I]; // stored the number of the last one
17 int j = 0;
18 for (j = I-1; j> = 0 & temp <inputIntArray

Related Article

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.