forked from Nihal-Priyadarshi/C-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertion-sort.c
More file actions
42 lines (34 loc) · 860 Bytes
/
insertion-sort.c
File metadata and controls
42 lines (34 loc) · 860 Bytes
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
//sorting an array using insertion sort.......
#include<stdio.h>
#include<stdlib.h>
void main()
{
int i,j,*p,n,temp=0,k;
printf("enter the size of the array:\n");
scanf("%d",&n);
printf("enter the elements of the array:\n");
p=(int*)calloc(n,4);
for(i=0;i<n;i++)
{
scanf("%d",(p+i));
}
printf("sorting the array in ascending order using insertion sort....\n");
for(i=0;i<n;i++)
{
for(j=i;j>0&&*(p+j-1);j--)
{
if(*(p+j)<*(p+j-1))
{
temp=*(p+j);
*(p+j)=*(p+j-1);
*(p+j-1)=temp;
}
}
printf("modified array:");
for(k=0;k<n;k++)
{
printf(" %d",*(p+k));
}
printf("\n");
}
}