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

Similar documents
Faster Sorting Methods

Programming II (CS300)

Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang

Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Acknowledgement. Outline

Unit-2 Divide and conquer 2016

L14 Quicksort and Performance Optimization

9/10/12. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

Better sorting algorithms (Weiss chapter )

Divide-and-Conquer. Dr. Yingwu Zhu

Divide and Conquer. Algorithm Fall Semester

Overview of Sorting Algorithms

We can use a max-heap to sort data.

CSC 273 Data Structures

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

Solutions to Problem 1 of Homework 3 (10 (+6) Points)

CSE 373: Data Structures and Algorithms

4. Sorting and Order-Statistics

Introduction to Algorithms February 24, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Handout 8

CS 310 Advanced Data Structures and Algorithms

Name: Lirong TAN 1. (15 pts) (a) Define what is a shortest s-t path in a weighted, connected graph G.

COMP Data Structures

S O R T I N G Sorting a list of elements implemented as an array. In all algorithms of this handout the sorting of elements is in ascending order

CSC Design and Analysis of Algorithms

Sorting and Searching Algorithms

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

ITEC2620 Introduction to Data Structures

Searching in General

SORTING, SETS, AND SELECTION

Programming II (CS300)

Sorting. Weiss chapter , 8.6

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

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

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

Multiple-choice (35 pt.)

Quick Sort. CSE Data Structures May 15, 2002

CS 171: Introduction to Computer Science II. Quicksort

CSE 373 NOVEMBER 8 TH COMPARISON SORTS

Homework #3. CMSC351 - Spring 2013 PRINT Name :

Divide and Conquer 4-0

Sorting Algorithms. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Sorting. CSE 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation. CSE143 Au

Divide-and-Conquer. The most-well known algorithm design strategy: smaller instances. combining these solutions

Lecture 7 Quicksort : Principles of Imperative Computation (Spring 2018) Frank Pfenning

CSE 373: Data Structures and Algorithms

Chapter 10. Sorting and Searching Algorithms. Fall 2017 CISC2200 Yanjun Li 1. Sorting. Given a set (container) of n elements

Sorting. CSC 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation CSC Picture

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

Data structures. More sorting. Dr. Alex Gerdes DIT961 - VT 2018

SORTING AND SELECTION

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

Technical Interviews Kenny Yu

Quicksort (Weiss chapter 8.6)

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

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

Chapter 4. Divide-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Sorting. Order in the court! sorting 1

Lecture Notes on Quicksort

Key question: how do we pick a good pivot (and what makes a good pivot in the first place)?

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

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

Giri Narasimhan. COT 5993: Introduction to Algorithms. ECS 389; Phone: x3748

9. The Disorganized Handyman

Sorting and Searching

Lecture Notes on Quicksort

UNIT 7. SEARCH, SORT AND MERGE

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

Sorting. Order in the court! sorting 1

CSC Design and Analysis of Algorithms. Lecture 5. Decrease and Conquer Algorithm Design Technique. Decrease-and-Conquer

2/14/13. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

Bubble sort is so named because the numbers are said to bubble into their correct positions! Bubble Sort

Classic Data Structures Introduction UNIT I

Introduction. e.g., the item could be an entire block of information about a student, while the search key might be only the student's name

CIS 121 Data Structures and Algorithms with Java Spring Code Snippets and Recurrences Monday, January 29/Tuesday, January 30

Algorithms and Data Structures (INF1) Lecture 7/15 Hua Lu

Intro to Algorithms. Professor Kevin Gold

Divide and Conquer Algorithms: Advanced Sorting. Prichard Ch. 10.2: Advanced Sorting Algorithms

BM267 - Introduction to Data Structures

O(n): printing a list of n items to the screen, looking at each item once.

Sorting. Bringing Order to the World

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

Algorithms and Applications

CS1 Lecture 30 Apr. 2, 2018

Chapter 1 Divide and Conquer Algorithm Theory WS 2014/15 Fabian Kuhn

Sorting & Searching (and a Tower)

Sorting and Selection

11/8/2016. Chapter 7 Sorting. Introduction. Introduction. Sorting Algorithms. Sorting. Sorting

Lecture Notes 14 More sorting CSS Data Structures and Object-Oriented Programming Professor Clark F. Olson

COSC242 Lecture 7 Mergesort and Quicksort

CP222 Computer Science II. Searching and Sorting

Chapter 3:- Divide and Conquer. Compiled By:- Sanjay Patel Assistant Professor, SVBIT.

Sorting (I) Hwansoo Han

Searching and Sorting

CS-A1140 Data structures and algorithms

Real-world sorting (not on exam)

CS61B, Spring 2003 Discussion #15 Amir Kamil UC Berkeley 4/28/03

CSE 2123 Sorting. Jeremy Morris

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

Topics Applications Most Common Methods Serial Search Binary Search Search by Hashing (next lecture) Run-Time Analysis Average-time analysis Time anal

Topics Recursive Sorting Algorithms Divide and Conquer technique An O(NlogN) Sorting Alg. using a Heap making use of the heap properties STL Sorting F

