New Android apps UniqueKey

Thursday, 28 August 2014

INSERTION SORT.


 1
 2
 3
 4
 5
 6
 7
 8
 9
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
/* a c program to perform insertion sort  */


#include<stdio.h>
#include<stdlib.h>

main()
{
    int a[50] , i , j , index , n;
 
    printf("enter the number of element in array\n");
    scanf("%d",&n);
 
    printf("enter all the elements of array\n");
 
    for(i=0;i<n;i++)
    {
      scanf("%d",&a[i]);
    }
 
    for(i=1;i<n;i++)
    {
        index = a[i];
  
        for(j=i ; j>0 && a[j-1]>index ; j--)
        {
           a[j] = a[j-1];
        }
       a[j] = index;
     }
 
   printf("sorted array in Ascending Order is \n");
 
   for(i=0;i<n;i++)
   {
      printf("%d\t",a[i]);
   }
}

No comments:

Post a Comment