Haven't written a blog for a long time, today simple summary under the direct insertion sort.
What is a direct insert sort?
inserting a sort directly is to compare an element to an element in the order table and then insert it in the appropriate position so that the entire order table is in an orderly state. the book on the structure of statistics will be used to illustrate the analogy of poker, just like playing poker, when we get a few cards, to be organized into a straight son, we usually do is to put the small card, in front of the big cards. In particular, you can read the book, think about it, or it is easy to understand.
the idea of inserting the order directly : Each time an element to be sorted is inserted into an ordered sequential sequence with the size of its key code.
The direct insert sort has two basic questions:
- How to construct an initial ordered sequence.
- How to find the insertion point in an ordered sequence.
Specific Implementation :
<span style= "Font-family:courier new;font-size:14px;" > #include <iostream> #define MAXSIZE 10using namespace std;struct sqlist{ int r[maxsize+1];//to store the array to sort r[ 0] used as sentinel int length; The length of the sequential table};void Insertsort (sqlist *l) { int J; for (int i=2;i<=l->length;i++) { //element comparison if (L->r[i]<l->r[i-1]) { l->r[0] = l->r[i];/ /assign this element to Sentinel for (j=i-1; l->r[j]>l->r[0];j--) { l->r[j+1] = l->r[j];//move back } l->r[j+1] = l->r[0]; Place the inserted element at the specified position } }}void Print (sqlist *l) {for (int i=1;i<=l->length;i++) { cout<<l- >r[i]<< ""; }} int main () { SqList L = {{0,6,8,3,11,7},5}; Insertsort (&l); Print (&l); return 0;} </span>
Data structure and algorithm--Direct Insert Sort