12. Toad Data Structure Advanced 12 The direct insertion method of the order realization

Source: Internet
Author: User

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

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.