回 帖 发 新 帖 刷新版面

主题:请大虾帮吗?直接插入排序算法实现问题。。。

#include <iostream.h>
#include <string.h>
#include <stdio.h>

void InsertSort(char *str)
{
    char temp;
    int i,j,num;
    for(i=1;i<strlen(str);i++)
    {
        num=strlen(str);
        temp=str[i];
        if(str[i-1]>temp)
        {
            j=i-1;
            while(j>=0&&str[j]>temp)
            {
                str[j+1]=str[j];//这一步为什么有问题
                j--;
            }
            str[j+1]=temp;
        }
    }
}

void main()
{
    char *a="qwertyuasdfgh";
    InsertSort(a);
    cout<<a<<endl;
}

请大虾们帮忙。
这个程序是直接插入排序的算法实现,在vc++6.0的运行环境下,编译都通过了,但是运行时出现问题,单步调试的时候在我标识的那一行报错了,哪位大虾帮忙解释下。
我初步判断是函数形参中的指针问题和字符串数组长度的问题,哪位大虾能够详细解释下。

回复列表 (共1个回复)

沙发

那个是在常量区.

我来回复

您尚未登录,请登录后再回复。点此登录或注册