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

A) What is the big O for the following: f(n)=5n2+logn+1 i)n^2 ii)1 iii)logn iv)5n2+logn+1 B) Given...
A) What is the big O for the following: f(n)=5n2+logn+1 i)n^2 ii)1 iii)logn iv)5n2+logn+1 B) Given two algorithms with the following running time:Algorithm A: Ta(n)=4n+20 andAlgorithm B: Tb(n)=2n2+10Which of the following statements are correct? i)Algorithm A is faster than algorithm B for n > 100. ii)Algorithm B is faster than algorithm A for n > 100. C) A function template can operate with: i)an integer ii)any type of data iii)a string iv)a Rectangle object
Derive the recurrence for the average time complexity of Quick Sort.
Derive the recurrence for the average time complexity of Quick Sort.
prove the worst case of quick sort using subsetution, what is T(n) of quick sort and...
prove the worst case of quick sort using subsetution, what is T(n) of quick sort and what is the worst case for it
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
What is O(g(n)) for the following method? What sorting algorithm is this? /*Function to sort array...
What is O(g(n)) for the following method? What sorting algorithm is this? /*Function to sort array using ??? sort*/ void sort(int arr[]) { int n = arr.length; for (int i = 1; i < n; ++i) { int key = arr[i]; int j = i - 1; /* Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position */ while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j];...
a. Define Ω(g(n)). b. Express 6n2 + 4n2 logn + 2n3/ 2 + 35 in O-notation....
a. Define Ω(g(n)). b. Express 6n2 + 4n2 logn + 2n3/ 2 + 35 in O-notation. c. Express 4log n + 5n1.63 + 3n in Ω-notation.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT