以下代码正是如此。但有一个问题,当矩阵的长度超过一定值时,它需要很长时间才能完成。
有没有可能让这段代码运行得更快一些?我真的无法想到任何方法了。max_index和min_index通常也是天真地实现。
对于N = 100万个项目,相对来说还可以,大约需要1.0-1.5分钟,但如果我需要像N = 1000万或更多的项目,则在我的笔记本电脑上永远无法完成。
while (1) {
int mini = max_index(other);
int maxi = min_index(data);
if (other[mini] > data[maxi]) {
int temp = other[mini];
other[mini] = data[maxi];
data[maxi] = temp;
} else {
break;
}
}
举个例子以澄清:
other =
0.5308 0.5458 0.8090 0.8063 0.8874
data =
0.2901 0.5497 0.9168 0.0882 0.7856
操作后:
other =
0.5308 0.5458 0.2901 0.5497 0.0882
data =
0.8090 0.8063 0.9168 0.8874 0.7856
std::sort
。 - Thomas Matthews