Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 months ago.
Improve this question
After learning bubble sorting, I learned other sorting.
And then I thought, there are other good sorting, do we need bubble sorting?
In the worst case, the Time Complexity is O(n²)
Even in the best case, Time Complexity is O(n²)
Is there a reason why need this kind of bubble sorting?
Bubble sort is easy to understand and implement. It is not very efficient, but it can be useful in certain situations, such as when the data is nearly sorted or when the input is very small.
Bubble sort can be easily adapted to sort lists of data that are stored in other data structures, such as linked lists
Bubble sort is a stable sorting algorithm, which means that it preserves the relative order of items with equal keys.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I was solving a question in codeforces,which was about insertion sort,
the link:the problem
I am unable to solve the question the editorial says
old - 2 * (di, ai + dj, aj - di, aj - dj, ai) - 1
I am not understanding the solution kindly help!
here is the editorial link:
https://codeforces.com/blog/entry/9584
For an insertion sort into a linked list, the number of "swaps" is maximum of 2, if you consider inserting a new item as "swapping pointers". The minimal number would be zero, if the key already exists; otherwise you'll swap one pointer when inserting before the head or after the tail.
The concept of an insertion sort is to keep the container sorted. Most of the time is spent searching for the proper place in the container to insert the new datum.
For an array container, you would need to move elements in order to make room for the new data; which doesn't involve swapping.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Somebody told me yesterday that the underlying structure of an ordered map is a binary search tree. This does not make sense to me since you cannot have O(1) retrieval if that were the case. Can anyone explain?
Also, if one were to implement a hash table in C++ without using the stdlib, what would be the best way to do so?
std::map lookup time is not O(1) its O(log(n)).
std::unordered_map has a lookup time of O(1) amortized.
std::unordered_map and std::unordered_set are hashtables.
The underlying data structure is implementation-defined. It is most commonly implemented as a Red-Black tree which is a self-balancing binary search tree. The time complexity for getting an element is O(logn) (see this)
I would just read the implementation of std::unordered_map as a starting point. I assume this is learning activity so reading and understanding working STL implementation would be a good exercise. If it's not an exercise then use std::unordered_map
std::map uses Red-Black tree as it gets a reasonable trade-off between the complexity of node insertion/deletion and searching.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Yesterday, this question came in my mind. Although I have neither read all the sorting algorithms like Quicksort, Merge Sort, Heapsort
Insertion Sort,
Selection Sort, and
Bubble Sort nor I have read the Introduction to Algorithms by CLRS but still, I am curious to know why there is a need to learn all such algorithms when the pre-defined sort function is already available to us in many languages.
Because
Simply sorting only may not be always the requirement. The requirement can be different. You may need to modify / integrate a sorting algorithm in order to develop a completely different thing.
The predefined sorting methods may not be the efficient at all cases.
Its always not about the sorted result but the approach of sorting in order to improve time and space complexity. Efficiency is the key.
There is no particular algorithm that is guaranteed to work best at all cases. Pros and cons may differ for different algorithms.
Need to understand which algorithm to be applied at what scenarios.
Sorting may not always done with numbers. It can be applied on other different complex types / structures. (There may not be pre-defined methods for complex cases )
There is always scope for a better approach.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Which of the following containers have average search by value complexity equal to O(log(n))?
Which ones have O(n) and O(n^2)?
std::vector
std::list
std::deque
std::set
std::multiset
std::unordered_set
std::unordered_multiset sorted
std::vector sorted
std::list sorted
std::deque sorted
The standard specifies the complexity of the relevant functions. Get a copy, or read a good reference. (See e.g. http://en.cppreference.com/w/cpp/container/unordered_set/find.)
They're all pretty much what you'd expect.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I saw this "Hopeless challenge" about priority queues in an algorithms book :
" O(1) insert, delete-min and decrease-key. Why is it impossible?"
Is it because the only way is to implement it with some sort of heap and heaps always take logn time to to delete-min (even if amortized)?
I assume the well-known fact that sorting n integers requires time n * log(n) and I assume that delete-min actually finds the minimum (and could, for instance return it).
Toward a contradiction, suppose we have a data structure such as the one you described. Then, in order to sort n integers, we first insert all of them into the data structure, thus taking time O(n). We then repeatedly delete-min until the structure is empty. This gives us the sorted integers in time O(n), giving us a contradiction. Therefore, that data structure cannot exist.