-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertion_sort.c
39 lines (31 loc) · 918 Bytes
/
insertion_sort.c
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
#include <stdio.h>
#include <stddef.h>
void insertion_sort(int *arr, size_t size) {
int key;
long j;
for (size_t i = 1; i < size; i++) {
key = arr[i];
for (j = i - 1; j >= 0 && arr[j] > key; j--) {
arr[j + 1] = arr[j];
}
arr[j + 1] = key;
}
}
int main(void) {
int arr[] = { 5, 2, 8, 1, 4 };
int expected[] = { 1, 2, 4, 5, 8 };
size_t size = sizeof(arr) / sizeof(arr[0]);
printf("Testing Insertion Sort:\n");
insertion_sort(arr, size);
printf("Expected: ");
for (size_t i = 0; i < size; i++) {
printf("%d ", expected[i]);
}
printf("\n");
printf("Actual: ");
for (size_t i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}