写了一个inst.c文件:
/*======================================
// 函数名: inst
// 功能描述: 直接插入排序
// 输入参数: x 存放待排序数据的数组
// n 数组长度
// 返回值: 无
//=======================================*/
void inst(int *x,int n)
{
int i,j,t;
for(i = 1; i < n; i ++) /* i 表示插入次数, 共进行 n-1 次插入 */
{
t = x[i];
for( j = i - 1; j >= 0; j --) /* 寻找要插入t的位置 */
if(t < x[j]) /* 后移,空出位置 */
x[j + 1] = x[j];
else
break; /* 找到位置时,退出循环*/
x[j + 1] = t; /* 直接插入 */
}
}
写了一个shel.c文件:
/*============================================
// 函数名: shel
// 功能描述: 希尔排序
// 输入参数: x 存放待排序数据的数组
// n 数组长度
// 返回值: 无
//==============================================*/
void shel(int *x,int n)
{
int i,j,d,t;
d = n/2; /* 初始步长是 n/2 */
while (d > 0) /* 按不同步长进行排序 */
{
for(i = d; i < n; i ++) /* 将x[i]插入到对应分组的合适位置中*/
{
t = x[i];
j = i - d;
while((j >= 0) && (x[j] > t)) /* 比较和移动 */
{
x[j + d] = x[j];
j = j -d;
}
x[j + d] = t;
}
d = d/2;
}
}
写了一个主文件inst0.c,用来包括inst.c,shel.c文件:
#include "stdio.h"
#include "inst.c"
#include "shel.c"
main()
{
extern void inst(int *x, int n);
extern void shel(int *x, int n);
int i,j,p[50],p1[50];
printf("Data:\n");
for(i = 0; i < 5; i ++)
{
for(j = 0; j < 10; j ++)
{
p[10 * i + j] = 20 + 10*i -j; /*生成一些无序数据*/
p1[10 * i + j] = p[10 * i + j];
printf("%d ",p[10 * i + j]);
}
printf("\n");
}
printf("Insert Sort:\n"); /* 直接插入排序的结果 */
inst(p, 50);
for ( i = 0; i < 5; i ++)
{
for( j = 0; j < 10; j ++)
printf("%d ", p[10*i + j]);
printf("\n");
}
printf("Shell Sort:\n"); /* 希尔排序的结果 */
shel(p1, 50);
for(i = 0; i < 5; i ++)
{
printf("%d ", p1[10*i + j]);
printf("\n");
}
}
/tmp/ccYEBSct.o: In function `inst‘:
inst.c:(.text+0x0): multiple definition of `inst‘
/tmp/ccNrTZCA.o:inst0.c:(.text+0x0): first defined here
/tmp/ccdZkuQl.o: In function `shel‘:
shel.c:(.text+0x0): multiple definition of `shel‘
/tmp/ccNrTZCA.o:inst0.c:(.text+0xa3): first defined here
collect2: error: ld returned 1 exit status
需要修改一下inst0.c文件:
去掉 #include "inst.c"
#include "shel.c"
两句,
再进行编译:
gcc -o inst0 inst0.c inst.c shel.c
会生成可执行文件:ints0,
执行该文件:./inst0
Data:
20 19 18 17 16 15 14 13 12 11
30 29 28 27 26 25 24 23 22 21
40 39 38 37 36 35 34 33 32 31
50 49 48 47 46 45 44 43 42 41
60 59 58 57 56 55 54 53 52 51
Insert Sort:
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 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
Shell Sort:
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 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
原文地址:http://blog.csdn.net/wangjiaweiwei/article/details/42360597