-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathinsertionSortV1.c
More file actions
54 lines (47 loc) · 1.4 KB
/
Copy pathinsertionSortV1.c
File metadata and controls
54 lines (47 loc) · 1.4 KB
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
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#define SIZE 6
void insertion_sort(int unsorted[], int sorted[], int size) {
int sorted_size = 0;
int u, i, j; //counters
int element; //current element you are positioning
int inserted=0; //0= not yet inserted, 1= inserted
for (u = 0; u < size; u++) {
element = unsorted[u];
inserted = 0; //0 means not yet inserted
// Try to insert into the correct position
for (i = 0; i < sorted_size; i++) {
if (element < sorted[i]) {
// Shift elements to the right
for (j = sorted_size; j > i; j--) {
sorted[j] = sorted[j - 1];
}
sorted[i] = element;
inserted = 1; //1 means inserted
sorted_size++;
break;
}
}
// If not inserted, append to end
if (!inserted) {
sorted[sorted_size] = element;
sorted_size++;
}
}
}
int main() {
int unsorted[SIZE] = {6, 2, 10, 7, 1, 5};
int sorted[SIZE]; // same size as unsorted
int i; //loop counter
printf("Unsorted list: ");
for (i = 0; i < SIZE; i++) {
printf("%d ", unsorted[i]);
}
printf("\n");
insertion_sort(unsorted, sorted, SIZE);
printf("Sorted list: ");
for (i = 0; i < SIZE; i++) {
printf("%d ", sorted[i]);
}
printf("\n");
return 0;
}