Derive the worst case big O time to find the 5th largest item in an unsorted array of length n. Assume n is much larger than 5. You can modify the way the array items are arranged. No code needed just explain what the big o is, which data structure/algorithm you used if any, and explain.
let we use array a[ ] to store our numbers. And size of array is n. Now we have to find 5th largest element for that we can use following algorithm.
1. Find_5th_largest(array a[ ])
2.{ int largest[5] ={0,0,0,0,0};
4.int i
5. for(i=0;i<n;i++)
6. {
7. if (a[i]>largest[0]) { largest[0] = a[i] }
8. elseif(a[i]>largest[1] { largest[1] = a[i] }
9. elseif( a[i] >largest[2]) { largest[2]=a[i]}
10. elseif (a[i]>largest[3] ) {largest[3]=a[i] }
11. elseif( a[i]>largest[4] ) {largest[4] = a[i] }
12. }
13. So 5th largest number is largest[4].
Explanation of code- We declare an array largest that can hold 5 numbers. Now at first we set numbers of largest array to 0.
Now we execute loop of i from 0 to n. and at each iteration we check whether value of a[i] is greater than value present in largest[] array. Now 0 index of largest array contain largest element of array 1st index of largest array contain second largest element of array and similarly 4th index contain fifth largest element of array. So at each iteration we check a[i] to largest[0] first. if a[i] is greater than largest[0] it means it is largest element of array if not then we check with largest[1] which is second largest and so on.
Time complexity -
lines 2,4,7,8,9,10,11,12,13 all are constant time operations because just they are either variable declaration or some condition checking. But line 5 contains a loop in which i range fro 1 to n. which means inner statement executes n times . so let all inner statement executes in c time and other constant operations outside loop executes k times here c and k both are constants.
therefore T(n)= (c+c+c+c...ntimes) +k
T(n) = cn +k
therefore T(n) = O(n)
Derive the worst case big O time to find the 5th largest item in an unsorted...
Show your work Count the number of operations and the big-O time complexity in the worst-case and best-case for the following code int small for ( i n t i = 0 ; i < n ; i ++) { i f ( a [ i ] < a [ 0 ] ) { small = a [ i ] ; } } Show Work Calculate the Big-O time complexity for the following code and explain your answer by showing...
Consider an array A[1...n] which is originally unsorted. One method to sort the array is as follows: First find the largest key in the unsorted portion of the array (initially the entire array s unsorted) and swap this with the last position in the unsorted section. This last position is now considered part of the sorted portion. The procedure is repeated until the entire array is sorted. (a) Write an algorithm to sort A as outlined above (in pseudo-code, no...
Please give little explanation, I'm trying to understand why. 2. List appropriate Worst Case Big O Notation under the different algorithms or data structure operations. O(1) O(n) O(n ^ 2) O(log n) A. Directly Accessing value in a 2-dimensional by [row][column] B. Selection Sort then Binary Search on a 1,000,000 element array C. Binary Search D. Highest element algorithm for 10,000 element array. E. Traversal of 6 character string O(n) 3. Give examples of Implicit declarations for Python and C++....
please I need it urgent thanks algorithms
2.1 Searching and Sorting- 5 points each 3. What is the worst case for quick sort? What is the worst case time com- plexity for quick sort and why? Explain what modifications we can make to quick sort to make it run faster, and why this helps. 4. Give pseudocode for an algorithm that will solve the following problem. Given an array AlL..n) that contains every number between 1 and n +1 in...
Big O SECTION List appropriate Worst Case Big O Notation under the different algorithms or data structure operations. Choose from right column and place under left column. Right column can be used more than once or not all. O(1) O(n) O(n ^ 2) O(log n) Time Efficiency A. dequeue() a Node 1,000,000 element Linked List - Queue Implementation B. Traversing a Doubly Linked List from the Tail Node to the Head Node C. Traversing every element in Stack with array...
List the worst case and average case Big O for each algorithm below and describe how the algorithm works. You can diagram or write a short paragraph. Bubble Sort Modified Bubble Sort Insertion Sort Merge Sort Selection Sort Shell Heap Quick
For each algorithm, give a reasonable big-O bound on its worst-case running time. Omit unnecessary terms and constants in your bound, for example, don't say O(2n22n 1), say O(n2). (In most cases, these aren't the best possible algorithms for each task!) Briefly explain your reasoning in each case.
Which big-O expression best characterizes the worst case time complexity of the following code? public static int foo(int N) ( int count = 0; int i1; while (i <N) C for (int j = 1; j < N; j=j+2) { count++ i=i+2; return count; A. O(log log N) B. O(log N2) C. O(N log N) D. O(N2)
1.If a list is implemented as a singly linked stack, give the big-O worst-case time complexity of the following operations (as usual use the smallest standard big-O category that works: a) push_front, b) push_back, c) lookup, d) read the i'th member 2.Repeat question 3 for a dynamic array (for example, as in the C++ vector class)
please check my answers if it wrong answer me
a) (25 points) Suppose that you are asked to analyze the performance. Algorithms operate on 1D array of size nor 2D a of the algorithms below, write down the Big O or order of grow terms of n. If the algorithm cannot be applied to the array, write 0(1), O(log n), O(n), O(n logn), 90), O(n"), O(n!). The will only be given for reasonable Big O answers. & algorithms for their...