Explain why building a list in sorted order is an O(n2) operation. You don’t have to derive it with mathematical formality, but at least explain why, mathematically, it’s O(n2).
By ignoring all the lower-order terms and constants, we would say sorting algorithm a is O(N2), which means that the growth rate of the work performed by algorithm a (the number of instructions it executes) is on the order of N2. This is called big O notation, and we use it to specify the complexity class of an algorithm.
We commonly express the cost of an algorithm as a function of the number N of elements that the algorithm acts on. The function gives us an estimate of the number of operations we have to perform in order to use the algorithm on N elements – it thus allows us to predict how the number of required operations will increase as N increases. We use a function which is an approximation of the exact function – we simplify it as much as possible, so that only the most important information is preserved.
For example, we know that when we use linear search on a list of N elements, on average we will have to search through half of the list before we find our item – so the number of operations we will have to perform is N/2. However, the most important thing is that the algorithm scales linearly – as N increases, the cost of the algorithm increases in proportion to N, not N2 or N3. The constant factor of 1/2 is insignificant compared to the very large differences in cost between – for example – N and N2, so we leave it out when we describe the cost of the algorithm.
Here are the best case, expected and worst case costs for the sorting and searching algorithms we have discussed so far:
Algorithm | Best case | Expected | Worst case |
---|---|---|---|
Selection sort | O(N2) | O(N2) | O(N2) |
Merge sort | O(N log N) | O(N log N) | O(N log N) |
Linear search | O(1) | O(N) | O(N) |
Binary search | O(1) | O(log N) | O(log N) |
What does O(1) mean? It means that the cost of an algorithm is constant, no matter what the value of N is. For both these search algorithms, the best case scenario happens when the first element to be tested is the correct element – then we only have to perform a single operation to find it.
Big O notation doesn't tell us everything that we need to know about the running time of an algorithm. For example, if two algorithms are O(N2), we don't know which will eventually become faster). And, if one algorithm is O(N) and another is O(N2), we don't know which will be faster for samll N.
Explain why building a list in sorted order is an O(n2) operation. You don’t have to...
Explain why building a list in sorted order is an O(n2) operation. You don’t have to derive it with mathematical formality, but at least explain why, mathematically, it’s O(n2).
(2 pts) (a) What “better-known”/simpler complexity class is equivalent to O(N log N2); briefly explain why. (b) Explain under what conditions sorted(set(l)) runs faster than set(sorted(l)) for a list l (they both produce the same answer); state the worst-case complexity class of each. (a) (b)
Discrete Mathematics Unsorted and Sorted Lists For linear search there was no requirement for the list to be organized in any manner. The linear search works for lists that are "unsorted." But what if the values in the list are given in ascending order? This would be a sorted list. With a sorted list, is there a more efficient way to find the target? Unsorted Lists (4 pts) Assume there is a sorting algorithm with order of growth O(n), where...
9. When we have two sorted lists of numbers in non-descending order, and we need to merge them into one sorted list, we can simply compare the first two elements of the lists, extract the smaller one and attach it to the end of the new list, and repeat until one of the two original lists become empty, then we attach the remaining numbers to the end of the new list and it's done. This takes linear time. Now, try...
When we have two sorted lists of numbers in non-descending order, and we need to merge them into one sorted list, we can simply compare the first two elements of the lists, extract the smaller one and attach it to the end of the new list, and repeat until one of the two original lists become empty, then we attach the remaining numbers to the end of the new list and it's done. This takes linear time. Now, try to...
You are given an array A of integers in sorted order. However, you do not know the length n of the array. Assume that in our programming language arrays are implemented in such a way that you receive an out-of-bounds error message whenever you wish to access an element A[i] with i>n. For simplicity we assume that the error message simply returns the value INT_MAX and that every value in the array is smaller than INT_MAX. (a) Design an algorithm...
I don’t understand what I did wrong. Could you please explain why it’s 32W/m^2? Question 16 The speed of sound in air is 340 m/s, and the density of air is 1.2 kg/m3. If the displacement amplitude of a 440-Hz sound wave is 10 μm, what is the intensity of the wave? 16 W/m2 rrect Answer 32 W/m2 0.32 W/m 12 W/n You Answered :0.16 W/m2 2/2 pts Question 17
List and explain three reasons why it is important for people to study world religions. Also, explain at least two things you have learned that surprised you
list and explain why you would want to have a property management Company in Dallas Texas
True or false for each, and explain why (4 pts) The height of a binary tree is bounded by O(n2), where n is the size of the C. tree. d. (4 pts) dynamic array and O(1) time if L is a linked list. Given a list L of n > 2 elements, the following code takes O(n) time if L is a iterator i = L. iterator () i.next); i.next); i.remove ); binary tree T that has size n and...