CS 12 Fall 2003 Solutions for mid-term exam #2

Similar documents
public static <E extends Comparable<? super E>>void BubbleSort(E[] array )

12/1/2016. Sorting. Savitch Chapter 7.4. Why sort. Easier to search (binary search) Sorting used as a step in many algorithms

Final Exam. COMP Summer I June 26, points

CS 163 Practice Final Exam Winter 2012

for (int outercounter = nums.length - 1; outercounter > 0 && swappedthatturn; outercounter --

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

Chapter 10 - Notes Applications of Arrays

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

CS 455 Final Exam Fall 2012 [Bono] Dec. 17, 2012

COP 3337 Test 3. private int minimumposition(int from) { Part 1: Selection Sort

DATA STRUCTURES AND ALGORITHMS. Sorting algorithms (insertion sort, bubble sort, selection sort)

Searching for Information. A Simple Method for Searching. Simple Searching. Class #21: Searching/Sorting I

Topic 5: Abstract Classes & Interfaces

COMP1511 focuses on writing programs. Effciency is also important. Often need to consider:

Introduction to Computer Science Midterm 3 Fall, Points

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

COMP 103. Mid Trimester Test 17 August 2006

Algorithm Analysis. Performance Factors

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 26, 2017

by Pearson Education, Inc. All Rights Reserved.

Sorting. Task Description. Selection Sort. Should we worry about speed?

Data Types. Operators, Assignment, Output and Return Statements

The University Of Michigan. EECS402 Lecture 07. Andrew M. Morgan. Sorting Arrays. Element Order Of Arrays

SCJ2013 Data Structure & Algorithms. Bubble Sort. Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi

Sorting. Weiss chapter , 8.6

Question: Total Points: Score:

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.

Computer Science Foundation Exam

Reviewing all Topics this term

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

Compiling Techniques

Name Section Number. CS210 Exam #3 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

Chapter Objectives. List Processing. Chapter 10: Applications of Arrays and Strings. Java Programming: Program Design Including Data Structures

What did we talk about last time? Finished hunters and prey Class variables Constants Class constants Started Big Oh notation

5. What is a block statement? A block statement is a segment of code between {}.

CS 216 Fall 2007 Final Exam Page 1 of 10 Name: ID:

Computer Science & Engineering 150A Problem Solving Using Computers

Searching & Sorting in Java Bubble Sort

CS 351 Design of Large Programs Programming Abstractions

C/C++ Programming Lecture 18 Name:

CS2630: Computer Organization Homework 1 Bits, bytes, and memory organization Due January 25, 2017, 11:59pm

! Search: find an item in an array, return the. ! Sort: rearrange the items in an array into some. ! There are various methods (algorithms) for

University of Waterloo Midterm Examination

New York University Intro to Computer Science (CSCI-UA.101) Fall 2014 Midterm #1 Test G. Instructions:

CMSC 202. Generics II

Lecture #8-10 Arrays

Tasks for fmri-setting (Tasks of first and second pilot study at the end)

CS61BL: Data Structures & Programming Methodology Summer 2014

2016 Spring COP 3502 Final Exam. Date: 4/28/2016. Name:

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 25, 2016

Computational Expression

Topics for CSCI 151 Final Exam Wednesday, May 10

B.V. Patel Institute of BMC & IT, UTU 2014

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

Week 12: Running Time and Performance

Algorithms. Chapter 8. Objectives After studying this chapter, students should be able to:

LECTURE 17. Array Searching and Sorting

Tutorial 1 CSC 201. Java Programming Concepts عؾادئماظربجمةمبادؿكدامماجلاصا

8.1. Chapter 8: Introduction to Search Algorithms. Linear Search. Linear Search. Linear Search - Example 8/23/2014. Introduction to Search Algorithms

CS 231 Data Structures and Algorithms, Fall 2016

CS 360 Programming Languages Interpreters

CS 170 Exam 2. Version: A Fall Name (as in OPUS) (print): Instructions:

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm

ALGORITHM ANALYSIS. cs2420 Introduction to Algorithms and Data Structures Spring 2015

Sample Exam I PAC II ANSWERS

SaM. 1. Introduction. 2. Stack Machine

CS 151 Name Final Exam December 17, 2014

CS 177 Week 15 Recitation Slides. Review

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

CS 106 Introduction to Computer Science I

20-CS Programming Languages Fall 2016

OCR H446 A-Level Computer Science

Pace University. Fundamental Concepts of CS121 1

10/21/ Linear Search The linearsearch Algorithm Binary Search The binarysearch Algorithm

Arrays. Eng. Mohammed Abdualal

Sorting. Order in the court! sorting 1

ECE 242 Fall 13 Exam I Profs. Wolf and Tessier

CS 455 Final Exam Fall 2015 [Bono] Dec. 15, 2015

Sorting (Weiss chapter )

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Supplementary Test 1

Sorting. Bubble Sort. Selection Sort

Java Bytecode (binary file)

Sorting Algorithms part 1

CmpSci 187: Programming with Data Structures Spring 2015

Exam 2. CSC 121 TTH Class. Lecturer: Howard Rosenthal. April 26, 2016

Searching and Sorting (Savitch, Chapter 7.4)

CMPT-166: Sample Midterm

Sorting. Order in the court! sorting 1

CS 132 Exam #1 - Study Suggestions

Computing Science 114 Solutions to Midterm Examination Tuesday October 19, In Questions 1 20, Circle EXACTLY ONE choice as the best answer

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

CS 101 Fall 2005 Midterm 2 Name: ID:

What is an algorithm?

Exam 1 - (20 points)

8/2/10. Looking for something COMP 10 EXPLORING COMPUTER SCIENCE. Where is the book Modern Interiors? Lecture 7 Searching and Sorting TODAY'S OUTLINE

CS 2412 Data Structures. Chapter 10 Sorting and Searching

PLD Semester Exam Study Guide Dec. 2018

Transcription:

CS 12 Fall 2003 Solutions for mid-term exam #2 1. (10 points) Compilers and interpreters Provide short answers (a few sentences at most) to the following questions. (a) What is the difference between a compiler and an interpreter? An interpreter translates source code to machine code on demand, as the program runs. A compiler perform the translation once, before the program runs. (b) Is C++ compiled, interpreted, or both? Explain. C++ is compiled. The source code and header files are translated into a single executable file that contains the machine code. To execute a C++ program, the machine code from that executable file is read and processed directly by the CPU. (c) Is Java compiled, interpreted, or both? Explain. Java uses both. The source code is always compiled into bytecode. To execute the bytecode, the Java Virtual Machine (JVM) must translate that bytecode into machine code. It can do so either by interpreting the bytecode, or by compiling needed portions of the bytecode using a just-in-time (JIT) compiler. 1

2. (15 points) Sorting Write pseudocode that would perform bubble-, selection-, or insertion-sort (select one) on an array of integers. void bubblesort (int[] array) { boolean swap; do { swap = false; for (int i = 0; i < array.length - 1; i++) { if (array[i] > array[i+1]) { int temp = array[i]; array[i] = array[i+1]; array[i+1] = temp; swap = true; while (swap); void selectionsort (int[] array) { for (int i = 0; i < array.length - 1; i++) { int minindex = i; for (int j = i + 1; j < array.length; j++) { if (array[j] < array[minindex]) { minindex = j; int temp = array[i]; array[i] = array[minindex]; array[minindex] = temp; void insertionsort (int[] array) { for (int i = 1; i < array.length; i++) { int insertionvalue = array[i]; int j = i - 1; while ((j >= 0) && (insertionvalue < array[j])) { array[j+1] = array[j]; j--; array[j+1] = insertionvalue; 2

3. (25 points) Linked lists and arrays Consider a class that stores a set of Objects. This class provides a find() method that finds and returns a pointer to a given object. The class also provides a getrecencyorder() method that indicates how recently each object was accessed through the find() method. We want the class to keep a data structure that orders the objects based on how recently each has been accessed. To do so, we use the move-to-front (MTF) algorithm: When find() is called, the object is found in the data structure and then moved to the beginning of the order. Answer the following questions about implementing the MTF algorithm with an array or linked list: (a) How long does a single MTF operation take using each choice? Express your answers with Big-O notation. Using an array, moving an object to the front takes O(n) time, since other objects must be copied to fill in the space left by removing the object from its original location. Using a linked list, this operation requires O(1) time, since removing a link takes a constant number of operations, and inserting at the front does also. If we include the time required to find the requested object, however, both arrays and linked lists require O(n) time to perform the search. (b) Consider that the class also keeps a lookup table a structure that allows it to find a particular object within the ordering in O(1) time. Now how long does an MTF operation take using each choice of data structure? Again, use Big-O notation. The MTF operation itself is unaffected. However, if we include the time to search for the object, arrays still require O(n) as described above, while linked lists now require a total of O(1) time. 3

4. (25 points) Parsing with state machines Consider a Java-like statement in which a variable is assigned a positive integer value specifically, a line of the following form: myvar15 = 3242; For this statement, the following rules apply: Variable names are case insensitive. The variable name must start with a letter. Subsequent characters in a variable name may be letters or digits. The space following the variable name is optional. The equals sign appears between the variable name and the integer value. The space following the equals sign is optional. The integer may be any number of digits in length, and may only be positive. A semicolon must immediately follow the last digit of the integer. Draw a state machine that will parse this statement. Clearly label the initial, failure, and success states. Also clearly label all transitions. [0 9, a z] [0 9, a z] Start [a z] [=] [0 9] [;] Success [space] [=] [space] [0 9] Unlabeled edges used for otherwise cases Failure 4

5. (25 points) Compression Consider the following message: EXAMS MAKE ME QUEASY Each symbol occurs the following number of times in the message: A 3 E 4 K 1 M 3 Q 1 S 2 U 1 X 1 Y 1 <space> 3 Encode this message using a Huffman code based on the symbol frequencies above. Be sure to provide a key or a tree that shows the bit pattern corresponding to each encoded symbol. In the tree below, following the left edge corresponds to a 0, and following a right edge corresponds to a 1. E space A M K Q U X Y S Therefore, the encoded message is: E 00 M 111 X 0111 E 00 A 110 100 M 111 Q 0101 S 1011 U 0110 100 E 00 M 111 A 110 A 110 S 1011 K 0100 Y 1010 E 00 100 5