-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertion_sort.c
More file actions
43 lines (32 loc) · 977 Bytes
/
insertion_sort.c
File metadata and controls
43 lines (32 loc) · 977 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
41
42
43
#include<stdio.h>
void insertion_sort(int arr[], int len);
int main()
{
int arr[] = {8, 4, 11, 6, 19, 13, 7, 24, 16, 3};
int length = sizeof(arr) / sizeof(int);
insertion_sort(arr, length);
getch();
return 0;
}
void insertion_sort(int arr[], int len)
{
int i, j, k, temp;
for(i = 1; i < len; i++)
{
for(j = 0; j < i; j++)
{
if(arr[i] < arr[j]) // Reverse sign to change to descending order
{
temp = arr[i];
for(k = i; k > j; k--)
arr[k] = arr[k - 1];
arr[j] = temp;
}
}
}
printf("\n\nInsertion Sort Result: ");
for(i = 0; i < len; i++)
{
printf("%d ", arr[i]);
}
}