English 中文(简体)
Stable Merge, C
原标题:Stable Merge sort C

我正在撰写一份简单的合并功能,以根据一项指定职能(<>):

void merge(int left, int mid, int right, int(*compar)(const void *, const void *))
{
  // sublist sizes
  int left_size = mid - left + 1;
  int right_size = right - mid;

  // counts
  int i, j, k;

  // create left and right arrays
  B *left_list = (B*) malloc(left_size*sizeof(B));
  B *right_list = (B*) malloc(right_size*sizeof(B));

  // copy sublists, could be done with memcpy()?
  for (i = 0; i < left_size; i++)
    left_list[i] = list[left + i];

  for (j = 0; j < right_size; j++)
    right_list[j] = list[mid + j + 1];

  // reset counts
  i = 0; j = 0;

  for (k = left; k <= right; k++)
  {
    if (j == right_size)
      list[k] = left_list[i++];
    else if (i == left_size)
      list[k] = right_list[j++];
    // here we call the given comparision function
    else if (compar(&left_list[i], &right_list[j]) < 0)
      list[k] = left_list[i++];
    else
      list[k] = right_list[j++];
  }
}

void sort(int left, int right, int(*compar)(const void *, const void *))
{
  if (left < right)
  {
    // find the pivot point
    int mid = (left + right) / 2;

    // recursive step
    sort(left, mid, compar);
    sort(mid + 1, right, compar);

    // merge resulting sublists
    merge(left, mid, right, compar);
  }
}

然后,我几次使用不同的比较功能,在同一个“<><>>list/strong>阵列上打电话。 我发现,这种说法在第一次呼吁中是稳定的,但此后,我看到一些要素被放弃,即使它们平等。

谁能提出这种行为的理由?

最佳回答

我不敢肯定这样做,而是试图改变这一思路:

compar(&left_list[i], &right_list[j]) < 0

为此:

compar(&left_list[i], &right_list[j]) <= 0

这将使它能够做到,如果它们已经平等,它就会采取首批行动,(全心全意)维护稳定,而不是把事情环绕。

这只是一种猜测。

问题回答

我认为你的规模是错误的。

int left_size = mid - left;

And, as pointed by arasmussen, you need to give preference to the left list in order to mantain stability

compar(&left_list[i], &right_list[j]) <= 0

在接受所有这些要求时,你在填写求助人名单后,没有打电话。 这不会使算法回归不正确的结果,而是将造成你的方案记忆被用于每当你称之为某种功能时都不可逆地增长。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签