swap function time complexity in cpp code example
Example 1: what is time complexity of swap function
The Behaviour is Equivalent to:
template <class T> void swap ( T& a, T& b )
{
T c(a); a=b; b=c;
}
Syntax : Swap(a,b) // a = Number 1 , b = Number 2
Time Complexity: It makes one constructions and one assignments
So, Linear O(n) time.
Example 2: swap two elements of a vector
std::iter_swap(arr.begin()+pos1,arr.begin()+pos2);