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