CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

Similar documents
CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

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

CSE 332 Spring 2014: Midterm Exam (closed book, closed notes, no calculators)

CPSC 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001

Programming II (CS300)

ECE242 Data Structures and Algorithms Fall 2008

Mergesort again. 1. Split the list into two equal parts

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

About this exam review

Quicksort (Weiss chapter 8.6)

CSE332 Summer 2010: Midterm Exam Sample Solutions

Data Structures and Algorithms 2018

Better sorting algorithms (Weiss chapter )

CS61B Fall 2015 Guerrilla Section 3 Worksheet. 8 November 2015

Quick Sort. CSE Data Structures May 15, 2002

COS 226 Midterm Exam, Spring 2011

CPSC 311: Analysis of Algorithms (Honors) Exam 1 October 11, 2002

CS 315 Data Structures Spring 2012 Final examination Total Points: 80

CS 146 Midterm Exam 2 Grade: Professors Howard, Fine, and Howard have proposed the following sorting algorithm:

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

COP 3502 Spring 2018 Study Union Review

Module Contact: Dr Geoff McKeown, CMP Copyright of the University of East Anglia Version 1

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

Real-world sorting (not on exam)

CS 171: Introduction to Computer Science II. Quicksort

CS171 Midterm Exam. October 29, Name:

Objectives. Chapter 23 Sorting. Why study sorting? What data to sort? Insertion Sort. CS1: Java Programming Colorado State University

Faster Sorting Methods

CS 112 Final May 8, 2008 (Lightly edited for 2012 Practice) Name: BU ID: Instructions

CS1800 Discrete Structures Fall 2016 Profs. Aslam, Gold, Ossowski, Pavlu, & Sprague December 16, CS1800 Discrete Structures Final

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

Prelim 2. CS 2110, 16 November 2017, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

CSE 373: Data Structures and Algorithms

Sorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort

COS 226 Midterm Exam, Spring 2009

CSE 332 Autumn 2016 Final Exam (closed book, closed notes, no calculators)

Computer Science Foundation Exam

Exam Datastrukturer. DIT960 / DIT961, VT-18 Göteborgs Universitet, CSE

L14 Quicksort and Performance Optimization

Prelim 2 SOLUTION. CS 2110, 16 November 2017, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

Mergesort again. 1. Split the list into two equal parts

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid

Selection, Bubble, Insertion, Merge, Heap, Quick Bucket, Radix

CSE 373 Spring 2010: Midterm #2 (closed book, closed notes, NO calculators allowed)

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid. Fall 2018

LINKED LISTS cs2420 Introduction to Algorithms and Data Structures Spring 2015

CS 112 Final May 8, 2008 (Lightly edited for 2011 Practice) Name: BU ID: Instructions GOOD LUCK!

CS200 Final Exam Fall 2007

CS171 Final Practice Exam

COMP171 Data Structures and Algorithms Fall 2006 Midterm Examination

Sorting (Weiss chapter )

Midterm Exam 2 CS 455, Spring 2011

CS210 (161) with Dr. Basit Qureshi Final Exam Weight 40%

Data Structures and Algorithms

CS32 Final Exam. E03, F15, Phill Conrad, UC Santa Barbara

University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

CS 171: Introduction to Computer Science II. Quicksort

CS1800 Discrete Structures Fall 2016 Profs. Aslam, Gold, Ossowski, Pavlu, & Sprague December 16, CS1800 Discrete Structures Final

CS171 Final Practice Exam

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

Midterm Exam 2 CS 455, Fall 2014

CSE373: Data Structure & Algorithms Lecture 18: Comparison Sorting. Dan Grossman Fall 2013

Midterm 2. Read all of the following information before starting the exam:

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING

Prelim 2, CS2110. SOLUTION

Cornell University Computer Science 211 Second Preliminary Examination 18 April 2006

Prelim 2 Solution. CS 2110, April 26, 2016, 5:30 PM

Introduction hashing: a technique used for storing and retrieving information as quickly as possible.

