I have a class and a function that calculates a number based on various attributes of an object in the class
I want to sort a vector made up of objects from that class based on how high that number is.
I've made a basic bubble sort algorithm for it, but am currently calling the function that calculates the number twice per time through the for loop (once for each object getting checked). What suggestions would you have to optimize this as it's doing a whole ton of unnecessary work? Here's my thoughts of solutions that might work, but I'm not sure how I'd implement either:
-
make that number calculated just a member variable. issue: not sure how to make sure the number is recalculated before running the sorting algorithm as that number changes throughout the course of the code and the sorter can be run at (essentially) any time
-
calculate all the numbers first, then sort the numbers, and relate them to the object they were calculated from. issue: not sure how to relate them. I've done a bit of python and think I'd approach this problem with a dictionary, but not sure how I'd do this.
I'm happy to provide code if helpful, but if there's a conceptual answer I'll happily take it even if it means a bit of work on my part to figure out how to implement on my specific situation. Also sorry if my terminology is inconsistent/off, I'm in a c++ class and we only just learned about classes. Open to any other suggestions of how to approach this if mine are "bad" XD