最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

关于合并排序的C ++代码

SEO心得admin70浏览0评论
本文介绍了关于合并排序的C ++代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

执行时合并的c ++程序在输出中显示8个错误我认为它们都是数组的大小,因为它必须是const。执行时,数组除以一半,这意味着它会更改数组的大小。我不知道是不是这个原因,但我希望你能尽快帮助我。感谢 我的尝试: #include< iostream> 使用命名空间std; void merge(int a [],int low,int high,int mid) { cout<< 已输入合并<< endl; int n1 = high - low + 1; int n2 = mid - high; int L [n1 + 1] = 999; int R [n2 + 1] = 999; int i = 1,j = 1; for(int i = 1; i< = n1; i ++) { L [i] = a [low + i - 1]; } for(int j = 1; j< = n2; j ++) { R [j] = a [high + j ]; } for(int k = low; k< = mid; k ++) { if(L [i]< = R [j]) { a [k] = L [i]; i = i + 1; } 其他 { a [k] = R [j]; j = j + 1; } } } void mergesort(int a [],int low,int high) { if(low< high) { int mid; mid =(low + high)/ 2; mergesort(a,低,高); mergesort(a,mid + 1,high); merge(a,low,h igh,mid); } } void main() { int size = 7; int a [7] = {38,43,27,3,9,82,10}; int low = 0; int high = size - 1; mergesort(a,low,high); for(int i = 0;我< 7; i ++) { cout<< a [i]<< ; } cout<< endl; }

The c ++ program for the merge when executed shows me in the output 8 errors I think they are all of the size of the array because it must be const. When executing, the array divides by half, which means it changes the size of the Array. I do not know if that is the reason, but I want you to help me as soon as possible. Thank What I have tried: #include<iostream> using namespace std; void merge(int a[], int low, int high, int mid) { cout << "Entered merge" << endl; int n1 = high - low + 1; int n2 = mid - high; int L[n1 + 1] = 999; int R[n2 + 1] = 999; int i = 1, j = 1; for (int i = 1; i <= n1; i++) { L[i] = a[low + i - 1]; } for (int j = 1; j <= n2; j++) { R[j] = a[high + j]; } for (int k = low; k <= mid; k++) { if (L[i] <= R[j]) { a[k] = L[i]; i = i + 1; } else { a[k] = R[j]; j = j + 1; } } } void mergesort(int a[], int low, int high) { if (low < high) { int mid; mid = (low + high) / 2; mergesort(a, low, high); mergesort(a, mid + 1, high); merge(a, low, high, mid); } } void main() { int size = 7; int a[7] = { 38, 43, 27, 3, 9, 82, 10 }; int low = 0; int high = size - 1; mergesort(a, low, high); for (int i = 0; i < 7; i++) { cout << a[i] << " "; } cout << endl; }

推荐答案

想想你在这里做什么以及你应该做什么。 Think about what you do here and what you should. mergesort(a, low, high); mergesort(a, mid + 1, high); merge(a, low, high, mid);

有一个工具可以让你看到你的代码是什么正在做,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。 当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器。 使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。 调试器 - 维基百科,免费的百科全书 [ ^ ] 掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ] 使用Visual Studio 2010进行基本调试 - YouTube [ ^ ] 调试器在这里向您展示您的代码正在做什么,您的任务是与什么进行比较应该这样做。 调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality. When you don't understand what your code is doing or why it does what it does, the answer is debugger. Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute. Debugger - Wikipedia, the free encyclopedia[^] Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^] Basic Debugging with Visual Studio 2010 - YouTube[^] The debugger is here to show you what your code is doing and your task is to compare with what it should do. There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

发布评论

评论列表(0)

  1. 暂无评论