-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertionSort.c
More file actions
37 lines (37 loc) · 867 Bytes
/
insertionSort.c
File metadata and controls
37 lines (37 loc) · 867 Bytes
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
#include<stdio.h>
void main() {
int a[20], i, n, j, temp;
printf("Enter value of n : ");
scanf("%d", &n);
// Write the for loop to read array elements
for(i = 0; i < n; i++)
{
printf("Enter element for a[%d] : ", i);
scanf("%d", &a[i]);
}
printf("Before sorting the elements in the array are\n");
// Write the for loop to display array elements before sorting
for(i = 0; i < n; i++)
{
printf("Value of a[%d] = %d\n", i, a[i]);
}
//Write the code to sort elements
for(i = 1; i < n; i++)
{
temp = a[i];
for(j = i; j > 0; j--)
{
if(a[j - 1] > temp)
{
a[j] = a[j - 1];
a[j - 1] = temp;
}
}
}
printf("After sorting the elements in the array are\n");
// Write the for loop to display array elements after sorting
for(i = 0; i < n; i++)
{
printf("Value of a[%d] = %d\n", i, a[i]);
}
}