Skip to content

Commit c49b96a

Browse files
committed
[code] mv files
1 parent e20a4cd commit c49b96a

File tree

3 files changed

+112
-109
lines changed
  • 043-希尔排序
  • 044-冒泡排序
  • 045-快速排序

3 files changed

+112
-109
lines changed

043-希尔排序/43.c

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,54 @@
11
#include <stdio.h>
22
#define MAX 255
33
int R[MAX];
4-
void Bubble_Sort(int n)
5-
{ /* R(l..n)是待排序的文件,采用自下向上扫描,对R做冒泡排序 */
6-
int i,j;
7-
int exchange; /* 交换标志 */
8-
for(i=1;i<n;i++){ /* 最多做n-1趟排序 */
9-
exchange=0; /* 本趟排序开始前,交换标志应为假 */
10-
for(j=n-1;j>=i;j--) /* 对当前无序区R[i..n]自下向上扫描 */
11-
if(R[j+1]<R[j]){/* 交换记录 */
12-
R[0]=R[j+1]; /* R[0]不是哨兵,仅做暂存单元 */
13-
R[j+1]=R[j];
14-
R[j]=R[0];
15-
exchange=1; /* 发生了交换,故将交换标志置为真 */
16-
}
17-
if(!exchange) /* 本趟排序未发生交换,提前终止算法 */
18-
return;
19-
}
20-
}
4+
void ShellPass(int d, int n)
5+
{ // 希尔排序中的一趟排序,d为当前增量
6+
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]的插入位置
14+
R[j + d] = R[j]; // 后移记录
15+
j = j - d; // 查找前一记录
16+
} while (j > 0 && R[0] < R[j]);
17+
R[j + d] = R[0]; // 插入R[i]到正确的位置上
18+
} // endif
19+
} // end of ShellPass
20+
21+
void Shell_Sort(int n)
22+
{
23+
int increment = n; // 增量初值,不妨设n>0
24+
do
25+
{
26+
increment = increment / 3 + 1; // 求下一增量
27+
ShellPass(increment, n); // 一趟增量为increment的Shell插入排序
28+
} while (increment > 1);
29+
} // ShellSort
2130

2231
void main()
2332
{
24-
int i,n;
33+
int i, n;
2534
clrscr();
2635
puts("Please input total element number of the sequence:");
27-
scanf("%d",&n);
28-
if(n<=0||n>MAX)
36+
scanf("%d", &n);
37+
if (n <= 0 || n > MAX)
2938
{
30-
printf("n must more than 0 and less than %d.\n",MAX);
39+
printf("n must more than 0 and less than %d.\n", MAX);
3140
exit(0);
3241
}
3342
puts("Please input the elements one by one:");
34-
for(i=1;i<=n;i++)
35-
scanf("%d",&R[i]);
43+
for (i = 1; i <= n; i++)
44+
scanf("%d", &R[i]);
3645
puts("The sequence you input is:");
37-
for(i=1;i<=n;i++)
38-
printf("%4d",R[i]);
39-
Bubble_Sort(n);
40-
puts("\nThe sequence after bubble_sort is:");
41-
for(i=1;i<=n;i++)
42-
printf("%4d",R[i]);
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]);
4352
puts("\n Press any key to quit...");
44-
getchar();
45-
getchar();
53+
getch();
4654
}

044-冒泡排序/44.c

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,48 @@
11
#include <stdio.h>
22
#define MAX 255
33
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){ /* 从区间两端交替向中间扫描,直至i=j为止 */
9-
while(i<j&&R[j]>=pivot) /* pivot相当于在位置i上 */
10-
j--; /* 从右向左扫描,查找第1个关键字小于pivot.key的记录R[j] */
11-
if(i<j) /* 表示找到的R[j]的关键字<pivot.key */
12-
R[i++]=R[j]; /* 相当于交换R[i]和R[j],交换后i指针加1 */
13-
while(i<j&&R[i]<=pivot) /* pivot相当于在位置j上*/
14-
i++; /* 从左向右扫描,查找第1个关键字大于pivot.key的记录R[i] */
15-
if(i<j) /* 表示找到了R[i],使R[i].key>pivot.key */
16-
R[j--]=R[i]; /* 相当于交换R[i]和R[j],交换后j指针减1 */
17-
} /* endwhile */
18-
R[i]=pivot; /* 基准记录已被最后定位*/
19-
return i;
20-
} /* end of partition */
21-
22-
void Quick_Sort(int low,int high)
23-
{ /* 对R[low..high]快速排序 */
24-
int pivotpos; /* 划分后的基准记录的位置 */
25-
if(low<high){/* 仅当区间长度大于1时才须排序 */
26-
pivotpos=Partition(low,high); /* 对R[low..high]做划分 */
27-
Quick_Sort(low,pivotpos-1); /* 对左区间递归排序 */
28-
Quick_Sort(pivotpos+1,high); /* 对右区间递归排序 */
29-
}
30-
} /* end of Quick_Sort */
31-
4+
void Bubble_Sort(int n)
5+
{ // R(l..n)是待排序的文件,采用自下向上扫描,对R做冒泡排序
6+
int i, j;
7+
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]不是哨兵,仅做暂存单元
15+
R[j + 1] = R[j];
16+
R[j] = R[0];
17+
exchange = 1; // 发生了交换,故将交换标志置为真
18+
}
19+
if (!exchange) // 本趟排序未发生交换,提前终止算法
20+
return;
21+
}
22+
}
3223

