Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Insertion vs Sort
#include <vector> #include <string> #include <iostream> #include <iomanip> #include <algorithm> #include <chrono> class muTimer { using Clock = std::chrono::high_resolution_clock; bool active = false; Clock::duration duration_; Clock::time_point start_ = Clock::now(), stop_ = Clock::now(); muTimer(const muTimer&) = delete; muTimer& operator=(const muTimer&) = delete; public: using ns = std::chrono::nanoseconds; using mks = std::chrono::microseconds; using ms = std::chrono::milliseconds; muTimer() { reset(); start(); } ~muTimer() = default; muTimer& reset() { duration_ = std::chrono::nanoseconds(0); active = false; return *this; } muTimer& start() { if (!active) { start_ = Clock::now(); active = true; } return *this; } muTimer& stop() { if (active) { stop_ = Clock::now(); duration_ += stop_ - start_; active = false; } return *this; } template<typename T = mks> unsigned long long duration() { return static_cast<unsigned long long> (std::chrono::duration_cast<T>(stop_-start_).count()); } }; using namespace std; template<typename Iter, typename Less = less<iterator_traits<Iter>::value_type>> void InsertionSort(Iter b, Iter e, Less c = Less()) { typedef typename iterator_traits<Iter>::value_type Value; Iter i = b; for(++i; i != e; ++i) { Value x = *i; Iter j = i, k = i; for(--j; (k!=b) && c(x,*j); --j, --k) *k = *j; *k = x; } } int main() { vector<int> v; v.reserve(1000000); for(int i = 0; i < 1000000; ++i) v.push_back(i); for(int i = 0; i < 1000; ++i) { int j = rand(); swap(v[j],v[j+10]); } vector<int> w = v; { muTimer mt; sort(v.begin(),v.end()); mt.stop(); cout << mt.duration<>() << " mks\n"; } { muTimer mt; InsertionSort(w.begin(),w.end()); mt.stop(); cout << mt.duration<>() << " mks\n"; } cout << (v == w) << endl; }
run
|
edit
|
history
|
help
0
Not an overflow
Problem_onoff_3
operator/function lookup
default
problem_soultion2
LinkedList
infix to postfix v 1.0
MyCodeWindows
pitch errors verification for Nowhk
hgh