University of the Western Cape Department of Computer Science

Transcription:

CISC 4080 Computer Algorithms Spring, 2019 Homework Assignment #3 1 (5 pts) Demonstrate how mergesort works when sorting the following list of numbers: 6 1 4 2 3 8 7 5

2 Given the following array (list), follows the following pseudocode to partition the array using the first element, 6. For your reference, here is an article on quicksort with python code: http://interactivepython.org/courselib/static/pythonds/sortsearch/thequicksort.html // partition list A[startI...endI] using // A[startI] (first element in the sublist) Partition (A, starti, endi) 1. pivot = A[startI] //set first element as pivot value 2. left = starti+1; //left index 3. right = endi; //right index 4. do { 5. // increment left until we find a value larger than pivot or 5. // until left==right 5. while (a[left]<=pivot && left<=right) //TYPO in orignal version 6. left++ 7. 8. // decrement right until we find a value smaller than pivot or until 9. // lef==right 10. while (a[right]>=pivot && left<=right) 11. right-- 12. 13. if (left<right) 14. swap (a[left], a[right]) 15. } while (left<=right) 16. 17. if (right!=starti) 18. swap (a[right], a[starti]) 19. 20. return right //the index of pivot value in the array 6 1 4 2 3 8 7 5 (1) Show the array after each time swap (line 14 and line 18 above) is executed. 2

(2). Consider that the array might contain duplicates (including multiple elements equal to the pivot value), add a few lines of codes after line 19 in the above pseudocode so that all pivot values are moved to the middle, and the function returns the starting and ending index of the middle section that contains elements that are equal to the pivot. (3). Read the DPV book section on Selection (page 60), and comment on where the 2nd smallest element should be, i.e., the left partition, middle partition or right partition of the above partition result? 3

3 Efficient exponentiation algorithm below. (a) Finish (or correct) the in-class exercise below. (Hint: you can type in your code and test it). // return a^n, i.e., raise a to the power of n // a is an integer, n is a natural number Exp (int a, int ) //1. write the base case here (for what n value, we can just solve it without // making recursive call result = Exp (a, n/2) //n/2 is integer division if n is odd // fill in what to do here else // fill in what to do here (b) Recall that to verify a recursive algorithm is correct, three questions need to be asked. Answer the three questions for the above pseudocode to show your above algorithm is correct. (c) Analyze the above algorithm s running time. 4

4 Counting Sort, Radix Sort Reference: https://storm.cis.fordham.edu/zhang/cs4080/demo/radixsort.cpp (a) Radix sort works on integers as follows: first sort the data using the ones digit (as the key), then sort the data using the tens digit,... and lastly sort the data using the most significant digit. Follow the example given in class (and in slides) to demonstrate how the following list of integers are sorted using radix sort: 149, 99, 321, 264, 501, 249, 354, 123 (b) Why we cannot reverse the order of radix sorting, i.e., sort using the highest digit, and then the second highest digit, and lastly sort using the ones digit? (c) If we want to consider the most significant digit first, we could use bucket sort, by first put all elements with 1 in 100s digit in a bucket, all elements with 2 in 100s digit in second bucket,... How would you complete the sorting algorithm? 5

5 Suppose a list P [1...n] gives the daily stock price of a certain company for a duration of n days, we want to find an optimal day d 1 to buy the stock and then a later day d 2 to sell the stock, so that the profit is maximized (i.e., P [d 2 ] P [d 1 ] is maximized. For example, if P [1...6] = 20, 10, 30, 50, 5, 14 Then we should buy in at day 2, and sell at day 4 to earn profit of 50 10 per share. (Simply buying at the lowest point and selling at the highest point will not necessarily work, as one have to buy before sell.) (a) Think about how you solve this problem algorithmically, and write down the pseudocode and analyze the running time in terms of input size n (the length of the stock price data). Hint: A good starting point to any problem is try brute-force approach first, i.e., try all possible pairs of buy/sell days, and see which one yields largest profit. (b) Think about how to solve this problem more efficiently using divide-and-conquer paradigm, and describe your algorithm in pseudocode. Hint: Consider the following analysis of the problem, and translate this into a pseudocode. For an input of length n (i.e., prices of the stock for n consecutive days), the optimal way to make profit is the best among the following three possibilities: i. either the optimal buy/sell days are both in the first half of the n days (i.e., left half of the array). ii. or the optimal buy/sell days are both in the second half of the n days (i.e., left half of the array). iii. or the optimal days is buy in the first half of the n days, and sell in the second half of the n days. Note that for possibility 1 and 2 above, they are just solutions to the two subproblems (obtained by cutting the array into two halves), while the solution to the third possibility can be found by searching for the lowest price among first half of the array, and highest price among second half of the array. (c) (Optional) Implement both algorithms above, and test them using the the following data given in CVS file (downloaded from Yahoo finance), using the Open (second column) price for each data to calculate the optimal buy/sell dates. http://storm.cis.fordham.edu/zhang/cs4080/data/goog.csv 6