-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertionsort.c
65 lines (53 loc) · 970 Bytes
/
insertionsort.c
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
//Insertion sort
#include<stdio.h>
void insertionsort(int a[], int n)
{
int pass,x,pos,i;
for(pass = 1; pass <= n-1; pass++)
{
x = a[pass];
pos = pass;
i = pass-1;
while( i >= 0)
{
if( a[i] < x)
break;
a[i+1] = a[i];
i--;
}
a[i+1] = x;
}
}
int main()
{
int a[20], n , i;
printf("Enter the number of elements in an array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter %dth element:",i+1);
scanf("%d",&a[i]);
}
printf("\nBefore sort array is: ");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
insertionsort(a,n); //call to mergesort
printf("\nAfter final sort array is: ");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
return 0;
}
/* OUTPUT
Enter the number of elements in an array:5
Enter 1th element:0
Enter 2th element:-22
Enter 3th element:-11
Enter 4th element:4
Enter 5th element:2
Before sort array is: 0 -22 -11 4 2
After final sort array is: -22 -11 0 2 4
*/