-
Notifications
You must be signed in to change notification settings - Fork 88
/
InsertionSort.c
57 lines (53 loc) · 1.01 KB
/
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
#include<stdio.h>
void printArray(int *a,int n)
{
for (int i = 0; i < n; i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
void InsertionSort(int *a,int n)
{
int key,j,i;
// Loop for Passes
for (i = 1; i <=n-1; i++)
{
key=a[i];
j=i-1;
// Loop for each pass
while(j>=0 && a[j]>key)
{
a[j+1]=a[j];
j--;
}
a[j+1]=key;
}
}
int main()
{
int n,a[100];
printf("Enter the size of array: ");
scanf("%d",&n);
printf("Enter the elements of the array: ");
for (int i = 0; i < n; i++)
{
scanf("%d",&a[i]);
}
printf("Array Before Sorting: ");
printArray(a,n);
InsertionSort(a,n);
printf("Array after Sorting: ");
printArray(a,n);
}
/* OUTPUT:-
Enter the size of array: 7
Enter the elements of the array: 55
99
23
1
0
66
99
Array Before Sorting: 55 99 23 1 0 66 99
Array after Sorting: 0 1 23 55 66 99 99 */