掌握调试Visual Studio 2010 - 初学者指南 [ ^ ] 使用Visual Studio 2010进行基本调试 - YouTube [ ^ ] 1.11 - 调试程序(步进和断点)|学习C ++ [ ^ ] 调试器仅显示您的代码正在执行的操作,您的任务是与应该执行的操作进行比较。 #include <iostream>using namespace std;void merge(int arr[], int p, int q, int r){int a = q - p + 1;int b = r - q;int left[a];int right[b];for(int i=0;i<a;i++){left[i] = arr[p + i];}for(int j=0;j<b;j++){right[j] = arr[q + j + 1];}int i=0;int j=0;int k=1;while(i < a && j < b){if(left[i] <= right[j]){arr[k] = left[i];i++;k++;}else{arr[k] = right[j];j++;k++;}}while(i < a){arr[k] = left[i];i++;k++;}while(j < b){arr[k] = right[j];j++;k++;}}void mergeSort(int arr[], int p, int r){if(p < r){int q = p + (r-p)/2;mergeSort(arr, p, q);mergeSort(arr, q+1, r);merge(arr, p, q, r);}}int main() { int arr[] = {12, 11, 13, 5, 6, 7}; int arr_size = sizeof(arr)/sizeof(arr[0]); printf("Given array is \n"); for(int i=0;i<arr_size;i++){ cout<<arr[i]<<" "; } mergeSort(arr, 0, arr_size - 1); printf("\nSorted array is \n"); for(int i=0;i<arr_size;i++){ cout<<arr[i]<<" "; } return 0; }What I have tried:I can't seem to figure out what's wrong with it. 解决方案 Quote:Can anyone tell me, what's wrong with this merge sort code! I'm new to programming and can't figure it out.With the debugger, you will watch your code performing, give it a try, it is a great learning tool.Your code do not behave the way you expect, or you don't understand why !There is an almost universal solution: Run your code on debugger step by step, inspect variables.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 know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.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[^]1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]The debugger is here to only show you what your code is doing and your task is to compare with what it should do. 这篇关于任何人都可以告诉我,这个合并排序代码有什么问题!我是编程新手,无法弄明白。先感谢您!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-01 10:12