MPATE-GE 2618: C Programming for Music Technology. Unit 4.2

Computer Science Foundation Exam

York University. AP/ITEC Section M INTRODUCTION TO DATA STRUCTURES Winter Midterm Test

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

Sorting. Weiss chapter , 8.6

AP Computer Science AB

CSE 373: Data Structures and Algorithms

CS301 - Data Structures Glossary By

CSC 273 Data Structures

Q1 Q2 Q3 Q4 Q5 Q6 Total

Homework Assignment #3. 1 (5 pts) Demonstrate how mergesort works when sorting the following list of numbers:

QuickSort

CS125 : Introduction to Computer Science. Lecture Notes #40 Advanced Sorting Analysis. c 2005, 2004 Jason Zych

Midterm CSE 21 Spring 2012

Prelim 2, CS :30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs

Reg. No. : Question Paper Code : 27157

Sorting Algorithms. + Analysis of the Sorting Algorithms

Midterm CSE 21 Fall 2012

Computer Science Foundation Exam

Computer Science Foundation Exam

Write the code to create an enhanced for loop that will go through every member of an ArrayList <String> myarray and print it out to the console.

CS 3343 (Spring 2013) Exam 2

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

Final Examination CSE 100 UCSD (Practice)

Draw a diagram of an empty circular queue and describe it to the reader.

Programming II (CS300)

Exam I Principles of Imperative Computation, Summer 2011 William Lovas. May 27, 2011

What is sorting? Lecture 36: How can computation sort data in order for you? Why is sorting important? What is sorting? 11/30/10

Searching and Sorting

APJ ABDUL KALAM TECHNOLOGICAL UNIVERSITY THIRD SEMESTER B.TECH DEGREE EXAMINATION, JULY 2017 CS205: DATA STRUCTURES (CS, IT)

Sorting. Riley Porter. CSE373: Data Structures & Algorithms 1

Transcription:

CS 61B Summer 2005 (Porter) Midterm 2 July 21, 2005 - SOLUTIONS Do not open until told to begin This exam is CLOSED BOOK, but you may use 1 letter-sized page of notes that you have created. Problem 0: (1 point) Please fill out this information, and when told to begin, please put your name on each page of the exam. Your name: Solution Your cs61b login: Lab time: Lab TA s name: Name of person to your left: Name of person to your right: Do not write below here: Problem Score Total possible 0 1 1 1 (10 minutes) 15 15 2 (10 minutes) 15 15 3 (15 minutes) 20 20 4 (35 minutes) 34 34 Total: 85 85 Do not open until told to begin Good Luck!

Name: 2 / 6 Problem 1 (15 pts) Algorithm Analysis (10 minutes) Given T(n) = 22n - 37, show that T(n) is O(N 2 ). Make use of the definition of Big-Oh. We need to find c and N 0 such that: 22n 37 <= c N 2. Let s start by setting c = 1. Now by moving some terms around, we get: 22n N 2 <= 37 = n(22 N) <= 37. Now for N > 22, we have that (22 N) is negative. For N > 22, N is positive. A positive number times a negative number is negative, and all negative numbers are <= 37. Thus we show that T(n) is O(N 2 ) by setting c = 1 and N 0 = 23. Problem 2 (15 pts) Probing (10 minutes) Show the result of each insertion sequence using the three insertion methods below. For chaining, use the space to the right of the array for the linked lists. hash ( 59, 11) = 4 hash ( 82, 11) = 5 hash ( 26, 11) = 4 hash ( 5, 11) = 5 hash ( 92, 11) = 4 Linear Probing Quadratic Probing Chaining 0 0 0 1 2 3 4 59 5 82 6 26 7 5 8 92 9 10 1 2 92 3 4 59 5 82 6 5 7 8 26 9 10 1 2 3 4 59, 26, 92 5 82, 5 6 7 8 9 10

