无效的数组传递?

时间:2010-11-18 15:02:26

标签: c

我做了一个合并排序功能:

void mergeSort(int emotionCount[], int low, int high){
    int i=0,k=0;
    //I did this to see the value inside the array, and I always got a garbage value 
    //when i=0, and the first correct value when i=1. I made a for loop here to 
    //see the values in the array in debugging mode in netbean.
    for (i=0;i<=high;i++){            
    }
    if (low == high){
        emotionCount[low]=emotionCount[low];          
    }else{
        int mid = (low+high)/2;
        mergeSort(emotionCount,low,mid);        
        mergeSort(emotionCount,mid+1,high);
        merge(emotionCount, low,mid, high);        }
}
void merge(int emotionCount[], int low,int mid, int high)
{
    int temp[high-low+1];
    int i=low,k=mid+1, j=high, n=0;
    int comparing=emotionCount[k];
    while (i<=mid || k<=high){
        while (emotionCount[i]<comparing)
        {
            temp[n]=emotionCount[i];
            i++;
            n++;

        }
        comparing=emotionCount[i];
        temp[n]=emotionCount[k];
        k++;
        i++;
        n++;
        while (emotionCount[k]<comparing){
            temp[n]=emotionCount[k];
            k++;
            n++;
        }
        comparing=emotionCount[k];
        temp[n]=emotionCount[i];
        k++;
        i++;
        n++;
   }
    while (i<=mid)
    {
        temp[n]=emotionCount[i];
        i++;
        n++;
    }
    while (k<=high)
    {
        temp[n]=emotionCount[k];
        k++;
        n++;
    }
    while (low<=high)
    {
        emotionCount[low]=temp[i];
        low++;
    }
}

在main中,我传递一个数组:

int array[10] = {0,5,8,2,4,6,8,2,20,25};
//number 9 because the highest position is 9. After this, array[10] is supposed to be sorted.
mergeSort(array ,0, 9);

嗯,这个方法有点长,但我想自己实现它。基本上,我将一个数组传递给mergeSort函数,如果它没有达到它的最小大小(即1),它将继续传递该数组。困扰我的是,当我将数组传递给它时,第一个值总是垃圾值(如地址值或其他东西)。只有在i = 1之后,它才会给出数组的第一个值。我不明白。此外,每次退出较低的mergeSort函数以继续更高的值时,数组中的所有排序值都将变为0.

编辑:k变量仅用于保存数组值,以便在我处于调试模式时查看数组值。我删除了mergeSort函数中的k变量以消除混淆。

4 个答案:

答案 0 :(得分:1)

然而,要阅读您的功能,但乍一看突出的问题是您调用该功能的方式。

应该是:

mergeSort(array, 0, 9); 

如果您使用array[10],则意味着您正在尝试传入数组的第11个元素,不幸的是它超出界限!

答案 1 :(得分:0)

看起来你正在从数组内部传递一个值而不是数组本身:不应该

mergeSort(array[10] ,0, 9);

类似

mergeSort(array, 0, 0);

答案 2 :(得分:0)

for (i=0;i<=high;i++){
    int k=*(emotionCount+i);
}
if (low == high){
    emotionCount[low]=emotionCount[low];
    int k=emotionCount[low];
}else{
    int mid = (low+high)/2;
    mergeSort(emotionCount,low,mid);
    int i=0;
    for (i=low;i<=mid;i++){
        int k=emotionCount[i];
    }

    mergeSort(emotionCount,mid+1,high);
    for (i=mid+1;i<=high;i++){
        int k=emotionCount[i];
    }
    merge(emotionCount, low,mid, high);
    for (i=low;i<=high;i++){
        int k=emotionCount[i];
    }
}

您在所有这些块中重新声明了int k。我假设您要为k分配一个值,因此删除前导“int”。就像你声称熟悉的Java一样。

你还想做什么?即使您使用全局声明的k,在循环中分配所有数组元素根本没有意义。基本上你最终会将k等于emotionCount [high]。

您知道可以使用[]语法取消引用数组元素吗?

k = emotionCount[i]

更容易阅读。

你使用Netbeans有一个很好的GDB前端。我建议您设置几个断点,逐步执行代码并尝试了解您在那里实现的内容。

答案 3 :(得分:0)

我解决了这个问题。更正后的代码:

void mergeSort(int emotionCount[], int low, int high){
    int i=0,k=0;

    if (low == high){
        emotionCount[low]=emotionCount[low];
        k=emotionCount[low];
    }else{
        int mid = (low+high)/2;
        mergeSort(emotionCount,low,mid);
        /*
        int i=0;            
        printf("Lower half:");
        for (i=low;i<=mid;i++){
            k=emotionCount[i];            
            printf("%d ",k);            
        }             
        printf("\n");*/

        mergeSort(emotionCount,mid+1,high);

        /*
        printf("Upper half:");
        for (i=mid+1;i<=high;i++){
            k=emotionCount[i];            
            printf("%d ",k);
        }
        printf("\n");*/

        merge(emotionCount, low,mid, high);
        //this is how I view the sorting process, along with other 2 above loops
       /* printf("Merged:");
        for (i=low;i<=high;i++){            
            k=emotionCount[i];
            printf("%d ",k);
        }
        printf("\n\n");*/
    }
}
void merge(int emotionCount[], int low,int mid, int high)
{
    int temp[high-low+1],index;
    for (index=0;index<high-low+1;index++)
    {
        temp[index]=0;
    }
    int i=low,k=mid+1, j=high, n=0;
    int comparing=emotionCount[k],temp1;
    while (i<=mid && k<=high){
        while (emotionCount[i]<comparing && i<=mid)
        {
            temp[n]=emotionCount[i];
            i++;
            n++;
        }
        if (i<=mid){
            comparing=emotionCount[i];
            if (k<=high)
                temp[n]=emotionCount[k];
            else
                temp[n]=comparing;
            k++;
            n++;
        }

        while (emotionCount[k]<comparing && k<=high){
            temp[n]=emotionCount[k];
            k++;
            n++;
        }
        if (k<=high){
            comparing=emotionCount[k];
            if (i<=mid)
                temp[n]=emotionCount[i];
            else
                temp[n]=comparing;
            i++;
            n++;
        }

   }
    while (i<=mid)
    {
        temp[n]=emotionCount[i];        
        i++;
        n++;
    }
    while (k<=high)
    {
        temp[n]=emotionCount[k];        
        k++;
        n++;
    }
    i=0;
    while (low<=high)
    {
        emotionCount[low]=temp[i];
        low++;
        i++;
    }
}

初始高值必须是数组大小 - 1。