-
Notifications
You must be signed in to change notification settings - Fork 0
/
1051. Height Checker.c
44 lines (38 loc) · 1.14 KB
/
1051. Height Checker.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
/** C
先將 array 複製一份再用快速排序法排序後將兩份 array 比較是否有所不同 , 回傳不同的元素個數
int bubble_sort(int *heights, int heightsSize);
void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*));
int cmpfunc (const void * a, const void * b);
int heightChecker(int *heights, int heightsSize){
int swap_time = 0;
int *temp = malloc(sizeof(int) * heightsSize);
for (int i = 0; i < heightsSize; i++) {
temp[i] = heights[i];
}
qsort(heights, heightsSize, sizeof(int), cmpfunc);
for (int i = 0; i < heightsSize; i++) {
if (temp[i] != heights[i])
swap_time++;
}
return swap_time;
}
int cmpfunc (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int bubble_sort(int *heights, int heightsSize)
{
int swap = 0, time = 0;
for (int i = 0; i < heightsSize; i++) {
for (int j = 0; j < heightsSize; j++) {
if (heights[i] < heights[j]) {
swap = heights[i];
heights[i] = heights[j];
heights[j] = swap;
time++;
}
}
}
return time;
}
*/