Data structure _ Direct insertion sorting method

Source: Internet
Author: User

See this blog for details: http://www.cnblogs.com/skywang12345/p/3596881.html

Introduction to direct insertion sorting

The basic idea of a direct insert sort (straight insertion sort) is to look at the N-sorted elements as an ordered table and an unordered table. The ordered table contains only 1 elements at the beginning, and the unordered table contains n-1 elements, each time the first element is taken out of the unordered table, it is inserted into the appropriate position in the ordered table, the order is made into a new ordered table, and the sorting process is repeated n-1 times.

Complete code for subset completion:

#include"stdio.h"/** Direct Insert Sort * 2015-08-10:may * parameter Description: * A--array to be sorted * N--The length of the array*/voidInsert_sort (intA[],intN) {    intI, j, K;  for(i=1; i<n;i++)    {         for(j=i-1; j>=0; j--)        {            if(a[i]>A[j]) {               Break;//Find a location, find a place larger than a certain location and stop .            }        }        if(j!=i-1)//The position is found, and the back of the position is started, and if it happens, it doesn't move .        {            intTemp=a[i];//the first to be inserted will be overwritten.             for(k=i-1; k>j;k--) {a[k+1]=A[K];//all move backwards .} a[j+1]=temp;//insert in;        }    }}voidMainvoid){    inta[]={5,8,3,1, A,1}; Insert_sort (A,6);  for(intk=0;k<6; k++) {printf ("%d", A[k]); }}

Data structure _ Direct insertion sorting method

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.