#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/// Prints content of the vector
/// \param array - vector to prints content of
void printVector(vector<int> array) {
for (int el : array) {
cout << el << " ";
}
cout << endl;
}
/// Compute absolute value of given integer
/// \param a - integer to compute an absolute value of
/// \return Absolute value of a
int abs(int a) {
if (a < 0) {
return -a;
} else {
return a;
}
}
/// Compare two elements using their absolute values
/// \param a - first value
/// \param b - second value
/// \result true if absolute value of a is lower than absolute value of b, false otherwise
bool compare(int a, int b) {
return abs(a) < abs(b);
}
int main() {
vector<int> array;
array.push_back(5);
array.push_back(-2);
array.push_back(-6);
array.push_back(12);
array.push_back(0);
cout << "Sorting array in ascending order" << endl;
sort(array.begin(), array.end());
printVector(array);
cout << "Sorting array in descending order" << endl;
sort(array.rbegin(), array.rend());
printVector(array);
cout << "Sorting array using custom comparing function" << endl;
sort(array.begin(), array.end(), compare);
printVector(array);
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8YWxnb3JpdGhtPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCi8vLyBQcmludHMgY29udGVudCBvZiB0aGUgdmVjdG9yCi8vLyBccGFyYW0gYXJyYXkgLSB2ZWN0b3IgdG8gcHJpbnRzIGNvbnRlbnQgb2YKdm9pZCBwcmludFZlY3Rvcih2ZWN0b3I8aW50PiBhcnJheSkgewogICAgZm9yIChpbnQgZWwgOiBhcnJheSkgewogICAgICAgIGNvdXQgPDwgZWwgPDwgIiAiOwogICAgfQoKICAgIGNvdXQgPDwgZW5kbDsKfQoKLy8vIENvbXB1dGUgYWJzb2x1dGUgdmFsdWUgb2YgZ2l2ZW4gaW50ZWdlcgovLy8gXHBhcmFtIGEgLSBpbnRlZ2VyIHRvIGNvbXB1dGUgYW4gYWJzb2x1dGUgdmFsdWUgb2YKLy8vIFxyZXR1cm4gQWJzb2x1dGUgdmFsdWUgb2YgYQppbnQgYWJzKGludCBhKSB7CiAgICBpZi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