C++快速排序向量并保留原始索引号
C++ QuickSorting a vector and keeping the original index numbers
我有一个有效的快速排序函数,但我不确定如何保留未排序数据的原始索引号。有什么想法吗?谢谢!这是我的函数。我可以以某种方式将一对合并在那里吗?
double NearestNeighbor::partition(vector<double>& theList, double start, double end) {
int pivot = theList[end];
int bottom = start - 1;
int top = end;
bool notdone = true;
while (notdone) {
while (notdone) {
bottom += 1;
if (bottom == top) {
notdone = false;
break;
}
if (theList[bottom] > pivot) {
theList[top] = theList[bottom];
break;
}
}
while (notdone) {
top = top - 1;
if (top == bottom) {
notdone = false;
break;
}
if (theList[top] < pivot) {
theList[bottom] = theList[top];
break;
}
}
}
theList[top] = pivot;
return top;
}
快速排序函数
double NearestNeighbor::quickSort(vector<double>& theList, double start, double end) {
if (start < end) {
double split = partition(theList, start, end); //recursion
quickSort(theList, start, split - 1);
quickSort(theList, split + 1, end);
} else {
return 0;
}
}
我已经计算了几个向量的点积,我正在尝试打印 10 个最近的邻居。我可以对它们进行排序,但我的教授被要求返回 10 个最近邻居的索引号,所以我试图弄清楚如何保留这些原始索引号。
例如:排序后的数据可能如下所示:
索引号:3 45 15 9 45
数据: 10 14 17 30 35
我只想打印出索引号。抱歉,我不知道如何格式化它,以便数字与数据对齐,但我想你明白了。
- 将
std::vector<T>
转换为std::vector<std::pair<int, T>>
。 - 使用
pair
中的第二个对std::vector<std::pair<int, T>>
进行排序。
相关文章:
- 数组索引的值没有增加
- 芬威克树(BIT).找到具有给定累积频率的最小索引,单位为 O(logN)
- 查找最接近的大于当前数字的数字的索引
- 在C++中调整向量中的索引
- 重载元组索引运算符-C++
- 将浮动的heightmap数组导出为16位原始值
- 给定一个向量,如何找到该向量的所有子集和的原始索引
- 为std::string的某个索引赋值
- 并行用于C++17中数组索引范围内的循环
- 有没有办法从非C/C++文件中读取C++原始字符串文字的内容
- 如何在保留原始索引的同时对向量的向量进行排序
- 如何在跟踪原始索引的同时压缩向量(具有重复项)
- std::排序比较器,可以看到元素的(原始)索引
- 获取 CUDA 设备中的原始矩阵索引
- 正在将指针从数组值存储到原始数组索引位置
- 对二维矢量进行排序,保持对原始索引的跟踪
- 对n维点进行排序,并跟踪原始索引
- 原始数组期望整数作为索引
- 对数据结构进行排序并保存原始索引的记录
- 在保留原始索引的同时对值进行更快的排序