-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertion_sort.c
More file actions
49 lines (48 loc) · 969 Bytes
/
insertion_sort.c
File metadata and controls
49 lines (48 loc) · 969 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
43
44
45
46
47
48
49
#include<stdio.h>
#include<stdlib.h>
void input(int *,int );
void display(int *,int);
void insertion(int *,int);
void main()
{
int *a,n;
printf("Enter the no.of elements\n");
scanf("%d",&n);
a=(int*)malloc(sizeof(int)*n);
input(a,n);
printf("The aaray before sorting\n");
display(a,n);
insertion(a,n);
printf("\nSorted array\n");
display(a,n);
}
void input(int *a,int n)
{
int i;
for(i=0;i<n;i++)
{
printf("Element [%d]",i+1);
scanf("%d",&a[i]);//why we need to take a+i, instead of a[i]//
}
}
void display(int *a,int n)
{ int i;
printf("\n");
for(i=0;i<n;i++)
{printf(" %d ",a[i]);}
}
void insertion(int *a, int n)
{
int i,j,temp;
for(i=1;i<n;i++)
{
temp=a[i];
j=i-1;
while(j>=0 && a[j]>temp)
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
}