CS 112 Practice Midterm Solutions

Similar documents
CS Sorting Terms & Definitions. Comparing Sorting Algorithms. Bubble Sort. Bubble Sort: Graphical Trace

Computer Science E-119 Practice Midterm

Quicksort. Repeat the process recursively for the left- and rightsub-blocks.

BM267 - Introduction to Data Structures

Sorting Algorithms. + Analysis of the Sorting Algorithms

MERGESORT & QUICKSORT cs2420 Introduction to Algorithms and Data Structures Spring 2015

Algorithm for siftdown(int currentposition) while true (infinite loop) do if the currentposition has NO children then return

CSE 2123 Sorting. Jeremy Morris

// walk through array stepping by step amount, moving elements down for (i = unsorted; i >= step && item < a[i-step]; i-=step) { a[i] = a[i-step];

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

Sorting. Bringing Order to the World

COMP 250 Fall Homework #4

Quicksort. The divide-and-conquer strategy is used in quicksort. Below the recursion step is described:

CSE 2320 Section 002, Fall 2015 Exam 2 Time: 80 mins

Lecture 11: In-Place Sorting and Loop Invariants 10:00 AM, Feb 16, 2018

Cosc 241 Programming and Problem Solving Lecture 17 (30/4/18) Quicksort

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

Data Structures Brett Bernstein

Searching in General

Plan of the lecture. Quick-Sort. Partition of lists (or using extra workspace) Quick-Sort ( 10.2) Quick-Sort Tree. Partitioning arrays

CS 310 Advanced Data Structures and Algorithms

Lecture 19 Sorting Goodrich, Tamassia

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

Cpt S 122 Data Structures. Sorting

Sorting. Quicksort analysis Bubble sort. November 20, 2017 Hassan Khosravi / Geoffrey Tien 1

Introduction. Sorting. Definitions and Terminology: Program efficiency. Sorting Algorithm Analysis. 13. Sorting. 13. Sorting.

COMP2012H Spring 2014 Dekai Wu. Sorting. (more on sorting algorithms: mergesort, quicksort, heapsort)

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

ECE 242 Fall 13 Exam I Profs. Wolf and Tessier

COMP 250 Fall Solution - Homework #4

COMP 250 Winter Homework #4

Sorting. Bubble Sort. Pseudo Code for Bubble Sorting: Sorting is ordering a list of elements.

MTH 307/417/515 Final Exam Solutions

Bin Sort. Sorting integers in Range [1,...,n] Add all elements to table and then

Sorting: Quick Sort. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

Sorting. Lecture10: Sorting II. Sorting Algorithms. Performance of Sorting Algorithms

Stacks, Queues (cont d)

CS126 Final Exam Review

