Skip to content

Commit ec78dc5

Browse files
authored
Create insertion_sort.c
1 parent 5d7cd75 commit ec78dc5

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

sort/insertion_sort.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// INSERTION SORT
2+
3+
#include<stdio.h>
4+
5+
int main(){
6+
7+
int i,j,s,temp,a[20];
8+
9+
printf("Enter total elements: ");
10+
scanf("%d",&s);
11+
12+
printf("Enter %d elements: ",s);
13+
for(i=0;i<s;i++)
14+
scanf("%d",&a[i]);
15+
16+
for(i=1;i<s;i++){
17+
temp=a[i];
18+
j=i-1;
19+
while((temp<a[j])&&(j>=0)){
20+
a[j+1]=a[j];
21+
j=j-1;
22+
}
23+
a[j+1]=temp;
24+
}
25+
26+
printf("After sorting: ");
27+
for(i=0;i<s;i++)
28+
printf(" %d",a[i]);
29+
30+
return 0;
31+
}

0 commit comments

Comments
 (0)