-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathINSERT~1.C
More file actions
69 lines (63 loc) · 1.3 KB
/
INSERT~1.C
File metadata and controls
69 lines (63 loc) · 1.3 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<stdio.h>
#include<conio.h>
void readarray(int a[50],int n)
{
int i;
printf("\nEnter elements of an array.....\n");
for(i=0;i<n;i++)
{
printf("enter value a[%d] = ",i);
scanf("%d",&a[i]);
}
}
void printarray(int a[50],int n)
{
int i;
for(i=0;i<n;i++)
printf("a[%d] = %d\n",i,a[i]);
printf("\n");
}
void insertionsort(int a[50],int n)
{
int i,j,key;
for(i=1;i<n;i++)
{
key = a[i];
for(j = i-1; (j >= 0)&& (a[j] > key); j--)
{
a[j+1] = a[j];
}
a[j+1] = key;
}
}
void main()
{
int x[50],n;
clrscr();
printf(" * * * * * INSERTION SORT * * * * * \n\n");
printf("Enter no. of elements to be sorted : ");
scanf("%d",&n);
readarray(x,n);
insertionsort(x,n);
printf("Sorted elements using insertion sort.....\n");
printarray(x,n);
getch();
}
/*..............OUTPUT.............
* * * * * INSERTION SORT * * * * *
Enter no. of elements to be sorted : 6
Enter elements of an array.....
enter value a[0] = 2
enter value a[1] = 3
enter value a[2] = 6
enter value a[3] = 7
enter value a[4] = 9
enter value a[5] = 1
Sorted elements using insertion sort.....
a[0] = 1
a[1] = 2
a[2] = 3
a[3] = 6
a[4] = 7
a[5] = 9
*/