algo.h指针数组总是产生一个连出索引C++
#ifndef _algorithm_
#define _algorithm_
namespace Sorting{
int *bubble(int *dataPass, int dataSize);
}
#endif
algo.cpp的
#include "algorithm.h"
int *Sorting::bubble(int *dataPass, int dataSize){
for(int i = 0; i < dataSize; i++){
for(int j = 0; j < dataSize; j++){
cout<<dataPass[j+1]<<" < "<<dataPass[j]<<endl;
if(dataPass[j+1] < dataPass[j]){
int tmp = dataPass[j];
dataPass[j] = dataPass[j+1];
dataPass[j+1] = tmp;
}
}
}
return dataPass;
}
我想使分选库和我开始冒泡排序,显然我面临一个问题。例如我给了属性dataPass 数组[5] = {7,2,5,8,3}和dataSize 5.此函数执行完返回数组后1,2,3,5,7哪里有我做错了?该数组没有1,但排序显示1,我想到8推到索引5,所以我的数组变成了6,我不知道发生了什么。
解决此类问题的正确工具是您的调试器。在*堆栈溢出问题之前,您应该逐行执行您的代码。如需更多帮助,请阅读[如何调试小程序(由Eric Lippert撰写)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。至少,您应该\编辑您的问题,以包含一个[最小,完整和可验证](http://stackoverflow.com/help/mcve)示例,该示例再现了您的问题,以及您在调试器。 –
请仔细检查数组索引。 – molbdnilo
只是一点提示:如果'j = dataSize-1',数组索引'j + 1'的指向元素是什么? –