If you are writing C++ (as opposed to plain C) you should prefer the sorting algorithms that are part of the STL. It really doesn't get much simpler than using them.
Example:
Code:#include <algorithm> #include <functional> #include <iostream> int main( ) { const int arraySize = 5; float data[arraySize] = { 5.0f, 2.34f, 0.932f, 10.0f, 8.213f }; std::sort(data, data + arraySize, std::less<float>( )); std::cout << "Sorted least to greatest:" << std::endl; for (int i = 0; i < arraySize; ++i) std::cout << data[i] << std::endl; std::sort(data, data + arraySize, std::greater<float>( )); std::cout << "Sorted greatest to least:" << std::endl; for (int i = 0; i < arraySize; ++i) std::cout << data[i] << std::endl; return 0; }




Reply With Quote