you sort the data.
c++ contains a sort, std::sort() which will just do it for you and is usually the best choice.
c++ also contains std::swap(). You wrote that, and it would be smart to move it to its own function as you will need it in most sorting algorithms, esp the simple ones.
you can learn about some sorts here:
my list of must-know sorts:
- bubble (getting started, its the simplest, and slowest, way to sort. You can skip it, but everyone feels compelled to teach it)
- insertion (slightly better than bubble, and equally easy to understand, and below)
- shellshort (very good, slightly modified insertion sort, easy to code, difficult to understand)
- counting sort (fastest but only works for specific problems. you can often force your data to fit the specific problem, though, with some creativity).
- introsort (current best approach in general: std::sort uses this one. It is advanced, hard to code correctly, hard to understand, uses recursion)
The rest you can poke around at if you are interested.