big-o

Generic and practical sorting algorithm faster than O(n log n)?

北城余情 提交于 2020-08-22 04:27:05
问题 Is there any practical algorithm for generic elements (unlike counting sort or bucket sort) that runs faster than O(n log n)? 回答1: Many people have mentioned the information-theoretic Ω(n lg n) bound on comparison sorting algorithms, which can't be broken in comparison sorts. (This earlier question explores why that's the case.) However, there are some types of comparison sorts that, while not breaking O(n lg n) in the average case, can be shown to run faster on inputs that are already

Time Complexity of Doubly Linked List Element Removal?

╄→гoц情女王★ 提交于 2020-08-21 09:17:52
问题 A lot of what I'm reading says that removing an internal element in a doubly linked list (DLL) is O(1) ; but why is this the case? I understand why it's O(n) for SLLs; traverse the list O(n) and remove O(1) but don't you still need to traverse the list in a DLL to find the element? 回答1: For a doubly linked list, it's constant time to remove an element once you know where it is. For a singly linked list, it's constant time to remove an element once you know where it and its predecessor are.

Time complexity of a Priority Queue in C++

蓝咒 提交于 2020-08-21 05:22:00
问题 Creating a heap takes O(n) time while inserting into a heap (or priority queue) takes O(log(n)) time. Taking n inputs and inserting them into the priority queue, what would be the time complexity of the operation? O(n) or O(n*log(n)). Also, the same result would hold in case of emptying the entire heap too (i.e. n deletions), right? 回答1: If you have an array of size n and you want to build a heap from all items at once, Floyd's algorithm can do it with O(n) complexity. See Building a heap.

Time complexity of a Priority Queue in C++

雨燕双飞 提交于 2020-08-21 05:21:38
问题 Creating a heap takes O(n) time while inserting into a heap (or priority queue) takes O(log(n)) time. Taking n inputs and inserting them into the priority queue, what would be the time complexity of the operation? O(n) or O(n*log(n)). Also, the same result would hold in case of emptying the entire heap too (i.e. n deletions), right? 回答1: If you have an array of size n and you want to build a heap from all items at once, Floyd's algorithm can do it with O(n) complexity. See Building a heap.

Why is the complexity of BFS O(V+E) instead of O(E)? [duplicate]

社会主义新天地 提交于 2020-08-19 11:11:13
问题 This question already has answers here : Why is time complexity for BFS/DFS not simply O(E) instead of O(E+V)? (2 answers) Breadth First Search time complexity analysis (6 answers) Closed 9 days ago . This is a generic BFS implementation: For a connected graph with V nodes and E total number of edges, we know that every edge will be considered twice in the inner loop. So if the total number of iterations in the inner loop of BFS is going to be 2 * number of edges E , isn't the runtime going

Why is the complexity of BFS O(V+E) instead of O(E)? [duplicate]

雨燕双飞 提交于 2020-08-19 11:10:59
问题 This question already has answers here : Why is time complexity for BFS/DFS not simply O(E) instead of O(E+V)? (2 answers) Breadth First Search time complexity analysis (6 answers) Closed 9 days ago . This is a generic BFS implementation: For a connected graph with V nodes and E total number of edges, we know that every edge will be considered twice in the inner loop. So if the total number of iterations in the inner loop of BFS is going to be 2 * number of edges E , isn't the runtime going