我如何使这个排序算法发挥作用?
我正在尝试尝试一些迭代排序算法,我正在努力使这个算法发挥作用。当我在 20 个或更少的小列表上使用它时,实际上没问题,但是当我使用 200 个或更多元素时,它会给我一个运行时错误(未处理异常)。我猜这是一个记忆问题,但我真的不知道如何继续。
我想要得到的结果是一个函数,它接受一个列表,根据元素是否大于或小于平均值将其分成两部分。然后在创建的两个新列表上再次调用该函数。 当它需要一个空列表、只有一个元素或已经排序时,它不应该执行任何操作。
错误:ConsoleApplication4.exe 中 0x00007FFF05EE5469 (ucrtbased.dll) 处出现未处理的异常:0xC00000FD:堆栈溢出(参数:0x0000000000000001、0x000000B0AB403FF8)。
#include <list>
#include <iostream>
void mySort(std::list<int>& input_list) {
if (input_list.begin() != input_list.end()) {
std::list<int> listaMin;
std::list<int> listaMaj;
std::list<int>::iterator it;
bool alreadySorted = true;
int previousValue = 0;
int avg = 0, counter = 0;
for (it = input_list.begin(); it != input_list.end(); it++) {
if (*it >= previousValue)previousValue = *it;
else alreadySorted = false;
avg += *it;
counter++;
}
if (alreadySorted == false) {
for (it = input_list.begin(); it != input_list.end(); it++) {
if (*it < (avg / counter)) { listaMin.push_front(*it); }
else listaMaj.push_front(*it);
}
mySort(listaMin);
mySort(listaMaj);
input_list.clear();
input_list.merge(listaMaj);
input_list.merge(listaMin);
}
}
}
之后:
int main() {
srand(time(NULL));
std::list<int> mainList;
for (int i = 0; i < 100; i++) mainList.push_back(rand() % 100);
mySort(mainList);
return 0;
}
Im trying to experiment with some iterative sorting algorythms, im trying hard to make this one work. It is actually fine when i use it on small lists of 20 or less, but when i go up to 200 or more elements it gives me a runtime error (exception not handled). I guess it's a memory problem, but i dont really know how to proceed.
The result that i would like to get is a function that takes a list, splits it in two part depending if the elements are bigger or smaller than the average. Then the function is called again on both the new lists created.
When it takes a list that is either empty, with a single element, or already sorted, it should do nothing.
The error: Unhandled exception at 0x00007FFF05EE5469 (ucrtbased.dll) in ConsoleApplication4.exe: 0xC00000FD: Stack overflow (parameters: 0x0000000000000001, 0x000000B0AB403FF8).
#include <list>
#include <iostream>
void mySort(std::list<int>& input_list) {
if (input_list.begin() != input_list.end()) {
std::list<int> listaMin;
std::list<int> listaMaj;
std::list<int>::iterator it;
bool alreadySorted = true;
int previousValue = 0;
int avg = 0, counter = 0;
for (it = input_list.begin(); it != input_list.end(); it++) {
if (*it >= previousValue)previousValue = *it;
else alreadySorted = false;
avg += *it;
counter++;
}
if (alreadySorted == false) {
for (it = input_list.begin(); it != input_list.end(); it++) {
if (*it < (avg / counter)) { listaMin.push_front(*it); }
else listaMaj.push_front(*it);
}
mySort(listaMin);
mySort(listaMaj);
input_list.clear();
input_list.merge(listaMaj);
input_list.merge(listaMin);
}
}
}
Later:
int main() {
srand(time(NULL));
std::list<int> mainList;
for (int i = 0; i < 100; i++) mainList.push_back(rand() % 100);
mySort(mainList);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是您在计算用于将列表分成两部分的平均值时使用整数除法。
将元素视为包含 3 个元素 { 1, 2, 1 } 的列表。
这将导致
avg = 4
和counter = 3
。然后,根据每个元素是
<
还是>=
avg / counter
,将列表分成两部分。在本例中,
avg / counter = 4 / 3 = 1
(使用整数除法)由于所有 3 个元素 >= 1,最终会得到包含所有 3 个元素的
listaMaj
elements 和 listaMin 包含 0。调用
mysort(listaMaj)
会导致无限递归,因为同一个列表实际上会永远不变地传递。将
avg
变量定义为double
而不是int
可以解决此问题。Your problem is that you are using integer division in calculating the average used to split your list into 2 parts.
Consider elements a list of 3 elements { 1, 2, 1 }.
This results in
avg = 4
andcounter = 3
.You then split the list into two depending on whether each element is
<
or>=
avg / counter
.In this case,
avg / counter = 4 / 3 = 1
(using integer division)With all 3 elements being >= 1, you end up with
listaMaj
containing all 3 elements andlistaMin
containing 0.The call to
mysort(listaMaj)
then results in infinite recursion as the same list is effectively passed on unchanged forever.Defining your
avg
variable asdouble
instead ofint
, would fix this.