Question

In: Computer Science

Q1. A. What is the complexity of partition process in quick sort? O(1) O(logN) O(N) O(NlogN)...

Q1.

A.

What is the complexity of partition process in quick sort?

O(1)

O(logN)

O(N)

O(NlogN)

B.

Evaluate the following postfix expression.

2 3 4 + *

C.

In an array representation of heap, what are the parent node of a node a[10]?

a[9]

a[11]

a[5]

a[20]

There is no easy way to access parent node in such representation.

D.

In an array representation of heap, what are the children nodes (if any) of a node a[10]?

a[11] and a[12]

a[9] and a[11]

a[5] and a[6]

a[20] and a[21]

There is no easy way to access children nodes in such representation.

Solutions

Expert Solution

A. O(1)

Explanation: partition is just the swapping process and getting the mid index which can be done in constant time.

B. 14  

Explanation:

The given expression is 2 3 4 + *, we can scan all elements one by one

1. Scan “2”, it’s a number so push it to the stack. Stack contains “2”.

2. Scan “3”, it’s a number so push it to the stack. Stack contains “2 3”.

3. Scan “4”, it’s a number so push it to the stack. Stack contains “2 3 4”.

4. Scan “+”, it’s an operator, so pop last two elements from stack, apply the + operator, we get 3+4=7, We push the “7” to the stack. Stack contains “2 7”,

5. Scan “*”, it’s an operator so pop last two elements from stack, apply the * operator, we get 2 * 7 = 14. We push 14 to the stack. Stack contains “14”.

6. There are no more elements to scan, we return the top element from stack

C. A[5]

Explanation:

Parent node in array representation can be found using A[ceil(i - 1 / 2)], so in this case

i = 10

i - 1 / 2 = (10 - 1 / 2) = ceil(4.5) = 5

So A[5]

D. Answer will be A[21] and A[22]

Explanation:

Left child can be found using A[ ( 2 * i) + 1] = A[ 2*10 + 1] = A[21]

Right child can found using A[ (2 * i) + 2] = A[ 2*10 + 2] = A[22]


Related Solutions

Using the buildHeap method, write a sorting function that can sort a list in O(nlogn) time....
Using the buildHeap method, write a sorting function that can sort a list in O(nlogn) time. def buildHeap(self,alist): i = len(alist) // 2 self.currentSize = len(alist) self.heapList = [0] + alist[:] while (i > 0): self.percDown(i) i = i - 1 Base on this code please
1. Given three arrays of N names each, describe an O(nlogn) algorithm to determine if there...
1. Given three arrays of N names each, describe an O(nlogn) algorithm to determine if there is any name common to all three arrays, and if so, return the first such name.? 2. Given an input array with all equal values, compare the following sorting algorithm using big-O notation. Running Time Space Complexity Merge Sort Quick Sort Heap Sort
What is the runtime for quick sort?
What is the runtime for quick sort?
(50’) Implement Quick-Sort algorithm in quickSort.cpp, where you are expected to implement three functions, swap(), partition()...
(50’) Implement Quick-Sort algorithm in quickSort.cpp, where you are expected to implement three functions, swap(), partition() and quickSort(). You are expected to call swap() within partition(), to call partition() within quickSort(), and you are not expected to declare/ implement other additional functions nor change the main() function. OPTIONAL: If you don’t need/ want to use swap() in your implementation, that is fine. Just delete/ comment it. quickSort.cpp #include <iostream> using namespace std;    // A helper function to facilitate swapping...
Calculate the Big-O time complexity. Show work 1. n^2 + 3n + 2 2. (n^2 +...
Calculate the Big-O time complexity. Show work 1. n^2 + 3n + 2 2. (n^2 + n)(n ^2 + π/2 ) 3. 1 + 2 + 3 + · · · + n − 1 + n
use quick sort to sort the following array. show each pass and what the pivot is...
use quick sort to sort the following array. show each pass and what the pivot is during the pass. please explain why you are swapping positions. do not use a compiler. 30 5 40 11 20 9 15 2 60 25 80 3 73 35 4 75 20 6
how do i find a peak in time complexity of O(log(n)) in a list? input: a...
how do i find a peak in time complexity of O(log(n)) in a list? input: a list of numbers or integers output: a possible local peak in the list for an example: calling function [2,3,8,9,5] would return 3 im thinking about using binary search but it would require a sorted list.
How do I sort a sequence A = 7,2,3,14,2,8,8,3,7,10 in O(nlogm) time where n is size...
How do I sort a sequence A = 7,2,3,14,2,8,8,3,7,10 in O(nlogm) time where n is size of A and m is the number of distinct elements? No code, just a good explanation will do
Implement a CircularDoubleLinkedList.The removelast method should be implemented with O(1) complexity. There rest is just to...
Implement a CircularDoubleLinkedList.The removelast method should be implemented with O(1) complexity. There rest is just to keep all pointers correct. (Implement in Java pls)
What is time Complexity each of the following function? 1- void function(int n) {             for (int...
What is time Complexity each of the following function? 1- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n/2; j++)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 2- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n; j = 2 * j)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 3- void function(int n) {             for (int i=1; i<=n; i++)                           for (int j=1;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT