Skip to content

Commit b2b3096

Browse files
committed
[code] refactoring 42~51
1 parent c49b96a commit b2b3096

File tree

11 files changed

+640
-574
lines changed
  • 000-inc
  • 042-插入排序
  • 043-希尔排序
  • 044-冒泡排序
  • 045-快速排序
  • 046-选择排序
  • 047-堆排序
  • 048-归并排序
  • 049-基数排序
  • 050-二叉搜索树操作
  • 051-二项式系数递归

11 files changed

+640
-574
lines changed

000-inc/inc.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414
#endif
1515
#endif // end #ifdef CLS_AND_GETCH
1616

17+
// bool
18+
#ifndef TRUE
19+
#define TRUE 1
20+
#endif // #ifndef TRUE
21+
#ifndef FALSE
22+
#define FALSE 0
23+
#endif // #ifndef FALSE
24+
1725

1826
/* 辅助函数定义 */
1927

042-插入排序/42.c

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,51 @@
1+
// 042 插入排序
12
#include <stdio.h>
3+
// #define CLS_AND_GETCH // 需要清屏+按键退出就取消此行的注释
4+
#include "../000-inc/inc.h"
25
#define MAX 255
3-
int R[MAX];
4-
void Insert_Sort(int n)
5-
{ /* 对数组R中的记录R[1..n]按递增序进行插入排序 */
6-
int i,j;
7-
for(i=2;i<=n;i++) /* 依次插入R[2],…,R[n] */
8-
if(R[i]<R[i-1])
9-
{/* 若R[i]大于等于有序区中所有的R,则R[i] */
10-
/* 应在原有位置上 */
11-
R[0]=R[i];j=i-1; /* R[0]是哨兵,且是R[i]的副本 */
12-
do{ /* 从右向左在有序区R[1..i-1]中查找R[i]的插入位置 */
13-
R[j+1]=R[j]; /* 将关键字大于R[i]的记录后移 */
14-
j--;
15-
}while(R[0]<R[j]); /* 当R[i]≥R[j]时终止 */
16-
R[j+1]=R[0]; /* R[i]插入到正确的位置上 */
17-
}
18-
}
19-
20-
main()
21-
{
22-
int i,n;
6+
7+
8+
// 对数组 R 中的记录 R[1..n] 按递增序进行*插入排序*
9+
void Insert_Sort(int R[], int n) {
10+
int i, j;
11+
12+
// 依次插入 R[2], ..., R[n]
13+
for (i = 2; i <= n; i++) {
14+
if (R[i] < R[i - 1]) {
15+
// 若 R[i] 大于等于有序区中所有的 R,
16+
// 则 R[i] 应在原有位置上
17+
R[0] = R[i];
18+
j = i - 1; // R[0] 是哨兵,且是 R[i] 的副本
19+
do {
20+
// 从右向左在有序区 R[1..i-1] 中查找 R[i] 的插入位置
21+
R[j + 1] = R[j]; // 将关键字大于 R[i] 的记录后移
22+
j--;
23+
} while (R[0] < R[j]); // 当 R[i] ≥ R[j] 时终止
24+
R[j + 1] = R[0]; // R[i] 插入到正确的位置上
25+
}
26+
} // end for (i = 2; i <= n; i++)
27+
} // void Insert_Sort
28+
29+
int main() {
30+
int i, n, R[MAX];
31+
2332
clrscr();
24-
puts("Please input total element number of the sequence:");
25-
scanf("%d",&n);
26-
if(n<=0||n>MAX)
27-
{
28-
printf("n must more than 0 and less than %d.\n",MAX);
33+
printf("Please input total element number of the sequence: ");
34+
scanf("%d", &n);
35+
if (n <= 0 || n > MAX) {
36+
printf("n must more than 0 and less than %d.\n", MAX);
2937
exit(0);
3038
}
31-
puts("Please input the elements one by one:");
32-
for(i=1;i<=n;i++)
33-
scanf("%d",&R[i]);
39+
40+
printf("Please input the elements one by one: ");
41+
for (i = 1; i <= n; i++) scanf("%d", &R[i]);
3442
puts("The sequence you input is:");
35-
for(i=1;i<=n;i++)
36-
printf("%4d",R[i]);
37-
Insert_Sort(n);
38-
puts("\nThe sequence after insert_sort is:");
39-
for(i=1;i<=n;i++)
40-
printf("%4d",R[i]);
41-
puts("\n Press any key to quit...");
42-
getchar();
43-
getchar();
44-
}
43+
for (i = 1; i <= n; i++) printf("%4d", R[i]);
44+
45+
Insert_Sort(R, n);
46+
47+
puts("\nThe sequence after insert sort is:");
48+
for (i = 1; i <= n; i++) printf("%4d", R[i]);
49+
50+
quit();
51+
}

043-希尔排序/43.c

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,56 @@
1+
// 043 希尔排序
12
#include <stdio.h>
3+
// #define CLS_AND_GETCH // 需要清屏+按键退出就取消此行的注释
4+
#include "../000-inc/inc.h"
25
#define MAX 255
3-
int R[MAX];
4-
void ShellPass(int d, int n)
5-
{ // 希尔排序中的一趟排序,d为当前增量
6+
7+
8+
// 希尔排序中的一趟排序,d 为当前增量
9+
// 采用插入排序
10+
void ShellPass(int R[], int d, int n) {
611
int i, j;
7-
for (i = d + 1; i <= n; i++) // 将R[d+1..n]分别插入各组当前的有序区
8-
if (R[i] < R[i - d])
9-
{
10-
R[0] = R[i];
11-
j = i - d; // R[0]只是暂存单元,不是哨兵
12-
do
13-
{ // 查找R[i]的插入位置
12+
13+
for (i = d + 1; i <= n; i++)
14+
// 将 R[d+1..n] 分别插入各组当前的有序区
15+
if (R[i] < R[i - d]) {
16+
R[0] = R[i]; // R[0] 只是暂存单元,不是哨兵
17+
j = i - d;
18+
do { // 查找 R[i] 的插入位置
1419
R[j + d] = R[j]; // 后移记录
1520
j = j - d; // 查找前一记录
1621
} while (j > 0 && R[0] < R[j]);
17-
R[j + d] = R[0]; // 插入R[i]到正确的位置上
18-
} // endif
22+
R[j + d] = R[0]; // 插入 R[i] 到正确的位置上
23+
} // endif
1924
} // end of ShellPass
2025

21-
void Shell_Sort(int n)
22-
{
23-
int increment = n; // 增量初值,不妨设n>0
24-
do
25-
{
26+
void Shell_Sort(int arr[], int n) {
27+
int increment = n; // 增量初值,不妨设 n>0
28+
do {
2629
increment = increment / 3 + 1; // 求下一增量
27-
ShellPass(increment, n); // 一趟增量为increment的Shell插入排序
30+
ShellPass(arr, increment, n); // 一趟增量为 increment 的 Shell 插入排序
2831
} while (increment > 1);
2932
} // ShellSort
3033

31-
void main()
32-
{
33-
int i, n;
34+
int main() {
35+
int i, n, R[MAX];
36+
3437
clrscr();
35-
puts("Please input total element number of the sequence:");
38+
printf("Please input total element number of the sequence: ");
3639
scanf("%d", &n);
37-
if (n <= 0 || n > MAX)
38-
{
40+
if (n <= 0 || n > MAX) {
3941
printf("n must more than 0 and less than %d.\n", MAX);
4042
exit(0);
4143
}
42-
puts("Please input the elements one by one:");
43-
for (i = 1; i <= n; i++)
44-
scanf("%d", &R[i]);
44+
45+
printf("Please input the elements one by one: ");
46+
for (i = 1; i <= n; i++) scanf("%d", &R[i]);
4547
puts("The sequence you input is:");
46-
for (i = 1; i <= n; i++)
47-
printf("%4d", R[i]);
48-
Shell_Sort(n);
49-
puts("\nThe sequence after shell_sort is:");
50-
for (i = 1; i <= n; i++)
51-
printf("%4d", R[i]);
52-
puts("\n Press any key to quit...");
53-
getch();
48+
for (i = 1; i <= n; i++) printf("%4d", R[i]);
49+
50+
Shell_Sort(R, n);
51+
52+
puts("\nThe sequence after Shell Sort is:");
53+
for (i = 1; i <= n; i++) printf("%4d", R[i]);
54+
55+
quit();
5456
}

044-冒泡排序/44.c

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,50 @@
1+
// 044 冒泡排序
12
#include <stdio.h>
3+
// #define CLS_AND_GETCH // 需要清屏+按键退出就取消此行的注释
4+
#include "../000-inc/inc.h"
25
#define MAX 255
3-
int R[MAX];
4-
void Bubble_Sort(int n)
5-
{ // R(l..n)是待排序的文件,采用自下向上扫描,对R做冒泡排序
6-
int i, j;
6+
7+
// R(l..n)是待排序的文件,采用自下向上扫描,对R做冒泡排序
8+
void Bubble_Sort(int R[], int n) {
79
int exchange; // 交换标志
8-
for (i = 1; i < n; i++)
9-
{ // 最多做n-1趟排序
10-
exchange = 0; // 本趟排序开始前,交换标志应为假
11-
for (j = n - 1; j >= i; j--) // 对当前无序区R[i..n]自下向上扫描
12-
if (R[j + 1] < R[j])
13-
{ // 交换记录
14-
R[0] = R[j + 1]; // R[0]不是哨兵,仅做暂存单元
10+
11+
for (int i = 1; i < n; i++) { // 最多做 n-1 趟排序
12+
exchange = FALSE; // 本趟排序开始前,交换标志应为假
13+
for (int j = n - 1; j >= i; j--) {
14+
// 对当前无序区 R[i..n] 自下向上扫描
15+
if (R[j + 1] < R[j]) {
16+
// 交换记录
17+
R[0] = R[j + 1]; // R[0]不是哨兵,仅做暂存单元
1518
R[j + 1] = R[j];
16-
R[j] = R[0];
17-
exchange = 1; // 发生了交换,故将交换标志置为真
19+
R[j] = R[0];
20+
exchange = TRUE; // 发生了交换,故将交换标志置为真
1821
}
19-
if (!exchange) // 本趟排序未发生交换,提前终止算法
20-
return;
21-
}
22-
}
22+
}
23+
// 本趟排序未发生交换,提前终止算法
24+
if (!exchange) return;
25+
} // for (i = 1; i < n; i++)
26+
} // void Bubble_Sort
27+
28+
int main() {
29+
int i, n, R[MAX];
2330

24-
void main()
25-
{
26-
int i, n;
2731
clrscr();
28-
puts("Please input total element number of the sequence:");
32+
printf("Please input total element number of the sequence: ");
2933
scanf("%d", &n);
30-
if (n <= 0 || n > MAX)
31-
{
34+
if (n <= 0 || n > MAX) {
3235
printf("n must more than 0 and less than %d.\n", MAX);
3336
exit(0);
3437
}
35-
puts("Please input the elements one by one:");
36-
for (i = 1; i <= n; i++)
37-
scanf("%d", &R[i]);
38+
39+
printf("Please input the elements one by one: ");
40+
for (i = 1; i <= n; i++) scanf("%d", &R[i]);
3841
puts("The sequence you input is:");
39-
for (i = 1; i <= n; i++)
40-
printf("%4d", R[i]);
41-
Bubble_Sort(n);
42-
puts("\nThe sequence after bubble_sort is:");
43-
for (i = 1; i <= n; i++)
44-
printf("%4d", R[i]);
45-
puts("\n Press any key to quit...");
46-
getchar();
47-
getchar();
42+
for (i = 1; i <= n; i++) printf("%4d", R[i]);
43+
44+
Bubble_Sort(R, n);
45+
46+
puts("\nThe sequence after Bubble Sort is:");
47+
for (i = 1; i <= n; i++) printf("%4d", R[i]);
48+
49+
quit();
4850
}

045-快速排序/45.c

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,66 @@
1+
// 045 快速排序
12
#include <stdio.h>
3+
// #define CLS_AND_GETCH // 需要清屏+按键退出就取消此行的注释
4+
#include "../000-inc/inc.h"
5+
26
#define MAX 255
3-
int R[MAX];
4-
int Partition(int i, int j)
5-
{ // 调用Partition(R,low,high)时,对R[low..high]做划分,*/
6-
// 并返回基准记录的位置
7-
int pivot = R[i]; // 用区间的第1个记录作为基准
8-
while (i < j)
9-
{ // 从区间两端交替向中间扫描,直至i=j为止
10-
while (i < j && R[j] >= pivot) // pivot相当于在位置i上
11-
j--; // 从右向左扫描,查找第1个关键字小于pivot.key的记录R[j]
12-
if (i < j) // 表示找到的R[j]的关键字<pivot.key
13-
R[i++] = R[j]; // 相当于交换R[i]和R[j],交换后i指针加1
14-
while (i < j && R[i] <= pivot) // pivot相当于在位置j上*/
15-
i++; // 从左向右扫描,查找第1个关键字大于pivot.key的记录R[i]
16-
if (i < j) // 表示找到了R[i],使R[i].key>pivot.key
17-
R[j--] = R[i]; // 相当于交换R[i]和R[j],交换后j指针减1
18-
} // endwhile
19-
R[i] = pivot; // 基准记录已被最后定位*/
7+
8+
// 调用 Partition(R,low,high) 时,对 R[low..high] 做划分
9+
// 并返回基准记录的位置
10+
int Partition(int R[], int i, int j) {
11+
int pivot = R[i]; // 用区间的第 1 个记录作为基准
12+
while (i < j) { // 从区间两端交替向中间扫描,直至 i=j 为止
13+
// pivot 相当于在位置i上从右向左扫描,
14+
// 查找第1个关键字小于 pivot.key 的记录 R[j]
15+
while (i < j && R[j] >= pivot) j--;
16+
// 表示找到的 R[j] 的关键字 pivot.key
17+
if (i < j) R[i++] = R[j]; // 相当于交换 R[i] 和 R[j],交换后i指针加1
18+
19+
// pivot相当于在位置j上从左向右扫描,
20+
// 查找第1个关键字大于 pivot.key 的记录 R[i]
21+
while (i < j && R[i] <= pivot) i++;
22+
// 表示找到了 R[i],使 R[i].key>pivot.key
23+
if (i < j) R[j--] = R[i]; // 相当于交换R[i]和R[j],交换后j指针减1
24+
} // end while (i < j)
25+
R[i] = pivot; // 基准记录已被最后定位
2026
return i;
2127
} // end of partition
2228

23-
void Quick_Sort(int low, int high)
24-
{ // 对R[low..high]快速排序
29+
// 对 R[low..high] 快速排序
30+
void _Quick_Sort_Rec(int arr[], int low, int high) {
2531
int pivotpos; // 划分后的基准记录的位置
26-
if (low < high)
27-
{ // 仅当区间长度大于1时才须排序
28-
pivotpos = Partition(low, high); // 对R[low..high]做划分
29-
Quick_Sort(low, pivotpos - 1); // 对左区间递归排序
30-
Quick_Sort(pivotpos + 1, high); // 对右区间递归排序
32+
if (low < high) { // 仅当区间长度大于 1 时才须排序
33+
pivotpos = Partition(arr, low, high); // 对 R[low..high] 做划分
34+
_Quick_Sort_Rec(arr, low, pivotpos - 1); // 对左区间递归排序
35+
_Quick_Sort_Rec(arr, pivotpos + 1, high); // 对右区间递归排序
3136
}
3237
} // end of Quick_Sort
3338

34-
void main()
35-
{
36-
int i, n;
39+
// 对 arr 快速排序
40+
void Quick_Sort(int arr[], int n) {
41+
_Quick_Sort_Rec(arr, 1, n);
42+
}
43+
44+
int main() {
45+
int i, n, R[MAX];
46+
3747
clrscr();
38-
puts("Please input total element number of the sequence:");
48+
printf("Please input total element number of the sequence: ");
3949
scanf("%d", &n);
40-
if (n <= 0 || n > MAX)
41-
{
50+
if (n <= 0 || n > MAX) {
4251
printf("n must more than 0 and less than %d.\n", MAX);
4352
exit(0);
4453
}
45-
puts("Please input the elements one by one:");
46-
for (i = 1; i <= n; i++)
47-
scanf("%d", &R[i]);
54+
55+
printf("Please input the elements one by one: ");
56+
for (i = 1; i <= n; i++) scanf("%d", &R[i]);
4857
puts("The sequence you input is:");
49-
for (i = 1; i <= n; i++)
50-
printf("%4d", R[i]);
51-
Quick_Sort(1, n);
52-
puts("\nThe sequence after quick_sort is:");
53-
for (i = 1; i <= n; i++)
54-
printf("%4d", R[i]);
55-
puts("\n Press any key to quit...");
56-
getch();
58+
for (i = 1; i <= n; i++) printf("%4d", R[i]);
59+
60+
Quick_Sort(R, n);
61+
62+
puts("\nThe sequence after Quick Sort is:");
63+
for (i = 1; i <= n; i++) printf("%4d", R[i]);
64+
65+
quit();
5766
}

0 commit comments

Comments
 (0)