-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertion.c
51 lines (47 loc) · 863 Bytes
/
insertion.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
#include<stdio.h>
int a[10],n;
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void print_arr(int a[],int n)
{
for(int i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
printf("\n");
}
void insertion_sort(int a[],int n)
{
int j;
for(int i= 1 ; i<n ; i++)
{
int ele = a[i];
for(j = i-1 ; j>=0 && ele<a[j]; j --)
{
a[j+1] = a [j];
}
a[j+1] = ele;
printf("Pass %d : ",i);
print_arr(a,n);
}
}
void main()
{
printf("Enter the no of elements in the array : ");
scanf("%d",&n);
printf("Enter the elements\n");
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("The unsorted array is : ");
print_arr(a,n);
insertion_sort(a,n);
printf("The sorted array is : ");
print_arr(a,n);
}