-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertionsort.c
93 lines (74 loc) · 1.92 KB
/
insertionsort.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Bliss
// Found Insertion sort at https://www.geeksforgeeks.org/insertion-sort/
// It was more efficient than my code so I replaced it.
//Can toggle between the two sort blocks by encapsulating with /**/
#include "insertionsort.h"
int insertionSort(int arr[], int size, int* ncompares_ptr, int* nswaps_ptr, int* removal_ptr, int* insertion_ptr) {
int i = 0, key = 0, j = 0;
for (i = 1; i < size; i++)
{
key = arr[i];
j = i - 1;
*removal_ptr = *removal_ptr + 1;
// Move elements of arr[0..i-1], that are
// greater than key, to one position ahead
// of their current position
while (j >= 0 && arr[j] > key)
{
#ifdef IS_VERBOSE
printf(" Items compared: [ %d, %d ] \n", arr[j], key);
#endif
*ncompares_ptr = *ncompares_ptr + 1;
arr[j + 1] = arr[j];
j = j - 1;
*nswaps_ptr = *nswaps_ptr + 1;
}
arr[j + 1] = key;
#ifdef IS_VERBOSE
printf(" Inserting %d at location %d\n", key, j + 1);
printf("Iteration %d#: ", i);
display(arr, size);
#endif
*insertion_ptr = *insertion_ptr + 1;
}
// My less efficient code
/*
int i = 0;
int j = 0;
int select = 0;
int temp = 0;
while (i < size) {
j = i;
select = arr[i];
*removal_ptr = *removal_ptr + 1;
while (j >= 0) {
#ifdef IS_VERBOSE
printf(" Items compared: [ %d, %d ] ", arr[j], select);
#endif
*ncompares_ptr = *ncompares_ptr + 1;
if (arr[j] > select) {
temp = arr[j];
arr[j] = select;
arr[j + 1] = temp;
#ifdef IS_VERBOSE
printf(" => shifting [%d, %d]\n", arr[j], arr[j + 1]);
#endif
*nswaps_ptr = *nswaps_ptr + 1;
}
else {
#ifdef IS_VERBOSE
printf(" => not swapped\n");
#endif
}
j--;
}
//#ifdef IS_VERBOSE
printf("Iteration %d#: ", i);
display(arr, size);
//#endif
*insertion_ptr = *insertion_ptr + 1;
i++;
}
*/
return *nswaps_ptr + *ncompares_ptr + *removal_ptr + *insertion_ptr;
}