C LAB‎ > ‎(Sem2) Data Structures‎ > ‎

Q7. WAP to perform a Insert sort.

posted Jan 7, 2011, 11:53 PM by Neil Mathew   [ updated Jan 8, 2011, 8:46 PM ]

SOURCE CODE:

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
39
40
41
#include<stdio.h>
 
void Isort(int *a,int n)
{
int i,j,temp;
 
a[0]=-99;
 
for(i=2; i<n+1; i++)
{
 
temp=a[i];

for(j=i-1; temp < a[j]; j--)
{
a[j+1]=a[j];
}
a[j+1]=temp;
 
}
 
}
 
main()
{
 
int a[50],n,i;
printf("Enter size of your array: ");
scanf("%d",&n);
 
printf("Enter elements of your array: ");
for(i=1; i<n+1; i++)
scanf("%d",&a[i]);
 
Isort(a,n);
 
printf("\nElements: \n");
for(i=1; i<n+1; i++)
printf("%d ",a[i]);
 
}

OUTPUT:


Enter size of your array: 5
Enter elements of your array: 2 8 4 6 5
Elements: 2 4 5 6 8
Comments