Insert sort (C language)

Source: Internet
Author: User

Enter a number to insert into the sorted queue

First: Define an array of integers that are already lined up

Such as:

      int arry[7]={2,3,5,11,15,17};

or enter an array of integers, and then sort (bubble, select All)

Let's use the bubble Sort method:

             #include <stdio.h>             int main ()             {              int i,j,min=0, num[7];              for (i=0;i<6;i++)              {         printf ("Please enter number%d", i+1);           scanf ("%d", &num[i]);               }  for (j=0;j<6;j++)  {for   (i=0;i<6;i++)  {   if (num[i]>num[i+1])     {      min=num[i+1];      Num[i+1]=num[i];      Num[i]=min;  }}  }  for (i=0;i<6;i++)  {  printf ("   %d", num[i]);  }      
return 0; }

Second: Enter a number NUM1;

Code:

      int num1;      printf ("Please count in one is number:");      scanf ("%d", &num1);

Third: Find the location where the input number was inserted (find subscript index)

In three different situations

①, NUM1 is greater than the previous number, less than the next number

②, num1 less than the smallest number

③, Num1 greater than the maximum number

   int i=0;   int index=0;   for (i;i<6;i++)//Find index to insert the subscript position   {     if (num1>=num[i]&&num1<=num[i+1])        {        index=i+ 1;        break;        }     if (Num1<num[0]) {index=0; break;  }     if (Num1>num[5]) {index=6; break;  }   }

IV: After locating the subscript (where it is inserted), the number from the inserted position to the end of the array, followed by a value equal to the previous one (implementing the move assignment process)

   for (i=6;i>index;i--)//move   {   num[i]=num[i-1];   }   num[index]=num1;//Find subscript Assignment

V: output Array num

   for (i=0;i<7;i++)   {   printf ("%d,", Num[i]);   }

The overall code is as follows:

#include <stdio.h>int main (void) {    int num[7]={2,3,5,11,15,17},num1;   printf ("Please count in one is number:");   scanf ("%d", &num1);   int i=0;   int index=0;   for (i;i<6;i++)//Find index to insert the subscript position   {     if (num1>=num[i]&&num1<=num[i+1])        {        index=i+1;        break;        }     if (Num1<num[0]) {index=0; break;  }     if (Num1>num[5]) {index=6; break;  }   }   for (i=6;i>index;i--)//move   {   num[i]=num[i-1];   }   num[index]=num1;//Find subscript Assignment   for (i=0;i<7;i++)   {   printf ("%d,", Num[i]);}   }

  

  

  

 

Insert sort (C language)

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.