3324
void main()
3425
{
35-
int i,n;
26+
int i, n;
3627
clrscr();
3728
puts("Please input total element number of the sequence:");
38-
scanf("%d",&n);
39-
if(n<=0||n>MAX)
29+
scanf("%d", &n);
30+
if (n <= 0 || n > MAX)
4031
{
41-
printf("n must more than 0 and less than %d.\n",MAX);
32+
printf("n must more than 0 and less than %d.\n", MAX);
4233
exit(0);
4334
}
4435
puts("Please input the elements one by one:");
45-
for(i=1;i<=n;i++)
46-
scanf("%d",&R[i]);
36+
for (i = 1; i <= n; i++)
37+
scanf("%d", &R[i]);
4738
puts("The sequence you input is:");
48-
for(i=1;i<=n;i++)
49-
printf("%4d",R[i]);
50-
Quick_Sort(1,n);
51-
puts("\nThe sequence after quick_sort is:");
52-
for(i=1;i<=n;i++)
53-
printf("%4d",R[i]);
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]);
5445
puts("\n Press any key to quit...");
55-
getch();
56-
46+
getchar();
47+
getchar();
5748
}

045-快速排序/45.c

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,57 @@
11
#include <stdio.h>
22
#define MAX 255
33
int R[MAX];
4-
void ShellPass(int d, int n)
5-
{/* 希尔排序中的一趟排序,d为当前增量 */
6-
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];j=i-d; /* R[0]只是暂存单元,不是哨兵 */
11-
do {/* 查找R[i]的插入位置 */
12-
R[j+d]=R[j];/* 后移记录 */
13-
j=j-d; /* 查找前一记录 */
14-
}while(j>0&&R[0]<R[j]);
15-
R[j+d]=R[0]; /* 插入R[i]到正确的位置上 */
16-
} /* endif */
17-
} /* end of ShellPass */
18-
19-
void Shell_Sort(int n)
20-
{
21-
int increment=n; /* 增量初值,不妨设n>0 */
22-
do {
23-
increment=increment/3+1; /* 求下一增量 */
24-
ShellPass(increment,n); /* 一趟增量为increment的Shell插入排序 */
25-
}while(increment>1);
26-
} /* ShellSort */
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; // 基准记录已被最后定位*/
20+
return i;
21+
} // end of partition
2722

23+
void Quick_Sort(int low, int high)
24+
{ // 对R[low..high]快速排序
25+
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); // 对右区间递归排序
31+
}
32+
} // end of Quick_Sort
2833

2934
void main()
3035
{
31-
int i,n;
36+
int i, n;
3237
clrscr();
3338
puts("Please input total element number of the sequence:");
34-
scanf("%d",&n);
35-
if(n<=0||n>MAX)
39+
scanf("%d", &n);
40+
if (n <= 0 || n > MAX)
3641
{
37-
printf("n must more than 0 and less than %d.\n",MAX);
42+
printf("n must more than 0 and less than %d.\n", MAX);
3843
exit(0);
3944
}
4045
puts("Please input the elements one by one:");
41-
for(i=1;i<=n;i++)
42-
scanf("%d",&R[i]);
46+
for (i = 1; i <= n; i++)
47+
scanf("%d", &R[i]);
4348
puts("The sequence you input is:");
44-
for(i=1;i<=n;i++)
45-
printf("%4d",R[i]);
46-
Shell_Sort(n);
47-
puts("\nThe sequence after shell_sort is:");
48-
for(i=1;i<=n;i++)
49-
printf("%4d",R[i]);
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]);
5055
puts("\n Press any key to quit...");
5156
getch();
52-
5357
}

0 commit comments

Comments
 (0)