当我执行程序时出现错误的输出,我不知道可能是什么原因造成的。插入排序的错误输出
正如你可以在下面我有一个数组。然而在执行程序时,我得到的输出:
array[0]=3
array[1]=1
array[2]=1
array[3]=5
array[4]=5
array[5]=8
显然,这是不是我的预期输出这将是{} -1,3,4,5,7,8-。我究竟做错了什么?
#include <stdio.h>
#include <stdlib.h>
#define l 6
void isnertionSort(int array[]);
int main(void)
{
int array[l]={3,4,1,7,5,8};
for(int i=0; i<l;i++) {
printf("array[%d]=%d\n", i, array[i]);
}
printf("\n");
isnertionSort(array);
for(int i=0; i<l; i++){
printf("array[%d]=%d\n", i, array[i]);
}
return 0;
}
void isnertionSort(int array[])
{
int j,key;
for(int i =1;i<l;i++){
key = array[i];
j = i -1;
while(j>0 && array[j]>key){
array[j+1]=array[i];
j--;//j=j-1
}
array[j+1]= key;
}
}
请学习如何调试程序(例如[如何调试小方案](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/))。您应该学习如何使用调试器,和/或添加打印语句来识别错误。在SO上,您应该提供一些样本输入数据以及预期和实际输出(阅读如何创建MCVE - [MCVE])。 –
看起来你在移动时覆盖了数组中的元素。调试器是识别这些问题的好工具。 –
如果问题得到解决,请接受答案并收紧问题。您已收到类似的[建议](https://stackoverflow.com/questions/46758008/c-compiler-error-which-i-am-unable-to-locate#comment80461712_46758008)。请参阅[答案被接受时的含义是什么?](https://stackoverflow.com/help/accepted-answer) – BLUEPIXY