Saturday, 28 January 2017

Insertion sort in C program [Insertion Algorithm code] ~ mkniit ~ gniitsolution

Insertion sort in C programming language using arrays. This is to sort the given input numbers in ascending order. It is a simple sorting algorithm that generates the final output. The sample source code of this insertion algorithmis given below,

NSERTION SORT IN C

Insertion sort in C program is used to sort the array value in any order big too small or vice-versa by using statement. For example you have entered an array value as 67231 means, it checks the first and second value and compare with the second value. If it is smaller than 2nd, it moves to first. The same repeat operations will happen for all values in the array. Finally it will display the insertion sort value as 12367.

Insertion Algorithm code:

Q: Write a c program for insertion sorting algorithm using arrays concept.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* Tittle : Insertion sort Program
© http://mkniit.blogspot.com - Karthikh Venkat
*/
//Header Files
  #include<stdio.h>
  #include<conio.h>
  #define MAX 10
  //Function to specify the variable
  void insertionsort(int input[]);
  main()
    {
    //Program variables
      int data[MAX];
      int i;
      clrscr(); //Function to clear previous output
      printf("enter the input values for sorting \n"); //Display function
      for (i=0;i<MAX;i++) //Looping statement
    scanf("%d",&data[i]); //Getting input function
      insertionsort(data);
      printf("sorted data \n");
      for(i=0;i<MAX;i++)
    printf("data[%d]=%d \n",i+1,data[i]);
      getch();
    }
 
  void insertionsort(int input[MAX])
    {
      int i,j,key;
      for(j=1;j<MAX;j++)
    {
      key=input[j];
      i=j-1;
      while(i>=0 && input[i]<key) //Unconditional statement
        {
         input[i+1]=input[i];
         i--;
        }
      input[i+1]=key;
    }
   }

1 comment:

  1. awesome blog and awesome post, c language lover should visit here: kidsfront.com

    ReplyDelete