这段代码有什么问题......我可以这样做 [英] What is problem in this code....can I go thrue this way

查看:86
本文介绍了这段代码有什么问题......我可以这样做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望通过两种方式使用两种方式来编程以进行三向合并排序,最小时间复杂度



我尝试过:



I want to wright programm for three way merge sort by using two way,with minimum time complexity

What I have tried:

#include <stdio.h>
#define max 10

int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };
int b[10];

void merging(int low, int mid1,int mid2, int high) {
   int l1, l2,l3, i;

  for(l1 = low, l2 = mid1 + 1,l3=mid2+1,i = low; l1 <= mid1 && l2 <= mid2,l3<=high; i++)
      {
      if((a[l1] <= a[l2])&&(a[l1]<=a[l3]))
         b[i] = a[l1++];
      else if((a[l2]<=a[l1])&&(a[l2]<=a[l3]))
         b[i] = a[l2++];
        else b[i]=a[l3++];
   }
   
   while(l1 <= mid1)    
      b[i++] = a[l1++];

   while(l2 <= mid2)   
      b[i++] = a[l2++];
   
   while(l3<=high)
      b[i++]=a[l3++];

   for(i = low; i <= high; i++)
      a[i] = b[i];
}

void sort(int low, int high) {
   int mid1,mid2;
   
   if(low < high) {
      mid1 = (low + high) / 3;
      mid2=2*(low+high)/3;
      sort(low, mid1);
      sort(mid1+1,mid2);
      sort(mid2+1, high);
      merging(low, mid1,mid2, high);
   } else { 
      return;
   }   
}

int main() { 
   int i;

   printf("List before sorting\n");
   
   for(i = 0; i <= max; i++)
      printf("%d ", a[i]);

   sort(0, max);

   printf("\nList after sorting\n");
   
   for(i = 0; i <= max; i++)
      printf("%d ", a[i]);
}

推荐答案

然后你有我们的许可。

但是......因为这是你的作业,你应该自己动手做,不要让我们为你修理它。



所以,它取决于你。 />
幸运的是,您有一个可用的工具,它可以帮助您了解正在发生的事情:调试器。你如何使用它取决于你的编译器系统,但是一个快速的谷歌用于你的IDE名称和调试器应该给你你需要的信息。



放一个断点在函数的第一行,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但是我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!
Then you have our permission.
But ... since this is your homework, you are expected to do it yourself, noit get us to "fix it" for you.

So, its going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


这需要很多时间来检查您的错误代码以及它是否应该做它应该做的事情。



但是有一个明显的错误:

你的 b 数组太小导致缓冲区溢出。它必须具有与 a 数组相同的11( max + 1)大小。
It would require a lot of time to check your code for errors and if it would do what it should.

But there is one obvious error:
Your b array is too small resulting in a buffer overrun. It must have the same size of 11 (max + 1) as the a array.


看起来你修改了这个程序: C中的合并排序程序 [ ^ ]。它看起来也是直线修改不起作用。我认为合并函数存在缺陷。你应该检查它的算法并修复它。
It looks you modified this program: Merge Sort Program in C[^]. It looks, also, that straight modification doesn't work. I think the merging function is flawed. You should possibly inspect its algorithm and fix it.


这篇关于这段代码有什么问题......我可以这样做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