C++ QuickSorting a vector and keeping the original index numbers -
i have working quicksort function, i'm not sure how retain original index numbers of unsorted data. ideas? thanks! here's function. can incorporate pair in there somehow?
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;
}
//quicksort function
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; } }
i have calculated dot product of several vectors , i'm trying print 10 nearest neighbors. can sort them, professor asked return index number of 10 nearest neighbors, i'm trying figure out how retain original index numbers.
for example: sorted data might this:
index number:3 45 15 9 45
data: 10 14 17 30 35
i want print out index numbers. sorry, can't figure out how format numbers line data, think idea.
- convert
std::vector<t>
std::vector<std::pair<int, t>>
. - sort
std::vector<std::pair<int, t>>
using second ofpair
.
Comments
Post a Comment