Download presentation

Presentation is loading. Please wait.

Published byMarie Blea Modified over 2 years ago

1
If f(n) and g(n) are both O(h(n)), then f(n) + g(n) is O(h(2n)). False! From the Rule of Sum, f(n) + g(n) => max(O(h(n)), O(h(n))) => O(h(n))

2
A class is an abstract class if it fails to implement all methods declared in the interface which the class implements. True! An abstract class lies between an interface and a complete concrete class which may contain empty method declarations (that is, declarations of methods without bodies, such as methods declared in an interface).

3
Algorithm A uses 1000nlogn operations, while algorithm B uses n 2 operations, we say that A is faster than B. True! A: O(nlogn) B: O(n 2 ) and O(nlogn) << O(n 2 )

4
The following Java code is correct: int b = 10; Integer a = new Integer(20); int sum = a + b; False! Variable a is an Integer object, which does not have addition arithmetic defined. Correction: int sum = a.intValue() + b;

5
Using SinglyLinkedLists to implement the Stack ADT is not more efficient than using the DoublyLinkedLists. False! It is more efficient because for Stack ADT, the efforts on reverse links (memory and maintenance) are completely wasted.

6
Draw a single class inheritance diagram for the following set of classes: class Vehicle extends Object and defines methods ride() and move(). class Car extends Vehicle and defines methods drive() and stop(). class Yugo extends Car and defines instance variable “oil'' and methods drip() and burn(). class Honda extends Car and defines instance variable “fuel'‘ and method sip(). class Volvo extends Car and defines methods crash(), popAirBags(), and stop(). class SportUtility extends Vehicle and defines instance variable “allWheelDrive'‘ and method roadTrip().

7
Object Car SportUtility Yugo Volvo Honda Vehicle ride() move() drive() stop() oil drip() burn() fuel sip() crash() popAirBags() stop() allWhellDrive roadTrip()

8
Counting all primitive operations for each statement, characterize the worst-case running time of the following algorithm using the big-Oh notation, please explain your answer: Let A be a given array Of n integers. for i 0 to n-1 do if (A[i] = 0) then for j 0 to i do let A[i] A[j] + A[i]. end for end if end for Primitive operations: n 1, 2, 3, … n

9
Worse case happens when the input array A[] is initialized with all 0’s. The worse case running time in terms of n in Big_O is: O(n 2 )

10
Suppose you are asked to use two stacks, inStack and outStack, as your only instance variables to implement the Queue abstract data type. public class QueueObject implements Queues{ StackObject inStack; StackObject outStack; QueueObject() { // assumed } public int size(){ // assumed } public boolean isEmpty(){ // assumed } public void enqueue(Object anObj){ // your job } public Object dequeue() throws EmptyQueueException{ // your job }

11
enqueue(e3) inStackoutStack e1 e2

12
inStackoutStack e1 e2 e3e1 e2 e3 dequeue() 123

13
public void enqueue(Object anObj){ inStack.push(anObj); } public Object dequeue() throws EmptyQueueException{ if (inStack.isEmpty() && outStack.isEmpty()) throw new EmptyQueueException(); if (outStack.isEmpty()) while (!inStack.isEmpty()) outStack.push(inStack.pop()); return outStack.pop(); }

14
Complexity Analysis: enqueue(): O(1). dequeue(): O(1). Because for each element in the queue, the dequeue operation requires three primitive operations: 1) pop from inStack 2) push to outStack, and 3) pop from outStack.

Similar presentations

OK

Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.

Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on fair and lovely Ppt on home automation using rf Ppt on mars exploration rover Download ppt on cell organelles and their functions Ppt on endangered species free download Ppt on conservation of electrical energy Ppt on review writing service Ppt on area of parallelogram using vectors Ovary anatomy and physiology ppt on cells Ppt on pin diode application