Your favorite blog : (popularly known as VIJAY JOTANI S BLOG..now in facebook.join ON FB VIJAY

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych

Prof. Sushant S Sundikar 1

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

IS 709/809: Computational Methods in IS Research. Algorithm Analysis (Sorting)

QuickSort

Overview of Sorting Algorithms

CS 112 Midterm Exam Fall 2016

KF5008 Algorithm Efficiency; Sorting and Searching Algorithms;

CMPSCI 187: Programming With Data Structures. Review for Final Exam David Mix Barrington 10 December 2012

Data Structures and Algorithms

Data Structures and Algorithms, Winter term 2018 Practice Assignment 3

Design and Analysis of Algorithms Assignment-2 Solutions (Hints) REMARK: Quicksort

About this exam review

Data Structures and Algorithms Winter term 2012

Name: After line 1: After line 3: After line 5: After line 8: After line 11:

Data Structures And Algorithms

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

Bubble sort starts with very first two elements, comparing them to check which one is greater.

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

CHAPTER 7 Iris Hui-Ru Jiang Fall 2008

CS1020 Data Structures and Algorithms I Lecture Note #14. Sorting

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

Introduction. Sorting. Table of Contents

Faster Sorting Methods

No Aids Allowed. Do not turn this page until you have received the signal to start.

Assignment 4: Question 1b omitted. Assignment 5: Question 1b clarification

COMP171 Data Structures and Algorithms Fall 2006 Midterm Examination

Sorting. Popular algorithms: Many algorithms for sorting in parallel also exist.

Design and Analysis of Algorithms

4. Sorting and Order-Statistics

Binary Trees. Quicksort. SorKng process with quicksort. University of Massachuse5s Amherst ECE 242 Data Structures and Algorithms Lecture 18

No Aids Allowed. Do not turn this page until you have received the signal to start.

Data Structures and Object-Oriented Design III. Spring 2014 Carola Wenk

Chapter 7 Sorting. Terminology. Selection Sort

Measuring algorithm efficiency

16. Dynamic Data Structures

DATA STRUCTURES AND ALGORITHMS

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

Intermediate Programming, Spring 2017*

CS300 Final Review Questions 1

Assignment 1. Check your mailbox on Thursday! Grade and feedback published by tomorrow.

Merge Sort. Biostatistics 615/815 Lecture 11

Choose 3 of the 1 st 4 questions (#'s 1 through 4) to complete. Each question is worth 12 points.

CS 240 Data Structure Spring 2018 Exam III 04/25/2018

Divide and Conquer Sorting Algorithms and Noncomparison-based

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

Section I B COMPUTER SCIENCE SOLUTION

class for simulating a die (object "rolled" to generate a random number) Dice(int sides) -- constructor, sides specifies number of "sides" for the die

MIDTERM WEEK - 9. Question 1 : Implement a MyQueue class which implements a queue using two stacks.

Lecture 5: Sorting Part A

Programming II (CS300)

COMP Data Structures

Ashish Jamuda CS 331 DATA STRUCTURES & ALGORITHMS COURSE FINAL EXAM

Lecture 7: Sorting Techniques. Prakash Gautam 26 April, 2018

Randomized Algorithms, Quicksort and Randomized Selection

CENG3420 Computer Organization and Design Lab 1-2: System calls and recursions

Arrays aren t going to work. What can we do? Use pointers Copy a large section of a heap, with a single pointer assignment

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

ECE 2400 Computer Systems Programming Fall 2017 Topic 10: Sorting Algorithms

Computers in Engineering COMP 208. Where s Waldo? Linear Search. Searching and Sorting Michael A. Hawker

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

Transcription:

Question 1) CS 112 Practice Midterm Solutions class TreeNode<T> void set_left (TreeNode<T> new_left); // Update lchild to point to new_left void set_data (T new_data); // Set the data field to new_data T data; TreeNode<T> lchild; TreeNode<T> rchild; TreeNode( TreeNode<T> newleft, TreeNode<T> newright, T newdata ) lchild = newleft; rchild = newright; data = newdata; public class TreeNodeClient public static void main( String [] args ) // Create an instance of TreeNode with String data TreeNode<String> tree = new TreeNode<String>( null, null, cs112 );

Question 2a) The stack stored in an array, s.pop(); // = 10 s.push(41); s.push(23); s.pop(); // = 41 s.push(31); 55 10 s.size = 2 55 s.size = 1 55 41 s.size = 2 55 41 23 s.size = 3 55 41 s.size = 2 55 41 31 s.size = 3 Question 2b) (consistent with class version) The queue stored in an array, 55 10 7 q.front = 0, q.rear = 3 q.enqueue(43) 55 10 7 43 q.front = 0, q.rear = 4 q.dequeue() // = 55 10 7 43 q.front = 1, q.rear = 4 q.dequeue() // = 10 7 43 q.front = 2, q.rear = 4 q.dequeue() = 7 43 q.front = 3, q.rear = 4 q.enqueue(9) 43 9 q.front = 3, q.rear = 0 q.enqueue(3) 3 43 9 q.front = 3, q.rear = 1

Question 2c) q.size = (q.front <= q.rear)? (q..rear q.front) : (q.rear + (N q.front)); OR using modulo arithmetic, q.size = Math.mod( q.rear - q.front, N ); Examples of modulo operation: mod( 2, 5 ) = 2 mod( -2, 5 ) = 5 2 = 3 mod( -3, 5 ) = 5 3 = 2 Question 2b) (inconsistent with class version only for reference) The queue stored in an array, 55 10 7 q.front = 0, q.rear = 2 q.enqueue(43) 55 10 7 43 q.front = 4, q.rear = 2 q.dequeue() = 7 55 10 43 q.front = 4, q.rear = 1 q.dequeue() = 10 55 43 q.front = 4, q.rear = 0 q.dequeue() = 55 43 q.front = 4, q.rear = 4 q.enqueue(9) 9 43 q.front = 3, q.rear = 4 q.enqueue(3) 3 9 43 q.front = 2, q.rear = 4 Question 2c) q.size = (q.front <= q.rear)? (q..rear q.front + 1) : ((q.rear + 1) + (N q.front)); OR using modulo arithmetic, q.size = Math.mod( q.rear - q.front, N ) + 1;

