#include<stdio.h>
void shellsort();
int main()
{
char strs[10][20]={"should","we","go","to","the","park","with","her","aunt","melly"};
shellsort(&strs[0],10);
return 0;
}
void shellsort(char (*p)[][10],int n) //将十个字符串由小到大排列好
{
int gap=n/2;
int i;
char min[10];
for(gap;gap>0;gap/=2){
for(i=0;i<gap;i++){
int i_=i;
for(i_;i_<=n-gap;i_+=gap){ //选择法
int j,x;
strncpy(min,(*p)[i_],10); //预先假设一个最小值min(等于行字符串)
for(j=i_+gap;j<n;j+=gap){
if(strcmp(min,(*p)[j])>0){
strncpy(min,(*p)[j],10);
x=j;
}
}
if(strcmp(min,(*p)[i_])!=0){ //如果min不等于预先设定的值,则说明(*p) [i_]不是最小,与最小的互换
strncpy((*p)[x],(*p)[i_],10);
strncpy((*p)[i_],min,10);
}
}
}
}
for(i=0;i<n;i++){
printf("%s\n",(*p)[i]);
}
}
在图片中,首次调试运行到24行前min如愿被初始化等于“should”,但为什么运行了24行之后又变为0了呢?(24行是想把比min更小的字符串给min)