C++堆排序算法在4100多个数据条目后崩溃
C++ heap sort algorithm crashing after 4100+ data entries?
我已经开发了以下堆排序算法代码,但由于某种原因,它一直工作得很好,直到某个特定区域(大约4100区域),之后程序被迫关闭?
如有任何帮助,我们将不胜感激,谢谢。
void heap_from_root(MVector &v, int i, int n) {
int end=n,j=0;
// Identify the lowest root and how many sons it has. If it only has one son, set j=1.
if (n==1) { n = 0; j = 1; }
else if ((n-2) % 2 == 0) { n = (n-2)/2; }
else if ((n-1) % 2 == 0) { n = (n-1)/2; j=1; }
while (i <= n) { // Start from the lowest root, and go up until the highest root.
if (j==0) { // If 2 sons, then check for the biggest value. If the biggest value is greater than root, exchange.
if (v[2*n+1] > v[2*n+2] && v[2*n+1] > v[n]) { v.swap(2*n+1,n); }
if (v[2*n+2] > v[2*n+1] && v[2*n+2] > v[n]) { v.swap(2*n+2,n); }
}
if (j==1) { // If 1 son, if the son's value is greater than the root, exchange.
if (v[2*n+1] > v[n]) { v.swap(2*n+1,n); }
j=0; // As only the last root can only have one son, set j to 0.
}
n--; // Go backwards to the next root.
}
/* The top value of the heap will now be the biggest value. If the heap hasn't been completed sorted,
put the biggest value at the END of the heap, and restart the algorithm, this time excluding that last
value. Repeating this recursively will mean the heap will be sorted in ascending order. This saves using
significant excess memory. */
if (i < end) { v.swap(i,end); end--; heap_from_root(v,i,end); }
}
void heap(MVector &v) { heap_from_root(v,0,v.size()-1); }
MVector类为:
// class MVector contains arrays that can work with doubles
class MVector
{
// storage for the new vector class
vector<double> v;
public:
// constructor
explicit MVector(){}
explicit MVector(int n):v(n){srand(static_cast<unsigned>(time(NULL)));}
explicit MVector(int n,double x):v(n,x){}
// equate vectors;
MVector& operator=(const MVector& X)
{if(&X==this)return *this;v=X.v;return *this;}
// access data in vector
double& operator[](int index){ return v[index]; }
// access data in vector (const)
double operator[](int index) const { return v[index]; }
// size of vector
int size() const {return v.size();}
void push_back(double x){v.push_back(x);}
void swap(int i,int j) { double c; c=v[i]; v[i] = v[j]; v[j] = c; }
void initialise_random(double xmin, double xmax) {
const unsigned n = this->size();
for(unsigned i=0;i<n;i++) {
v[i] = xmin + rand()*(xmax - xmin) / static_cast<double>(RAND_MAX);
}
}
bool cmp(int i, int j) {
if (v[i] < v[j]) { return true; }
else { return false; }
}
}; // end class MVector
它是由发起的
int main ()
{
MVector x(4500);
x.initialise_random(0,10);
double y0 = timer();
Sort::heap(x);
double y = timer();
std::cout << abs(y-y0) << endl;
}
不是一个答案,而是一个观察。你有这个代码:
if (n==1) { n = 0; j = 1; }
else if ((n-2) % 2 == 0) { n = (n-2)/2; }
else if ((n-1) % 2 == 0) { n = (n-1)/2; j=1; }
考虑当n
是奇数时,n/2 == (n-1)/2
。当n
为偶数时,(n-1)/2 == (n-2)/2
。因此,您可以将所有代码替换为:
if ((n % 2) == 1) { j = 1; }
n = (n-1)/2;
相关文章:
- 为什么当我尝试搜索双链表中第一个数据条目之外的数据时,程序崩溃了?
- libCURL:上传数据崩溃(Qt,SMTP)
- Arangoimp会随机崩溃到Arangodb的数据导入
- 程序使用FSCANF读取文件数据后崩溃
- 我的C++程序在尝试输出从文件中读取的数据时崩溃
- Apache 崩溃并显示错误 R6016 没有足够的空间来存储线程数据
- 双重删除数据不会崩溃
- 将值分配给Float数据类型崩溃程序
- AVX:数据对齐:存储崩溃、存储、加载、加载你不
- 代码导致数据结构操作崩溃
- Google protobuf在解析字符串数据时在Android中崩溃
- Visual Studio 2013 因非静态数据成员初始化而崩溃
- 当JPEG数据不正确时,libjpeg解码崩溃
- C++堆排序算法在4100多个数据条目后崩溃
- 将数据流式传输到 std::vector<double> 崩溃
- 在数组中设置数据时程序崩溃
- 想要创建一个将数据存储在内存中的应用程序.但我不希望数据丢失,即使我的应用程序崩溃
- 调用不访问 null 对象指针上数据的方法有时有效,有时崩溃。为什么它不总是崩溃?
- 声明SDL_Rect数据成员在启动时崩溃,找不到原因
- 当绕过c++中的函数从目标进程的ExtTextOut复制数据字符串时,malloc会导致崩溃吗?