C program to Insertion Sort

Below is C program to Insertion Sort.

Program:

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
/* Insertion Sort */
#include<stdio.h>
 
int main()
{
 int arr[10],i,j,new;
 printf("Please enter 10 values:\n");
 for(i=0;i<10;i++)
  scanf("%d",&arr[i]);
  
 for(i=1;i<10;i++)
 {
  new=a[i];
  for(j=i-1;j >=0&&new < a[j];j--)
  {
   a[j+1]=a[j];
  }
  a[j+1]=new;
 }
   
 printf("Sorted Array is:\n");
 for(i=0;i<10;i++)
  printf("%d\n",arr[i]);
  
 return 0;
}

No comments:

Post a Comment