Skip to content

Commit 3756988

Browse files
committed
完成第八章测试
1 parent 0b90132 commit 3756988

11 files changed

+111
-16
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,8 @@ tags
4747
compile_commands.json
4848
.ycm_extra_conf.py
4949
.color_coded
50+
51+
## 我的自定义文件
52+
play
53+
play.c
54+

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ OBJS = $(patsubst %.c, %.o, $(SRCS))
88
CC = gcc
99
CFLAGS = -Wall -g -std=gnu99
1010
INCLUDEFLAGS =
11-
LDFLAGS = -lcunit
11+
LDFLAGS = -lcunit -lm
1212
TARGET = main
1313
TEST = test
1414

@@ -39,7 +39,7 @@ clean:
3939
@rm -f $(TARGET) $(OBJS) **/*.d **/*.d.*
4040

4141
.PHONY:debug
42-
debug:
42+
debug: all
4343
@gdb $(TEST)
4444

4545
.PHONY:dump

chap08/radix_sort.c

+7-11
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,26 @@
99
#include <stdlib.h>
1010

1111
void radix_sort(int nums[], int len) {
12-
int *counts = malloc(COUNTING_SORT_NUM_MAX * sizeof(int));
13-
int *sorted = malloc(len * sizeof(int));
14-
int base = 10;
12+
int counts[COUNTING_SORT_NUM_MAX];
13+
int sorted[len];
1514
// 从最低有效位开始为关键字进行计数排序(只要是稳定排序就行)
16-
for (int j = 0; j < RADIX_SORT_NUM_MAX_WIDTH; j++) {
15+
int max_len = get_dgt_cnt(RAND_MAX);
16+
for (int j = 0; j < max_len; j++) {
1717
for (int i = 0; i < COUNTING_SORT_NUM_MAX; i++) {
1818
counts[i] = 0;
1919
}
2020
for (int i = 0; i < len; i++) {
21-
counts[nums[i]%base/(base/10)]++;
21+
counts[get_ith_dgt(nums[i], j)]++;
2222
}
2323
for (int i = 1; i < COUNTING_SORT_NUM_MAX; i++) {
2424
counts[i] += counts[i-1];
2525
}
26-
// 必须从后往前遍历,以保证排序的稳定性
2726
for (int i = len-1; i >= 0; i--) {
28-
sorted[counts[nums[i]%base/(base/10)]-1] = nums[i];
29-
counts[nums[i]%base/(base/10)]--;
27+
sorted[counts[get_ith_dgt(nums[i], j)]-1] = nums[i];
28+
counts[get_ith_dgt(nums[i], j)]--;
3029
}
3130
for (int i = 0; i < len; i++) {
3231
nums[i] = sorted[i];
3332
}
34-
base *= 10;
3533
}
36-
free(counts);
37-
free(sorted);
3834
}

chap08/radix_sort.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#ifndef RADIX_SORT
22
#define RADIX_SORT
33

4-
#define RADIX_SORT_NUM_MAX_WIDTH 5
4+
#include <limits.h>
5+
#include <stdio.h>
6+
#include <math.h>
7+
#define RADIX_SORT_NUM_MAX_WIDTH ((int)floor((log(abs(INT_MAX))/(log(10)))) + 1)
58
void radix_sort(int nums[], int len);
6-
79
#endif

chap08/test_counting_sort.c

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "counting_sort.h"
2+
#include "test_counting_sort.h"
3+
#include "../utils.h"
4+
#include <CUnit/CUnit.h>
5+
#include <stdlib.h>
6+
7+
static void test_counting_sort() {
8+
int len = 10;
9+
int nums[len];
10+
for (int i = 0; i < len; i++) {
11+
nums[i] = rand() % len;
12+
}
13+
counting_sort(nums, len);
14+
for (int i = 1; i < len; i++) {
15+
CU_ASSERT(nums[i-1] <= nums[i]);
16+
}
17+
}
18+
19+
CU_ErrorCode add_test_counting_sort() {
20+
CU_pSuite pSuite = CU_add_suite("counting_sort", NULL, NULL);
21+
CHECK_CU_GLOBAL();
22+
CU_ADD_TEST(pSuite, test_counting_sort);
23+
CHECK_CU_GLOBAL();
24+
return CUE_SUCCESS;
25+
}

chap08/test_counting_sort.h

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef TEST_COUNTING_SORT_H
2+
#define TEST_COUNTING_SORT_H
3+
4+
#include <CUnit/CUnit.h>
5+
6+
CU_ErrorCode add_test_counting_sort();
7+
8+
#endif

chap08/test_radix_sort.c

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "radix_sort.h"
2+
#include "test_radix_sort.h"
3+
#include "../utils.h"
4+
#include <CUnit/CUnit.h>
5+
#include <stdlib.h>
6+
7+
static void test_radix_sort() {
8+
int len = 100;
9+
int nums[len];
10+
for (int i = 0; i < len; i++) {
11+
nums[i] = rand();
12+
}
13+
radix_sort(nums, len);
14+
for (int i = 1; i < len; i++) {
15+
CU_ASSERT(nums[i-1] <= nums[i]);
16+
}
17+
}
18+
19+
CU_ErrorCode add_test_radix_sort() {
20+
CU_pSuite pSuite = CU_add_suite("radix_sort", NULL, NULL);
21+
CHECK_CU_GLOBAL();
22+
CU_ADD_TEST(pSuite, test_radix_sort);
23+
CHECK_CU_GLOBAL();
24+
return CUE_SUCCESS;
25+
}

chap08/test_radix_sort.h

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef TEST_RADIX_SORT_H
2+
#define TEST_RADIX_SORT_H
3+
4+
#include <CUnit/CUnit.h>
5+
6+
CU_ErrorCode add_test_radix_sort();
7+
8+
#endif

test.c

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include "chap04/test_maximum_subarray.h"
44
#include "chap06/test_heap_sort.h"
55
#include "chap07/test_quick_sort.h"
6+
#include "chap08/test_counting_sort.h"
7+
#include "chap08/test_radix_sort.h"
68
#include "chap15/test_optimal_binary_search_tree.h"
79
#include "utils.h"
810
#include <stdio.h>
@@ -29,6 +31,8 @@ int main() {
2931
CHECK_CU_RETURN(add_test_maximum_subarray());
3032
CHECK_CU_RETURN(add_test_heap_sort());
3133
CHECK_CU_RETURN(add_test_quick_sort());
34+
CHECK_CU_RETURN(add_test_counting_sort());
35+
CHECK_CU_RETURN(add_test_radix_sort());
3236
CHECK_CU_RETURN(add_test_obst());
3337

3438
//使用console控制交互界面的函数入口

utils.c

+18
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*/
55
#include "utils.h"
66
#include <stdio.h>
7+
#include <math.h>
8+
79
void print_array(int nums[], int len) {
810
printf("[");
911
for (int i = 0; i < len; i++) {
@@ -40,17 +42,33 @@ void swap(int nums[], int a, int b) {
4042
nums[a] = nums[b];
4143
nums[b] = tmp;
4244
}
45+
4346
int maxnum(int a, int b) {
4447
if (a > b) {
4548
return a;
4649
} else {
4750
return b;
4851
}
4952
}
53+
5054
int minnum(int a, int b) {
5155
if (a < b) {
5256
return a;
5357
} else {
5458
return b;
5559
}
5660
}
61+
62+
63+
int get_ith_dgt(int num, int i) {
64+
return num % (int)pow(10, i+1) / (int)pow(10, i);
65+
}
66+
67+
int get_dgt_cnt(int num) {
68+
int i = 1;
69+
while (num > pow(10, i)-1) {
70+
i++;
71+
}
72+
return i;
73+
}
74+

utils.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@
1919
} \
2020
} \
2121
while (0)
22-
22+
/**
23+
* 输出数组
24+
*/
2325
void print_array(int nums[], int len);
2426
void print_array2(int **nums, int rows, int cols);
2527
void swap(int nums[], int a, int b);
2628
int maxnum(int a, int b);
2729
int minnum(int a, int b);
30+
int get_ith_dgt(int num, int i);
31+
int get_dgt_cnt(int num);
2832
#endif

0 commit comments

Comments
 (0)