Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

the question and the implementaion we learn. Problem 2 (40 points) In class we talked about improving the insertion sort using binary search to find

the question and the implementaion we learn. image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Problem 2 (40 points) In class we talked about improving the insertion sort using binary search to find the location of the key in the sorted part of the array. Implement this improvement. Specifically, write a method called improvedInsertionSort0 which is a modification of the insertion sort, such that instead of comparing the key with all the elements in the sorted part, uses binary search to find the location of the key with less comparisons. void improvedInsertionSort(int [arr) //your code Stacks LIFO (Last In First Out) Only the last element (top of the stack) can be accessed Simple operations in O(1) push pop Helpful tool for programmers Stack Implementation . Can use an array to hold elements class Stackx //fields private int maxSize private charD stackArray private int top maxSize-1 public StackX(int maxSize) 3 2 c top methods public void push(char item) ...) public char pop) .. public boolean isEmpty (...) public boolean isFullO . public int size0 ) public char peek Example-Delimiter Matching Tell if an input string has a matching parent theses (a+b)+c): correct (a+b)+c): incorrect Use a stack pushinto stack, pop when you see", decide based on stack empty condition. Example-Delimiter Matching public boolean checkParenthesisMatch (String exp) StackX st new Stackx (exp.lengthO for( int iexp. length();i++); What if I want to check brackets D. at the same time? char ch exp.charAt(i) if (ch st pushch) if (ch y) if (st.isEmpty0) return false else What is the time st.popo; return st.isEmpty0 Example-Delimiter Matching import java.util.Stack; public boolean checkParenthesisMatch (String exp) Stack Character> st new Stack= front) // contiguous sequence return rear-front+1 else // broken sequence return (maxSize-front)+(rear+) Queue enqueue dequeue rear front FIFO (First In First Out) Access from both ends Add to the rear Remove from front No immediate access to the middle elements Applications in job/resource management, packet transmission, Queue-Implementation Can use an array to hold elements Rear and front move as we add/remove elements Circular Array maxSize-1 2 c rear 0a front Queue Class class Queuce private int maxSize private long[] queArray; private int front; private int rear private int nltems; public Queuelint s) public void enqueue(longj public long dequeue0 public long peeko public boolean isEmptyo public boolean isFullo 8 public int size0 0 Priority Queues Similar to the simple queue, but elements have priorities. The highest priority element is at the front. Elements are kept in an ordered way. Applications in operating systems, graph algorithms. Priority Queues Remove from the front Add to the rear? We want to keep the order. So, add to the right place Assume element with smallest key has the highest priority. enqueue(d) dequeue Priority Queue-Implementation Can use an array to hold elements Fields needed maxSize-1 maxSize (capacity of queue queArray (array of elements in queue front (index of the front) rear (index of the rear) nltems(number of elements or size) 3 afront 2 b o e rear enqueue(d) dequeue0 Priority Queue-Implementation What is the time complexity of dequeue0 and enqueue0? O(l) for dequeue0 and O(n) for enqueue Slow insertion Is there a better way to implement priority queue? maxSize-1 We do not need front and rear indices. rear is always 0 and front is nltems-1 front Priority Queue-Implementation Methods needed enqueue(element) dequeue0 peckO size0 isEmptyO isFullO maxSize-1 0 Priority Queue Class public long dequeue)/remove the highest priority element if (isEmpty) throw new IllegalStateException Queue is empty") return queArray I-nltems) public long peek0 peek at highest priority item if (isEmpty0 throw new IllegalStateException Queue is empty): return queArray [nltems-1 Priority Queue Class public boolean isEmptyO//true if queue is empty return (nltems 0) public boolean isFullO/ true if queue is full return (nltems max Size); public int size)number of items in queue return nltems Priority Queue Demo Class class PQDemo public static void main(String] args) PriorityQ myPQ-new PriorityQ(4) myPQ.enqueue(4) myPQ.enqueue(2); myPQ.enqueue(3); myPQ.enqueue(); while(!myPQ.isEmpty) System.out.printin(myPQdequeue0) Parsing Arithmetic Expressions How do we evaluate arithmetic expressions: 2+3 or 2*(3+4) Infix notation Easier to convert the expression into a postfix notation, then evaluate the postfix expression. 23+ or 234+ Examples: 1+2-3 12+3- 1+2*3 D 123*+ 1*2+3*4 12*34*+ Parsing a Postfix Expression Given a postfix expression, evaluate the expression. (For conversion from infix to postfix, read chapter 4 pages 149-166) Examples 1+2-3 12+3- 1 +2*3123#4 12+3*4 12 34*+ Code to Parse a Postfix Expression public static int esp) Stack Imeger> stack new Stack Integer -0 char ch exp.charAdik CharacterisDiginch) else int vall stack pop int val2 stack pop What is the output if I pass "52? case break; case stack pusval2- vall) break: break; break; rcturn stack.pop

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Accounting And Auditing Research And Databases Practitioner's Desk Reference

Authors: Thomas R. Weirich, Natalie Tatiana Churyk, Thomas C. Pearson

1st Edition

1118334426, 978-1118334423

More Books

Students also viewed these Databases questions

Question

1. Describe a comprehensive approach to retaining employees.pg 87

Answered: 1 week ago