forked from seeditsolution/cprogram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Insertion sort
40 lines (37 loc) · 899 Bytes
/
Insertion sort
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
// Write a program in C that sorts any given array in Ascending order
#include<stdio.h>
#include<conio.h>
int main()
{
int arr[50], size, i, j, k, element, index;
printf("Enter Array Size: ");
scanf("%d", &size);
printf("Enter %d Array Elements: ", size);
for(i=0; i<size; i++)
scanf("%d", &arr[i]);
for(i=1; i<size; i++)
{
element = arr[i];
if(element<arr[i-1])
{
for(j=0; j<=i; j++)
{
if(element<arr[j])
{
index = j;
for(k=i; k>j; k--)
arr[k] = arr[k-1];
break;
}
}
}
else
continue;
arr[index] = element;
}
printf("\nSorted Array:\n");
for(i=0; i<size; i++)
printf("%d ", arr[i]);
getch();
return 0;
}