2 然后我必须使用合并排序,之后 输出为 第一个元素是下标4 0 3 5 1 5 2 2 6 3 3 7 -1 4 0 6 5 4 1 6 1 7 b $ b 2 2 它适用于索引列,输入列,但不适用于链接。我在链接上搜索了我发现它们可以从sort函数中提取出来。现在我的问题是我正在使用的mergesort函数是否适用于此目的。缺什么? 我的尝试: Hi, I 'm working on this practice problem. In which I am testing my code using bash command with a text file. The file contains numbers the 1st number is the total of rest of the numbers. For example 1st number as a 4 means there are total 4 numbers in line. Example array length 83 5 6 7 0 4 1 2 Then I have to use merge sort and after that the output is First element is at subscript 4 0 3 5 1 5 2 2 6 3 3 7 -1 4 0 6 5 4 1 6 1 7 7 2 0 It works fine for indices column, input column, but not the links. I did my search on links I find out they can be extracted from sort function. Now my question is the mergesort functions I'm using are these proper for this purpose. What is missing? What I have tried:#include <stdio.h>#include <stdlib.h>#define MAX 50//Prototypesint binary(int a[],int n,int m,int l,int u);void partition(int **arr,int low,int high);void mergeSort(int arr[],int low,int mid,int high);int main(){ //Variable initialization int n, j, temp, i=0, low = 0, indx = 0; int *index, *input, *link, *sorted; scanf("%d ",&n); //Memory allocation input = (int*) malloc((n)* sizeof(int)); index = (int*) malloc((n)* sizeof(int)); link = (int*) malloc((n)* sizeof(int)); sorted = (int*) malloc((n)* sizeof(int)); //Error handeling if(input == NULL) { printf("Error! memory not allocated."); exit(0); } //Input for(i= 0; i < n; i++) { scanf("%d",&input[i]); index[i]=i; sorted[i]=input[i]; link[i]=-1; } partition(&sorted,0,n-1); // Finding minimum value low=sorted[0]; for(i=0;i<n;i++){ if(input[i]==low) { indx=i; } } //Binary search for(i=0;i<n;i++){ temp= input[i]; for(j=0;j<n;j++){ if(sorted[j]==temp){ link[i]=j+1; printf("print link j+i %d\n", link[j]); if(link[i]==input[i]){ } } } } // Printing output printf("First element is at the subscript %d \n", indx); for(i=0;i<n;i++){ printf(" %d %d %d \n",index[i],input[i],link[i]); } /* for(i=0;i<n;i++) { printf("%d ",sorted[i]); }*/ //Memory deallocation free(index); free(input); free(link); return 0; }void partition(int **arr,int low,int high){ int mid; if(low<high){ mid=(low+high)/2; partition(arr,low,mid); partition(arr,mid+1,high); mergeSort(*arr,low,mid,high); }}void mergeSort(int arr[],int low,int mid,int high){ int i,m,k,l,temp[MAX]; l=low; i=low; m=mid+1; while((l<=mid)&&(m<=high)){ if(arr[l]<=arr[m]){ temp[i]=arr[l]; l++; } else{ temp[i]=arr[m]; m++; } i++; } if(l>mid){ for(k=m;k<=high;k++){ temp[i]=arr[k]; i++; } } else{ for(k=l;k<=mid;k++){ temp[i]=arr[k]; i++; } } for(k=low;k<=high;k++){ arr[k]=temp[k]; }} 推荐答案 有一个工具可以让你看到你的代码正在做什么,它的name是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。 当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器。 使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第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. 这篇关于C如何以递归的merg-sort动态数组递归扫描链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-02 19:10