回覆列表
-
1 # 中年人也愛動漫
-
2 # 使用者551921813817
main()
{
char s[10];
printf("輸入7 個字元的字串:");
gets(s);
fun(s,7);
printf("\n%s",s);
}
void fun(char *s,int num)
{
int i,j;
char t;
for(i=0;i
{
for(j=i+1;j
if(*(s+j)
{
t=*(s+i);
*(s+i)=*(s+j);
*(s+j)=t;
}
}
}
插入排序
void fun2(char *s,int n)
{
int i,j;
char temp;
for (i = 1;i
{
temp = s[i];
for (j = i-1;j >= 0 && temp
{
s[j+1] = s[j];
}
s[j+1] = temp;
}
}
main(){char s[10];printf("輸入7 個字元的字串:");gets(s);fun(s,7);printf("\n%s",s);}void fun(char *s,int num){ int i,j; char t; for(i=0;i< num;i++) //從頭開始 { for(j=i+1;j< num;j++) if(*(s+j)<*(s+i)) //小者上浮 { t=*(s+i); *(s+i)=*(s+j); *(s+j)=t; } }}插入排序void fun2(char *s,int n){ int i,j; char temp; for (i = 1;i < n;++ i) { temp = s[i]; for (j = i-1;j >= 0 && temp <s[j];-- j) { s[j+1] = s[j]; } s[j+1] = temp; }}