Name: 3 / 6 Problem 3 (20 pts) Sorting (smaller to larger) (15 minutes) 3.1) (7 pts) Here is an array of ten integers: 5 3 8 9 1 7 0 2 6 4 Suppose we partition this array using quicksort's partition function (use the Quicksort code from Weiss at the end of the exam) and using 5 for the pivot. Draw the resulting array after the partition finishes. 1) swap 5 with array[high]: 4 3 8 9 1 7 0 2 6 5 2) i stops at 8, j stops at 2; swap: 4 3 2 9 1 7 0 8 6 5 3) i stops at 9, j stops at 0; swap: 4 3 2 0 1 7 9 8 6 5 4) i stops at 7, j < i; swap w/ pivot: 4 3 2 0 1 5 9 8 6 7 * step 4 is optional 3.2) (6 pts) Here is an array of ten integers: 5 3 8 9 1 7 0 2 6 4 Draw this array after the TWO recursive calls of merge sort are completed, and before the final merge step has occurred. 1 st recursive call: mergesort of left half: 1 3 5 8 9 7 0 2 6 4 2 nd recursive call: mergesort of right half: 1 3 5 8 9 0 2 4 6 7 we don t do the final merge step, thus: 1 3 5 8 9 0 2 4 6 7 3.3) (7 pts) Indicate the worst case and average case performance using Big-Oh notation: Algorithm Worst Case Average Case Binary Search of a sorted array log N log N Insertion sort Merge sort Quick sort without median of three pivot selection Quick sort with median of three pivot selection Shell sort N^(3/2) or N^(7/6) or N^(5/4)

Name: 4 / 6 Problem 4 (34 pts) Hashtables vs Sorted Linked Lists (35 minutes) Consider a composite data structure to represent an inventory of items, declared as follows. It contains as private data members: class Item { public String name; public int cost; }; public class Inventory {... private LinkedList sortedbycost; private ArrayList hashtable;... }; (The LinkedList class is similar to what you implemented in the homework). Items are identified uniquely by name; i.e. there shouldn t be two items with the same name. Elements of the sortedbycost list appear in decreasing order by cost, except that if there are more than one item with the same cost, those items appear in the list alphabetically by name. The corresponding items should also all be represented in the hash table, which is accessed by hashing a name, then searching the corresponding chain for a pointer to the item with that name. To aid debugging, one might devise a function that checks that the inventory structure is internally consistent. Such a function might check that the lists are correctly constructed, i.e. that there aren t any circular pointers and that the size is equal to the number of items in each list. It might also check that sortedbycost is correctly sorted. For this problem, you are to describe how to implement two additional consistency checks: Duplicate check: no two items represented in sortedbycost should have the same name; no two items represented in hashtable should have the same name. Correspondence between components: Every item represented in sortedbycost should also be represented in hashtable, and vice-versa.

Name: 5 / 6 Part a (20 points) Describe an implementation for performing the operations labeled as Duplicate check and Correspondence between components on the previous page. Each consistency check should be performed as efficiently as possible on the average. You may use additional structures and should not change the existing contents of the inventory. Your description should specify what additional data structures you use, what they contain and, if your data structures include arrays, how big the arrays are. Your description should be in terms suitable for another CS 61B student to understand immediately how it would be translated into code. 1. Hash the contents of the individual chains by name, using another hash function and a table that's of size dependent on M, the size of the chain: If two items with the same name are seen, print an error message 2. Sort the chains by cost (primary key) and name (secondary key) using a fast sort (Quicksort or merge sort). 3. Compare the resulting list with sortedbycost. or, Duplicate check: 1) For each chain in your hashtable (M_i), copy it to an array of length size(m_i). 2) Sort M_i using mergesort 3) Check adjacent elements for elements with the same name you can check sortedbycost in a similar way

Name: 6 / 6 Part b (14 points) Give an estimate of the running time needed for each step involved in your answer to part a. Be specific about the quantities your estimates involve. 1. For each chain, order M for a reasonable hash function. Total this over all chains => order N. 2. Order, where N is the number of items. 3. Order N. (alternative approach): building the array: O(size(M_i)) sorting: O(M_i * size(m_i)) checking neighbors: O(size(M_i)) If we sum M_i over all i, we get N. Thus, O().