-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
184 lines (143 loc) · 3.36 KB
/
utils.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bitmapStruct.h"
void printArr(const int* nums, int numsSize){
int i;
printf("\n");
for (i=0; i<numsSize; ++i){
printf("%4d ", nums[i]);
}
printf("\n");
}
void printArrLong(const LONG* nums, int numsSize){
int i;
printf("\n");
for (i=0; i<numsSize; ++i){
printf("%4d ", nums[i]);
}
printf("\n");
}
void printArrDbe(const double* nums, int numsSize){
int i;
printf("\n");
for (i=0; i<numsSize; ++i){
printf("%6.2lf ", nums[i]);
}
printf("\n");
}
void printErrors(char* info){
printf("\n!!! Error: %s !!!\n\n", info);
}
void printSuccess(char* info){
//printf("\n!!! Success: %s !!!\n\n", info);
}
char* strJoin(char* a, char* b){
char* c = (char*)malloc(strlen(a)+strlen(b)+1);
if(c == NULL){
printErrors("malloc failed");
}
strcat(c, a);
strcat(c, b);
return c;
}
double getDecimalPart(double x){
return x - (int)x;
}
double getExtremumValue(const double nums[], int numsSize, int mode){
int i;
double max, min;
max = min = nums[0];
for (i=0; i<numsSize; ++i){
max = (max>nums[i]) ? max : nums[i];
min = (min<nums[i]) ? min : nums[i];
}
return (mode==0) ? min : max;
}
LONG getExtremumValueByte(const LONG nums[], int numsSize, int mode){
int i;
LONG max, min;
max = min = nums[0];
for (i=0; i<numsSize; ++i){
max = (max>nums[i]) ? max : nums[i];
min = (min<nums[i]) ? min : nums[i];
}
return (mode==0) ? min : max;
}
int getExtremumIndex(const double nums[], int numsSize, int mode){
int i, maxIndex, minIndex;
maxIndex = minIndex = 0;
for (i=0; i<numsSize; ++i){
maxIndex = (nums[maxIndex]>nums[i]) ? maxIndex : i;
minIndex = (nums[minIndex]<nums[i]) ? minIndex : i;
}
return (mode==0) ? minIndex : maxIndex;
}
BYTE* countingSort(BYTE* nums, int numsSize, int maxValue){
int i;
BYTE* arrTemp = (BYTE*)calloc(maxValue, sizeof(BYTE));
BYTE* result = (BYTE*)calloc(numsSize, sizeof(BYTE));
for (i=0; i<numsSize; ++i){
++arrTemp[nums[i]];
}
// arrTemp[i] now contains the number of elements equal to i;
for (i=1; i<maxValue; ++i){
arrTemp[i] += arrTemp[i-1];
}
// arrTemp[i] now contains the number of elements less than or equal to i;
for (i=numsSize-1; i>=0; --i){
result[arrTemp[nums[i]]-1] = nums[i];
--arrTemp[nums[i]];
}
return result;
}
BYTE getMedianValue(BYTE* nums, int numsSize, int maxValue){
BYTE* sortedNums = countingSort(nums, numsSize, maxValue);
return sortedNums[numsSize/2];
}
float fMax(float a, float b){
return (a>b)?a:b;
}
double getMaxValue(const double nums[], int numsSize) {
int i;
double max;
max = nums[0];
for (i = 0; i<numsSize; ++i) {
max = (max>nums[i]) ? max : nums[i];
}
return max;
}
int getMaxIndex(const double nums[], int numsSize) {
int i, maxIndex;
maxIndex = 0;
for (i = 0; i<numsSize; ++i) {
maxIndex = (nums[maxIndex]>nums[i]) ? maxIndex : i;
}
return maxIndex;
}
void exchange(double* a, double* b) {
double temp = *a;
*a = *b;
*b = temp;
}
int partition(double* A, int p, int r) { // 这种partition方法具有更高的效率,应该优先应用。
double x = A[r];
int i = p - 1;
for (int j = p; j<r; ++j) {
if (A[j] < x) {
++i;
exchange(&A[i], &A[j]);
}
}
exchange(&A[i + 1], &A[r]);
return i + 1;
}
void quickSort(double* A, int p, int r) {
int q;
if (p < r) {
q = partition(A, p, r);
quickSort(A, p, q - 1);
quickSort(A, q + 1, r);
}
}