The situation is as follows:-
We have n number and we have print
them in sorted order. We have access
to balanced dictionary data structure,
which supports the operations serach,
insert, delete, minimum, maximum each
in O(log n) time.
We want to retrieve the numbers in
sorted order in O(n log n) time using
only the insert and in-order
traversal.
The answer to this is:-
Sort()
initialize(t)
while(not EOF)
read(x)
insert(x,t);
Traverse(t);
Now the query is if we read the elements in time "n" and then traverse the elements in "log n"(in-order traversal) time,, then the total time for this algorithm (n+logn)time, according to me.. Please explain the follow up of this algorithm for the time calculation.. How it will sort the list in O(nlogn) time??
Thanks.
Each insert is O(log n). You are doing n inserts, so that gives n * O(log n) = O(n log n) asymptotic time complexity. Traversing the tree is O(n), because there are n nodes. That adds up to O(n + n log n) which differs from O(n log n) by a constant, so the final asymptotic complexity is O(n log n)..
and then traverse the elements in "log n
Traversal is O(n), not O(log n). An insertion is O(log n), and you're doing n such insertions.
Related
For example, the tightest bound for Binary search is θ(logn), but we can also say it has O(n^2) and Ω(1).
However, I'm confused about if we can say something like "Binary search has a θ(n) bound" since θ(n) is between O(n^2) and Ω(1)?
The worst-case execution of binary search on an array of size n uses Θ(log n) operations.
Any execution of binary search on an array of size n uses O(log n) operations.
Some "lucky" executions of binary search on an array of size n use O(1) operations.
The sentence "The complexity of binary search has a Θ(n) bound" is so ambiguous and misleading that most people would call it false. In general, I advise you not to use the word "bound" in the same sentence as one of the notations O( ), Θ( ), Ω( ).
It is true that log n < n.
It is false that log n = Θ(n).
The statement log n < Θ(n) is technically true, but so misleading that you should never write it.
It is true that log n = O(n).
The "because" is wrong. Θ(n) is indeed compatible with O(n²) and Ω(1), but so is Θ(log n).
In the case of the dichotomic search, you can establish both bounds O(log n) and Ω(log n), which is tight, and summarized by Θ(log n).
You may not choose complexities "randomly", you have to prove them.
An array A of size n is known to be sorted except for the first k elements and last k elements where k is a constant. which of the following algorithms is best suited for sorting the array?
A) Quicksort
B) Bubble sort
C) Selection Sort
D) Insertion Sort
Given answer is D.
unable to understand how this works and also What would have been the answer if Merge sort is also given?
Let's have a look at the complexity of the algorithms:
A) Quicksort: would take worse case O(n²) average O(n log n)
B) Bubble Sort: would take O(n²)
C) Selection Sort: would take O(n²)
D) Insertion Sort: would take O(k* n) if k is constant = O(n)
So D has the best performance.
(for each of the k elements: O(log n) to find the position to insert to + O(n) to insert)
But since Quicksort is known to have a small konstant faktor and is in average O(n log n), it is most likely faster for "bigger" k values.
Extra:
E) merge sort : would take 2 * O(k log k) + O(n)
sort the k elements at the front O(k log k)
sort the k elements at the end O(k log k)
merge the 3 lists O(n)
Over all that makes for constant k O(n) so based on complexity the same as Insertion Sort.
But if you look at it with k not constant:
merge sort: O(k log k) + O(n)
Insertion Sort: O(k* n)
So insertion sort would be faster.
Arguments against merge sort:
In general merge sort is not inplace (insertion sort is), so you would need extra space or a very clever implemenattion variant that manages to do it inplace without much overhead in complexity.
Since the first K and last K elements are constant in numbers, so it really makes no sense in calculating their complexity as it will be constant.
Comparing all above given algos with their complexity:
A) Quicksort: Worst case O(n²) average O(n log n)
B) Bubble Sort: O(n²)
C) Selection Sort: O(n²)
D) Insertion Sort: O(k* n) if k=constant = O(n)
If the inversion count is O(n), then the time complexity of insertion sort is O(n). In worst case, there can be n(n-1)/2 inversions. The worst case occurs when the array is sorted in reverse order. So the worst case time complexity of insertion sort is O(n2).*
So, Quicksort is best in general but for small List Insertion sort has
a Advantage :
Insertion sort is faster for small n because Quick Sort has extra overhead from the recursive function calls. Insertion sort is also
more stable than Quick sort and requires less memory.
See Why is Insertion sort better than Quick sort for small list of elements?
Merge Sort time complexity is O(n log n) so here n is dominate on logn , so Is Merge-Sort is O(N)
Thanks
O(n log n) is the best that you can get using tradional sort algorithms.
You can't say that O(n log n) == O(n) even if n dominates logn because they are multiplying not adding.
If you got n + logn and n dominates logn then you can say that O is O(n)
given n elements, the datastructure has the following runtime complexities:
Finding the minimum element is Θ(1),
Deleting the minimum element is Θ(lg n)
Inserting an element is Θ(lg n)
i made research, i dont know this fast data structure
from wikipedia:
http://en.wikipedia.org/wiki/Heap_(data_structure)
Operation Binary Binomial Fibonacci
find-min Θ(1) Θ(1) Θ(1)
delete-min Θ(log n) Θ(log n) O(log n)*
insert Θ(log n) O(log n) Θ(1)
decrease-key Θ(log n) Θ(log n) Θ(1)*
merge Θ(n) O(log n)** Θ(1)
(*) Amortized time
(**) Where n is the size of the larger heap
Getting ready for exam. This is not the homework question.
I figured that the worst case O(N^2) to build BST. (each insert req N-1 comparison, you sum all the comparisons 0 + 1 + ... + N-1 ~ N^2). This is the case for skewed BST.
The insertion for (balanced) BST is O(log N), so why the best case is O(N logN) to construct the tree ?
My guess best guess - since single insertion is log N, than summing all the insertions somehow gives us N log.
Thanks !
As you wrote :) Single insertion is O(log N).Because the weighted tree height of N element is log N you need up to log N comparsions to insert single element. You need to do N of these insertions. So N*logN.