Question 3) (1) void quicksort(item [] a, int l, int r) (2) if (r >= l) return; // base case, incorrect, fix below (3) int i = partition (a, l, r); // quicksort the right side, then the left side (4) quicksort(a, i+1, r); (5) quicksort(a, l, i-1); (6) int partition(item [] a, int l, int r) (7) int i = l, j = r-1, Item v = a[r]; (8) for (;;) // find the next i such that a[i] is larger than v (9) while (a[ i ] <= v) // bounds checking missing (10) i++; // find the previous j such that a[j] is smaller than v // stop if j goes outside the array bounds. (11) while (v < a[j]) (12) j--; (13) if (j == l-1) break; if (i >= j) break; // pointers crossed (14) else swap (a[i], a[j]); // swap (a[ i ], a[ r ]); missing here (15) return i; Solution: Note from the pair of while loops: all a[i] <= pivot v should be in the left partition and a[i] > pivot v should be in the right partition. (2) if ( r <= l ) return; // base case should check for length <= 1 (9) while ( i < r && a[ i ] <= v ) // need bounds checking for i (14-15) swap (a[ i ], a[ r ]); // move the pivot value to the end of left // partition, so that we satisfy the condition // a[ l to (i 1) ] <= a[ i ] < a[ (i + 1) to r ] // we can now call quicksort on // the two partitions.

Complete test code: package test; import java.util.*; public class TestSort public static void main(string[] args) int [] arr = new int[1000]; Random rand = new Random(1234); for(int i = 0; i < arr.length; i++) arr[i] = rand.nextint(arr.length/10); quicksort(arr, 0, arr.length - 1); System.out.println(Arrays.toString(arr)); static void quicksort(int [] a, int l, int r) if (r <= l) return; // base case int i = partition (a, l, r); // quicksort the right side, then the left side quicksort(a, i+1, r); quicksort(a, l, i-1); static int partition(int [] a, int l, int r) int i = l, j = r-1, v = a[r]; for (;;) // find the next i such that a[i] is larger than v while ( i < r && a[ i ] <= v ) i++; // find the previous j such that a[j] is smaller than v // stop if j goes outside the array bounds. while (v < a[j]) j--; if (j == l-1) break; if (i >= j) break; // pointers crossed else //swap (a[i], a[j]); int temp = a[i]; a[i] = a[j]; a[j] = temp; // swap(a[i], a[r]); int temp = a[i]; a[i] = a[r]; a[r] = temp; return i;

Question 5) public class ListNode int data; ListNode next; 5a) remove duplicates in sorted linked list public removeduplicatessorted( ListNode p ) while( p!= null ) while( p.next!= null && p.data == p.next.data ) p.next = p.next.next; p = p.next; // this pair of loops is linear: in each while loop iteration we either // (a) delete a node or (b) move to the next node. 5b) remove duplicates in unsorted linked list public removeduplicatesunsorted( ListNode p ) while( p!= null ) // Remove all duplicates in list that match p // Need to check all nodes succeeding p since list is unsorted ListNode q = p; // start with p to allow next node delete while( q!= null ) // Is next node to q a duplicate for p? if( q.next!= null && q.next.data == p.data ) q.next = q.next.next; // remove successive duplicates else q = q.next; // advance q to next node (non-duplicate) p = p.next; // advance p

Analysis: The best case for unsorted is when all elements are duplicates, in this case inner loop for q does one pass removing duplicates and we are done => O(N) operations. The worst case is with a linked list of all distinct elements. In this case, the inner loop for q scans the following number of nodes, N nodes in outer iteration 1, (N 1) nodes in outer iteration 2, (N 2) nodes in outer iteration 3,.. 1 node in outer iteration N, The total computation is 1 + 2 + 3 +. + N is O( N^2 ) The unsorted algorithm is hence O( N^2 )