program for insertion sort in c

C Program to Perform insertion Operation on one dimensional array                                  


 

 Code:

#include <stdio.h>
#include<conio.h>
int main()
{
        int a[50],pos,i,val,n;
        printf("Enter no.of elements in array:");
        scanf("%d",&n);
        printf("Enter %d elements\n",n);
        for(i=0;i<=n-1;i++)
        {
            scanf("%d",&a[i]);
        }
        printf("Enter the location where You wish to insert an element:");
        scanf("%d",&pos);
        printf("Enter a Value:");
        scanf("%d",&val);
        for(i=n-1;i>=pos-1;i--)
        {
            a[i+1]=a[i];
        }
        a[pos-1]=val;
        printf("Resultant array is\n");
        for(i=0;i<=n;i++)
        {
            printf("%d\t\t",a[i]);
        }
        
        
        getch();
    return 0;
}

 

Sample Input and Output

Enter no.of elements in array:4                                                                                                      
Enter 4 elements                                                                                                                     
2                                                                                                                                    
4                                                                                                                                    
1                                                                                                                                    
5       
Enter the location where You wish to insert an element:2                                                                             
Enter a Value:12                                                                                                                     
Resultant array is                                                                                                                   
2               12              4               1               5  

 In this article we are going to see how to write Perform insertion Operation on one dimensional array So Let's start the explanation of above program!  

 In the first line of code,we are including stdio.h header file which will help us to use some of the built-in functions like printf() and scanf() which are defined in this header file.this function will help us to perform some basic operations like Taking user input from user,printing the statement etc.

In the next line of code,we have imported one more header file conio.h header file which provides us getch(),clrscr() and many more function which are defined in conio.h header file but we will be using the getch(),clrscr() only.The main use of getch() function is used to hold the screen during the execution of program if we don't use this statament in the traditional compiler's like Turbo C Compiler then the output will come and go so fast that we wan't be able to see it,so to overcome this we will use getch().

The use of clrscr() function is to clear the output screen of previously executed program.In the next line we have create main function with int return type so we can say that we have to return some value from main function in our case we will return 0.we will define all the code inside the main function itself.

In the first line of main function,we have declared some variable's like  array a of type int and size 50 which we will use to store all the element of an array taken from user,pos variable is for position which  will help us to insert element at that particular position.i will be used as iterative variable which we are going to use for for loop,n variable will be used to store total no.of element of an array taken from user,val variable will be used to take the value which user want to insert at particular position.

In the next line of code,we are printing "Enter no.of elements in array:" statement which will be printed as it is inside the double quote('') and we already known that whatever we put inside the double quote('') will be printed as it is.we have printed this statement to indicate the user that we have to provide some input then only the program will proceed since after the printf() statement we have written one scanf() statement which actually pause the execution unless the input is provided. here we are taking total no.of element of an array and storing the input in n variable,we are using %d to format specifier because n is of int(Integer) data type.

In the next line of code, we have wrote one more print statement  which will help user to understand that how much element we have to provide printf("Enter %d elements\n",n);  here if the value of n is 6 then the statement would be 'Enter 6 elements' from this,user will get to known that that we have to provide 6 elements,to take user input we will be iterating through for loop.

Let's understand iteration by iteration

the sample of n = 3 

1st iteration 

i=0 //(for(i=0;i<n;i++) 

0<3 //Condition satisfied Therefore the control will go inside the block 

scanf("%d",&a[0]) //The user input will be stored at a[0] since the value of i is 0

Increment the value of i

2nd iteration 

i=1 //Incremented the value of i therefore i become 1 from 0.

1<3 //Condition satisfied Therefore the control will go inside the block 

scanf("%d",&a[1]) //The user input will be stored at a[1] since the value of i is 1

Increment the value of i

3rd iteration 

i=2 //Incremented the value of i therefore i become 2 from 1.

2<3 //Condition satisfied Therefore the control will go inside the block 

scanf("%d",&a[2]) //The user input will be stored at a[2] since the value of i is 2

Increment the value of i

4th iteration 

i=3 //Incremented the value of i therefore i become 3 from 2.

3<3 //Condition is not satisfied therefore the control will come outside the loop.







If you any have doubt,suggestion regarding this post or website then feel free to share with us.
 
If you have learned something new from this post then share this post with your family and
friends.
 
Happy Learning :)😎  

 

Previous Next

Post a Comment

Previous Post Next Post