【问题标题】:Inserting one element in the right position in a sorted array using binary search to find the right position,,,but having a run time error使用二进制搜索将一个元素插入到已排序数组中的正确位置以找到正确的位置,但是有运行时错误
【发布时间】:2016-06-04 13:47:30
【问题描述】:

这是我的代码


#include <stdio.h>

void show(int*,int);
int sort (int*,int);
int BinSearch(int[],int,int);
int Insertelem(int*,int,int,int*);

int main(void)
{
    int array[100]={15,30,40,10,25,5,35,20,45};
    int len=9;//length
    int *pos=0;//position
    int elem=0;//element
    int ok=0;

    printf("array : ");
    show(array,len);
    sort(array,len);
    printf("array after sorting : ");
    show(array,len);
    printf("enter the number you want to add :");
    scanf("%d",&elem);
    *pos=BinSearch(array,elem,len);
    ok=Insertelem(array,len,elem,pos);
    show(array,len);

    return 0;
}
void show(int *array,int len)//print the array
{
    for(int i=0;i<len;i++)
    {
        printf("%d,",array[i]);
    }
    printf("\n");
    }
int sort(int *array,int len)//sort the array
{
    int help=0;
    for(int i=len-1;i>0;i--)
    {
        for(int j=0;j<i;j++)
        {
            if(array[j]>array[j+1])
            {
                help=array[j];
                array[j]=array[j+1];
                array[j+1]=help;
            }
        }
    }
    return *array;
}
int BinSearch(int array[100],int elem,int len)//searching for the position
{
    int first=1;
    int last=len-1;
    int middle=(first+last)/2;
    int pos;

    while(first<=last)
    {
        if(elem<array[middle])
        {
            last=middle-1;
            middle=(first+last)/2;
        }
        else if(elem>array[middle])
        {
            first=middle+1;
            middle=(first+last)/2;
        }
        else if(elem==array[middle])
        {
            break;
        }

    }
    pos=array[middle]+1;
    return pos;
}
int Insertelem(int *array,int len,int elem,int *pos)//inserting the element
                                                    //in the right position 
{
    len++;
    for(int i=len-1;i>*pos+1;i--)
    {
        array[i+1]=array[i];
    }
    elem=array[*pos];
    return *array;
}

注意:没有编译器错误


我认为问题出在 binsearch 函数或 Insertelem 函数中,因为我测试了所有其他函数并且它们按预期工作


当我运行这段代码时,它运行得非常好,直到用户输入他不会输入的元素,程序就会崩溃

【问题讨论】:

  • 在调试器中运行它或添加一些打印来跟踪正在发生的事情。这是您需要学习的基本技能的一部分。用一堆“可能相关”的标签发布“它不起作用”只是懒惰。
  • 1) int first=1; 应该是 int first=0; 2) *pos=BinSearch... : pos == 0 不能取消引用。

标签: c if-statement for-loop while-loop binary-search


【解决方案1】:
    int pos=-1;//change not pointer
    ...
    pos=BinSearch(array,elem,len);
    ok=Insertelem(array,len,elem,&pos);//pos is no need for a pointer, because since not changed.
    len += 1;//The length must be changed(or by inside Insertelem)
    ...

int BinSearch(int array[100],int elem,int len)//searching for the position
{
    int first=0;//not 1
    ...  
    pos=first;//not midlle, not element of array(array[middle])
    return pos;
}
int Insertelem(int *array,int len,int elem,int *pos)
{
    ...
    for(int i=len-1;i>= *pos;i--)//not i>*pos+1
    ...
    array[*pos]=elem;//not elem=array[*pos];
    return *array;//??
}

【讨论】:

    猜你喜欢
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-27
    • 2020-01-19
    • 1970-01-01
    相关资源
    最近更新 更多