: , . , .
, , , . , , , , .
, , .
i a[i].- , ,
a[i] - ( , ),
a[i] a[j], . a[i] , ...- , (: ?) ,
a[i], . a[i] , , .
, . , - . "", , .
... - / ?
, . O (n), n. , O (N ^ 2) , . ; .
++
,
template<typename Iter>
void insertion_sort(Iter first, Iter last)
{
for (Iter it = first; it != last; ++it)
std::rotate(std::upper_bound(first, it, *it), it, std::next(it));
}
, ( ), ++, , : std::upper_bound std::rotate.
std::upper_bound . , , , (*it). - O (logN), , O (n).
, std::rotate , , . :
0 1 2 3 5 6 4
^ ^ * these will be rotated right one element
0 1 2 3 5 6
4
0 1 2 3 5 6
4
0 1 2 3 4 5 6
, .
, - , - . , , :
.