-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.c
More file actions
40 lines (36 loc) · 890 Bytes
/
Copy pathInsertionSort.c
File metadata and controls
40 lines (36 loc) · 890 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
38
39
40
// Insertion Sort code
// Insertion Sort is quite similar to Bubble Sort
// With a little modification, it will arrange numbers in reverse order
#include <stdio.h>
int main()
{
int n, array[1000], c, d, t, flag = 0;
// A "flag" is simply a "true" or a "false"
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 1 ; c <= n - 1; c++)
{
t = array[c];
for (d = c - 1 ; d >= 0; d--)
{
if (array[d] > t)
{
array[d+1] = array[d];
flag = 1; // "flag" with '0' for "false" and '1' for "true"
}
else
break;
}
if (flag)
array[d+1] = t;
}
printf("Sorted list in ascending order:\n");
for (c = 0; c <= n - 1; c++)
{
printf("%d\n", array[c]);
}
return 0;
}