12. Toad Data Structure Advanced 12 The direct insertion method of the order realization
This famous article:"The road is the foot out of The history is written by people, each step of the action in the book to set their own history." -- Gishongchang "
Next look at the implementation of the direct insertion method.
Welcome reprint, Reprint please indicate source: http://blog.csdn.net/notbaron/article/details/47687631
1. Direct Insertion Method
Direct Insert sort (straightinsertion sort)
Each time the first element is taken out of the unordered table, it is inserted into the proper position of the ordered table so that the ordered table remains orderly.
The first two numbers are compared, then the second number is inserted into the ordered table by size, and the second one scans the third data with the first two numbers, inserts the third number into the ordered table by size, and then goes through the n-1 scan to complete the sorting process.
2. Code implementation
The code is relatively simple, the specific view of the source section.
3. Source Code
#include "Stdio.h"
void print (inta[],intn) {
for (intj= 0; j<n; j + +) {
printf ("%d",a[j]);
}
}
void Insertsort (inta[],intn)
{
for (inti= 1; i<n; i++) {
if (a [i]< a [i-1]) { / / if i i-1 element, insert directly. If less, move the ordered table and insert &NBSP;
intj= i-1;
int x= a[i]; // Copy as Sentinel, which stores the elements to be sorted
a [i]= a [i-1]; // move one element &NBSP;
while (x< a[j]) { // Find the insertion position in an ordered table
a [J+1]=a[j];
j--; // element Move back
}
a [J+1]= x; // Insert to correct position
}
//print (a,n); Print the results of each trip sort
}
}
int Main () {
intA[8] = {3,1,5,7,2,4,9,6};
printf ("beforeinsertion sort\n");
Print (a,8);
Insertsort (a,8);
printf ("\nafterinsertion sort\n");
Print (a,8);
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
12. Toad Data Structure Advanced 12 The direct insertion method of the order realization