Skip to content

Commit 7de3dbd

Browse files
committed
完成第九章测试
1 parent 3756988 commit 7de3dbd

8 files changed

+61
-8
lines changed

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@ $(TARGET): $(addsuffix .o, $(TARGET)) $(OBJS)
2121
$(TEST): $(addsuffix .o, $(TEST)) $(ALLOBJS)
2222
@$(CC) -o $@ $^ $(LDFLAGS)
2323

24+
# 要么把所有源文件加到依赖;要么设为phony。
25+
# 否则首次生成过tags之后后续会因为已存在不再生成
2426
tags: $(ALLFILES)
2527
@ctags -R .
2628

2729
%.o:%.c
2830
@$(CC) -o $@ -c $< $(CFLAGS) $(INCLUDEFLAGS)
2931

32+
# 使用gcc的-MM选项自动生成各源文件的依赖
3033
%.d:%.c
3134
@set -e; rm -f $@; $(CC) -MM $< $(INCLUDEFLAGS) > $@.$$$$; \
3235
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \

chap08/test_counting_sort.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
#include <stdlib.h>
66

77
static void test_counting_sort() {
8-
int len = 10;
8+
int len = 100;
99
int nums[len];
1010
for (int i = 0; i < len; i++) {
11-
nums[i] = rand() % len;
11+
nums[i] = rand() % COUNTING_SORT_NUM_MAX;
1212
}
1313
counting_sort(nums, len);
1414
for (int i = 1; i < len; i++) {

chap09/order_statistic.c

+9-6
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,16 @@ int median_of_medians(int nums[], int begin, int end) {
6565
part[j] = nums[i*5+j];
6666
}
6767
int part_len = 5;
68-
if (i > len / 5) {
68+
if (i == parts_count-1) {
6969
part_len = last_part_len;
7070
}
71+
// 这里可以直接使用插入排序确定每组的中位数
72+
// 树上就是这样做而且证明这样整个算法还是最坏线性
73+
// 我的理解是因为每组的大小是常数
7174
insertion_sort(part, part_len);
72-
medians[i] = part[part_len/2];
75+
medians[i] = part[(part_len-1)/2];
7376
}
74-
int mm = linear_select(medians, 0, parts_count, parts_count/2);
77+
int mm = medians[(parts_count-1)/2];
7578
free(medians);
7679
return mm;
7780
}
@@ -98,9 +101,9 @@ int linear_select(int nums[], int begin, int end, int i) {
98101
if (len == 1) {
99102
return nums[begin];
100103
}
101-
// index of median_of_medians
102-
int imm = median_of_medians(nums, begin, end);
103-
int q = specified_partition(nums, begin, end, imm);
104+
// median_of_medians
105+
int mm = median_of_medians(nums, begin, end);
106+
int q = specified_partition(nums, begin, end, mm);
104107
int left_len = q - begin;
105108
if (i == left_len) {
106109
return nums[q];

chap09/test_order_statistic.c

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

chap09/test_order_statistic.h

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

test.c

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "chap07/test_quick_sort.h"
66
#include "chap08/test_counting_sort.h"
77
#include "chap08/test_radix_sort.h"
8+
#include "chap09/test_order_statistic.h"
89
#include "chap15/test_optimal_binary_search_tree.h"
910
#include "utils.h"
1011
#include <stdio.h>
@@ -33,6 +34,7 @@ int main() {
3334
CHECK_CU_RETURN(add_test_quick_sort());
3435
CHECK_CU_RETURN(add_test_counting_sort());
3536
CHECK_CU_RETURN(add_test_radix_sort());
37+
CHECK_CU_RETURN(add_test_order_statistic());
3638
CHECK_CU_RETURN(add_test_obst());
3739

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

utils.c

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
#include "utils.h"
66
#include <stdio.h>
7+
#include <stdlib.h>
78
#include <math.h>
89

910
void print_array(int nums[], int len) {
@@ -72,3 +73,10 @@ int get_dgt_cnt(int num) {
7273
return i;
7374
}
7475

76+
void shuffle(int nums[], int len) {
77+
// 最后一个位置自动确定了
78+
for (int i = len-1; i > 0; i--) {
79+
swap(nums, i, rand()%(i+1));
80+
}
81+
}
82+

utils.h

+1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ int maxnum(int a, int b);
2929
int minnum(int a, int b);
3030
int get_ith_dgt(int num, int i);
3131
int get_dgt_cnt(int num);
32+
void shuffle(int nums[], int len);
3233
#endif

0 commit comments

Comments
 (0)