Direct Insert Sort:
This sort of sorting is very good to understand, the real example is that we fight the landlord, when we caught a mess of cards, we have to according to size comb poker, 30 seconds later,
Poker comb finished, 4 3, 5 s, wow ... Remember how we combed it at the time.
The left one card is 3, the second card is 5, the third card is 3, hurriedly inserted into the back of the first card, the fourth card is 3, exultation, hurriedly inserted behind the second one,
The fifth card is 3, ecstasy, Haha, a cannon is produced.
How, the life everywhere is the algorithm, has already melted into our life and the blood.
The following illustration shows:
Look at this picture. I don't know if you can understand, in the insert sort, the array is divided into two types, ordered array blocks and unordered array blocks,
Yes, the first time. Extracts a number of 20 as an ordered array block from the unordered array block.
The second time from the "unordered array block" to extract a number 60 ordered into the "ordered array of blocks", that is, 20,60.
The third time. Similarly, the difference is that 10 is smaller than the ordered array, so the 20,60 position is shifted back, freeing up a position for 10 to insert.
Then you can insert all of them in this pattern.
using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Namespace Insertsort {public class program {static void Main (string[] args)
{list<int> List = new List<int> () {3, 1, 2, 9, 7, 8, 6}; Console.WriteLine ("Before:" + string.)
Join (",", list));
Insertsort (list); Console.WriteLine ("After Sort:" + string.)
Join (",", list)); static void Insertsort (List<int> List) {//No sequence for (int i = 1; i < list.) Count;
i++) {var temp = list[i];
Int J;
Ordered sequence for (j = i-1 J >= 0 && temp < list[j]; j--) {
List[j + 1] = List[j];
} List[j + 1] = temp; }
}
}
}