-
Notifications
You must be signed in to change notification settings - Fork 25
/
ins_sort.c
48 lines (40 loc) · 887 Bytes
/
ins_sort.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
#include<stdio.h>
void Insertion_sort(int [],int );
int main(){
int n,x;
printf("Enter the size of array : ");
scanf("%d",&n);
int arr[n];
printf("Enter the value of array : ");
for (int i = 0; i < n; i++)
{
scanf("%d",&arr[i]);
}
printf("\nBefore Sort : \n\n ");
for (int i = 0; i < n; i++)
{
printf("%d ",arr[i]);
}
Insertion_sort(arr,n);
printf("\nAfter Sort : \n\n ");
for (int i = 0; i < n; i++)
{
printf("%d ",arr[i]);
}
return 0;
}
void Insertion_sort(int arr[],int n ){
for (int i = 0; i < n-1; i++)
{
for (int j = i+1; j > 0; j--)
{
if(arr[j] < arr[j-1]){
int temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
}
else
break;
}
}
}