Sorting algorithm Big Collection _ Insert class--Direct insert Sort

Source: Internet
Author: User

The feature of this series of blogs is--give the results of each order


Originally thought to write a good process, get a picture of what, but think the online analysis too much, are better. "Introduction to the algorithm" above that analogy, than do playing cards inserted new cards, it is more image. So these blogs are even a summary of their own.


#include <stdio.h> void insertsort (int *m, int n) {    int i,j,temp;    for (i=1;i<n;i++)    {        temp=m[i];        j=i-1;        while (j>=0 && temp<m[j])        {            m[j+1]=m[j];            j--;        }        M[j+1]=temp;                for (int t=0;t<10;t++)            printf ("%d", m[t]);        printf ("\ n");}     } int main () {    int i;     int a[10]={2,8,5,7,4,3,1,9,6,10};     for (i=0;i<10;i++)        printf ("%d", A[i]);     printf ("\ n" \ n ");     Insertsort (a,10);     for (i=0;i<10;i++)        printf ("%d", A[i]);     printf ("\ n");     return 0;}




Wait.... It's not OVER!!!

The last 3 lines are in order, but they still don't stop.


Improve the sort algorithm


void Insertsort (int *m, int n) {    int i,j,temp;    BOOL Flag;    for (i=1;i<n;i++)    {        temp=m[i];        j=i-1;        Flag=false;        while (j>=0 && temp<m[j])        {            flag=true;            M[J+1]=M[J];            j--;        }        M[j+1]=temp;                for (int t=0;t<10;t++)            printf ("%d", m[t]);        printf ("\ n");        if (flag) break            ;    }}





The result is wrong, since the direct insertion of the sorting process, there may be one or two trips and no value exchanged. This is different from bubbling sort!


Therefore, to optimize the algorithm, it is necessary to meet the actual situation for analysis.


Sorting algorithm Big Collection _ Insert class--Direct insert Sort

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.