insertion-sort

how do you insert the value in a sorted vector?

耗尽温柔 提交于 2019-11-27 04:02:18
ALL, This question is a continuation of this one . I think that STL misses this functionality, but it just my IMHO. Now, to the question. Consider following code: class Foo { public: Foo(); int paramA, paramB; std::string name; }; struct Sorter { bool operator()(const Foo &foo1, const Foo &foo2) const { switch( paramSorter ) { case 1: return foo1.paramA < foo2.paramA; case 2: return foo1.paramB < foo2.paramB; default: return foo1.name < foo2.name; } } int paramSorter; }; int main() { std::vector<Foo> foo; Sorter sorter; sorter.paramSorter = 0; // fill the vector std::sort( foo.begin(), foo.end

Why is Insertion sort better than Quick sort for small list of elements?

允我心安 提交于 2019-11-26 13:51:08
问题 Isnt Insertion sort O(n^2) > Quick sort O(nlogn)...so for a small n, wont the relation be the same? 回答1: Big-O Notation describes the limiting behavior when n is large, also known as asymptotic behavior. This is an approximation. (See http://en.wikipedia.org/wiki/Big_O_notation) Insertion sort is faster for small n because Quick Sort has extra overhead from the recursive function calls. Insertion sort is also more stable than Quick sort and requires less memory. This question describes some

how do you insert the value in a sorted vector?

醉酒当歌 提交于 2019-11-26 10:58:04
问题 ALL, This question is a continuation of this one. I think that STL misses this functionality, but it just my IMHO. Now, to the question. Consider following code: class Foo { public: Foo(); int paramA, paramB; std::string name; }; struct Sorter { bool operator()(const Foo &foo1, const Foo &foo2) const { switch( paramSorter ) { case 1: return foo1.paramA < foo2.paramA; case 2: return foo1.paramB < foo2.paramB; default: return foo1.name < foo2.name; } } int paramSorter; }; int main() { std: