-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorted_squared.c
67 lines (58 loc) · 1.57 KB
/
sorted_squared.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
#include <stdio.h>
#include <stdlib.h>
typedef __int32_t int32_t;
void checkErrorArrays(int32_t *expected, int32_t *result, int32_t length, int32_t *failedCounter) {
for (int32_t i = 0; i < length; i++) {
if (expected[i] != result [i]) {
printf("expected and result differ at index %d\n", i);
(*failedCounter)++;
return;
}
}
}
void sort_squared(int32_t *list, int length) {
int32_t inflection_point = -1;
for (int32_t i = 0; i < length; ++i) {
if (list[i] < 0) {
inflection_point = i;
}
list[i] = list[i]*list[i];
}
int32_t *temp = (int32_t*)malloc(4*inflection_point);
for (int32_t i = inflection_point; i >= 0; --i) {
temp[inflection_point-i] = list[i];
}
int32_t i = 0, j = inflection_point+1, ind = 0;
while (i <= inflection_point && j < length) {
if (temp[i] >= list[j]) {
list[ind] = list[j];
j++;
} else {
list[ind] = temp[i];
i++;
}
ind++;
}
while (i <= inflection_point) {
list[ind] = temp[i];
ind++;
i++;
}
free(temp);
}
void test() {
int32_t failedCounter = 0;
int32_t input[] = {-9, -2, 0, 2, 3};
int32_t expected_output[] = {0, 4, 4, 9, 81};
sort_squared(input, 5);
checkErrorArrays(expected_output, input, 5, &failedCounter);
if (failedCounter != 0) {
printf("Failed %d tests\n", failedCounter);
} else {
printf("PASSED ALL TESTS!\n");
}
}
int main() {
test();
return 0;
}