I have read that insert operation in a set takes only log(n) time. How is that possible?
To insert, first we have find the location in the sorted array where the new element must sit. Using binary search it takes log(n). Then to insert in that location, all the elements succeeding it should be shifted one place to the right. It takes another n time.
My doubt is based on my understanding that set is implemented as an array and elements are stored in sorted order. Please correct me if my understanding is wrong.
std::setis implemented as a sorted array. Why? - Konrad RudolphHashSetor aTreeSet(which is aSortedSet), and I doubt there is an ArraySet available. (Note that unmodifiable sets can efficiently be implemented as a sorted array, using binarySearch - so actually as a sorted tree stored in an array) - Has QUIT--Anony-Mousse