C++ 快速排序算法不起作用
我正在为一个哈夫曼编码项目开发一个快速排序算法(解释了为什么所有函数名都以哈夫开头)。当使用调试器遍历它时,当查找最高项时(当尝试从向量右侧查找“不应该”在该侧的项时),函数似乎冻结。这段代码可能(可能)还有其他问题,但我现在主要关注这一点。顺便说一句,我大部分时间(一直)调用cout,这是为了调试 编辑:注释中对我的代码做了很多修改,但没有一个能解决我的问题。因此,我正在更新代码C++ 快速排序算法不起作用,c++,sorting,vector,while-loop,quicksort,C++,Sorting,Vector,While Loop,Quicksort,我正在为一个哈夫曼编码项目开发一个快速排序算法(解释了为什么所有函数名都以哈夫开头)。当使用调试器遍历它时,当查找最高项时(当尝试从向量右侧查找“不应该”在该侧的项时),函数似乎冻结。这段代码可能(可能)还有其他问题,但我现在主要关注这一点。顺便说一句,我大部分时间(一直)调用cout,这是为了调试 编辑:注释中对我的代码做了很多修改,但没有一个能解决我的问题。因此,我正在更新代码 void huff_sort_partition(vector<Node*>* v, int b, i
void huff_sort_partition(vector<Node*>* v, int b, int e){
int tempt = b+((rand()%(e-b))+1);
int p_idx = (*v)[tempt]->weight;
cout << tempt << endl;
int l = b+0;
int r = e;
cout << "P:" << p_idx << " L-R:" << l << "-" << r << endl;
while(l < r){
while((*v)[l]->weight < p_idx){
l++;
}
while((*v)[r]->weight > p_idx){
r--;
}
Node* s = (*v)[b+l];
(*v)[b+l] = (*v)[b+r];
(*v)[b+r] = s;
}
huff_sort_partition(v, b, l-1);
huff_sort_partition(v, l+1, e);
}
void Huff::huff_sort(vector<Node*>* v){
srand ( time(NULL) );
cout << "------sort------" << endl;
huff_sort_partition(v, 0, v->size());
}
void huff\u sort\u分区(向量*v,int b,int e){
int-test=b+((rand()%(e-b))+1);
int p_idx=(*v)[试探]->重量;
您是否有使用和停止条件的问题。
b
是从何处开始分区的索引,e
是从何处停止分区的索引。
因此,当您第一次为e
调用函数时,必须引用上一个索引,而不是大小。
另外,您在huff\u sort\u分区中缺少停止条件
-为了不永远运行,您应该检查b
和e
索引相对于每个0是否正常
请尝试下面代码的固定版本
void huff_sort_partition(vector<Node*>* v, int b, int e){
if (b >= e ) {
return;
}
int p = (*v)[b+(rand() % (e - b + 1))]->weight;
int l = 0;
int r = e-b;
cout << "P:" << p << " L-R:" << l << "-" << r << endl;
while(l < r){
while((*v)[b+l]->weight < p){
l++;
}
while((*v)[b+r]->weight > p){
r--;
}
Node* s = (*v)[b+l];
(*v)[b+l] = (*v)[b+r];
(*v)[b+r] = s;
}
huff_sort_partition(v, b, b+l-1);
huff_sort_partition(v, b+r+1, e);
cout << "P:" << p << " L-R:" << l << "-" << r << endl;
for_each(v->begin(), v->end(), show_freq);
}
void Huff::huff_sort(vector<Node*>* v){
srand ( time(NULL) );
cout << "------sort------" << endl;
huff_sort_partition(v, 0, v->size() - 1);
}
void huff\u sort\u划分(向量*v,int b,int e){
如果(b>=e){
返回;
}
int p=(*v)[b+(rand()%(e-b+1))]->权重;
int l=0;
int r=e-b;
CUT考虑在代码中有多个具有枢轴权重的节点时发生的事情-为了简单起见,考虑权重<代码> [1, 9, 5,2, 7, 5,6, 8, 3,7 ] < /代码>,并希望枢轴索引为5,因此
void huff_sort_partition(vector<Node*>* v, int b, int e){
int p = (*v)[b+(rand() % (e - b + 1))]->weight;
l=0
和r=9
cout << "P:" << p << " L-R:" << l << "-" << r << endl;
while(l < r){
while((*v)[b+l]->weight < p){
l++;
}
7>5
,减量r
,r=8
,v[8]=3<5
。交换v[1]
和v[8]
,给出[1,3,5,2,7,5,6,8,9,7]
下一轮,l=1<8=r
v[1]=3<5
,l
变为2,v[2]=5
不小于5,循环结束。现在进入第二个内循环,v[8]=9>5
,v[7]=8>5
,v[6]=5
;v[5]=5
v[5
[5]
,给出[1,3,5,2,7,5,6,8,9,7]
下一轮,l=2<5=r
,v[2]=5
不小于5,v[5]=5
不大于5,交换v[2]
和v[5]
。哎呀,我们卡住了
防止这种情况发生的通常方法是交换枢轴,并使两个条件之一为弱不等式,还必须检查内部循环中的条件l
,或者在所有项都相等的情况下,一个将从数组/向量的末尾流出。然后在分区后,将枢轴交换到正确的位置e
以下代码使用标准方式(未经测试,可能存在打字错误):
void huff\u sort\u分区(向量*v,int b,int e){
//如果元素少于两个,则无需排序
if(e)重量;
//将枢轴换到一边
Node*tmp_Node=(*v)[试探];
(*v)[试探]=(*v)[e];
(*v)[e]=tmp_节点;
为什么你不能只用C++中的Q排序函数?@ ANIO主要原因是我是为了学习而不是为了工作而工作的。另一个原因是它是指针的向量(所以我也不能使用SoRT()).你为什么要做b-e+1而不是e-b+1@Yakov我没有注意到我的错误,因为我只是编写了代码并试图运行它,但遇到了这个问题(即使进行了更正,这个问题仍然存在)。您可以向qsort
传递一个函数,在比较对象之前,该函数会延迟指针。
cout << "P:" << p << " L-R:" << l << "-" << r << endl;
while(l < r){
while((*v)[b+l]->weight < p){
l++;
}
while((*v)[b+r]->weight > p){ // where it freezes up and wont move on
r--;
}
void huff_sort_partition(vector<Node*>* v, int b, int e){
// Nothing to sort if there are fewer than two elements
if (e <= b) return;
int tempt = b+((rand()%(e-b))+1);
int p_idx = (*v)[tempt]->weight;
// swap pivot out of the way
Node *tmp_Node = (*v)[tempt];
(*v)[tempt] = (*v)[e];
(*v)[e] = tmp_Node;
cout << tempt << endl;
int l = b;
int r = e - 1;
cout << "P:" << p_idx << " L-R:" << l << "-" << r << endl;
while(l < r){
while((l < r) && (*v)[l]->weight < p_idx){
l++;
}
while((l < r) && (*v)[r]->weight >= p_idx){
r--;
}
if (l < r){
Node* s = (*v)[l];
(*v)[l] = (*v)[r];
(*v)[r] = s;
// stuff at l and r is okay now, we don't need to test again
++l;
--r;
}
}
// Now l is the first index with weight >= pivot weight,
// swap pivot into place
tmp_Node = (*v)[l];
(*v)[l] = (*v)[e];
(*v)[e] = tmp_Node;
huff_sort_partition(v, b, l-1);
huff_sort_partition(v, l+1, e);
}