简体   繁体   English

排序算法的行为不正确

[英]Incorrect behaviour for sorting algorithm

I am writing a sorting algorithm. 我正在写一个排序算法。 At the moment, I am trying to fill up an array with sorted elements. 目前,我正在尝试使用已排序的元素填充数组。 This is kind of a bubble sort method, I believe. 我相信,这是一种气泡排序方法。 Basically, what I'm doing is I'm ranking lines based on score with highest score at bestmatch[0], etc. For each line I run stage3(). 基本上,我要做的是根据在bestmatch [0]时得分最高的得分对行进行排名。对于每行,我运行stage3()。

So, essentially, I take the Score (score for each line) and I compare it with what is in the array, then add this in based on its rank comparatively. 因此,从本质上讲,我将获得分数(每行的分数),并将其与数组中的分数进行比较,然后根据其排名将其相加。 It's not working. 没用 My print statement, which prints for each input line, is just printing the score of that individual line (provided it is non-zero). 我的打印语句(为每个输入行打印)只是打印该行的分数(前提是它不为零)。 Could I please get some help? 我可以帮忙吗?

void
stage3(double Score, line_t * linePtr) {
    int j = 0;

    line_t line;
    size_t maxSz = MAX_LINELEN;
    int scorecmp(int j, double Score, line_t * linePtr);

        if (Score != 0 ) {
            if (j < TOP_SCORING_MAX) {
                scorecmp(j, Score, linePtr);
                j++;
            } /* fill up array */

            else {
                /* compare with last element
                  if greater than last element, check against
                  every element, moving it down while the thing
                  is bigger
                  when it is less than element, put it in that gap
                  */
        }
        }
    }   

This is second function 这是第二个功能

int
scorecmp(int j, double Score, line_t * linePtr) {
    line_t bestmatch[TOP_SCORING_MAX];
    line_t line;

    if (j == 0) {

        bestmatch[j].score = Score;
        bestmatch[j].index = linePtr->index;
        bestmatch[j].buf = linePtr->buf;

    }
    else if (line.score > bestmatch[j-1].score) {
        bestmatch[j].score = bestmatch[j-1].score;
        bestmatch[j].index = bestmatch[j-1].index;
        bestmatch[j].buf = bestmatch[j-1].buf;
        bestmatch[j-1].score = Score;
        bestmatch[j-1].index = linePtr->score;
        bestmatch[j-1].buf = linePtr->buf;
    }
    else if (line.score <= bestmatch[j-1].score) {

        bestmatch[j].score = Score;
        bestmatch[j].index = linePtr->index;
        bestmatch[j].buf = linePtr->buf;
        }


        printf("best match = %f\n",bestmatch[0].score);
return 0;   
}

When I've finished this, I then need to compare any additional lines to the bottom scoring one in the array. 完成此操作后,我需要将所有其他行与数组中得分最低的行进行比较。 If it is larger, I then need to compare it with every position in array until it finds its place. 如果它更大,则需要将其与数组中的每个位置进行比较,直到找到它的位置为止。

Thank you 谢谢

Here is the definition of line_t 这是line_t的定义

typedef struct line_t 
{
  char* buf;
  int lineLength;   
  int wordCount;
  int index;
  double score;
} line_t;

In your code, bestmatch[] is a local array. 在您的代码中,bestmatch []是一个本地数组。 so, it's expired when each step is finished. 因此,每个步骤完成后,它就会过期。
According to your algorithm, bestmatch[] should be keeped. 根据您的算法,应保留bestmatch []。

For solving this problem, you have two methods. 为了解决此问题,您有两种方法。

Simple method is that you define bestmatch[] to global variable. 一种简单的方法是将bestmatch []定义为全局变量。
Prefered method is that you define bestmath[] in specific function like stage3() or pervious caller function and pass bestmath[] to scorecmp(). 首选方法是,在诸如stage3()或以前的调用方函数之类的特定函数中定义bestmath []并将bestmath []传递给scorecmp()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM