CS 360 Exam 1 Fall 2014 Solution. 1. Answer the following questions about each code fragment below. [8 points]
|
|
- Thomasina Stanley
- 5 years ago
- Views:
Transcription
1 CS 360 Exam 1 Fall 2014 Solution 1. Answer the following questions about each code fragment below. [8 points] for (v=1; v<=n; v++) for (w=1; w<=v; w++) for (x=v; x<=n; x++) for (y=w; y<=x; y++) z++; for (r=1; r<=n; r++) for (s=n+1; s<=2*n; s++) for (t=r; t<=s; t++) u++; class A { int F( ) { return 3; int G( ) { return 3 + F( ); int H( ) { return 6 + G( ); class B extends A { int H( ) { return 7 + G( ); class C extends B { int G( ) { return 5 + F( ); class D extends C { int F( ) { return 6; class E extends D { int H( ) { return 9 + G( ); A b, c, d, e; b = new B( ); b.h( ); c = new C( ); c.h( ); d = new D( ); d.h( ); e = new E( ); e.h( ); If n=3, how many total times is z incremented? 20 If n=2, how many total times is z incremented? 6 If n=2, how many total times is u incremented? 12 If n=3, how many total times is u incremented? 36 This code is object-oriented. Therefore it uses both inheritance and late method binding (also known as dynamic method binding or dynamic dispatch). What value is returned from the call b.h( )? 13 What value is returned from the call c.h( )? 15 What value is returned from the call d.h( )? 18 What value is returned from the call e.h( )? 20
2 2. Evaluate each expression and write the answer in simplest form. [12 points] 100 mod 13 = 9 13 mod 100 = mod 25 = 2 9 mod 25 = 512 mod 25 = 12 3 lg 256 = 8/3 lg 80 + lg 48 lg 30 = lg (80*48/30) = lg 128 = mod 25 = k=100 k = Arithmetic series 11 k=0 2 kk = 4095 Finite geometic series k=0 4 kk = 4/3 Infinite geometic series log3 243 = 5 log = 3 log16 2 = 1/4 3. Questions about running times. [4 points] First suppose we run a program, and we find that T(2) = 0.01 seconds and T(8) = seconds. a. If T(n) = c k n, determine the value of constant k. T(8) / T(2) = k 8 / k 2 = / 0.01 k 6 = 4096 k=4 b. If T(n) = c n k, determine the value of constant k. T(8) / T(2) = (8/2) k = / k = 4096 k=6 Next we run a different program using an input of size 100, and we find that T(100) = 5 seconds. c. If T(n) = c n 3 for some constant c, then determine T(200). 5*(200/100) 3 = 40 seconds d. If T(n) = c n for some constant c, determine the value of n0 such that T(n0) = 15 seconds. n0 = 100*(15/5) 2 = 900
3 4. Suppose f(n) and g(n) are related by at least one of the relations O, o, θ, Ω, ω. Match each condition below to its equivalent relation. [13 points] A. f(n) is O(g(n)) B. f(n) is o(g(n)) C. f(n) is θ(g(n)) D. f(n) is Ω(g(n)) E. f(n) is ω(g(n)) F. Contradictory or impossible situation A C F C F A D E B F E B D f(n) is θ(g(n)) or f(n) is not Ω(g(n)). f(n) is not o(g(n)) and f(n) is not ω(g(n)). f(n) is Ο(g(n)) and f(n) is ω(g(n)). f(n) is Ο(g(n)) and f(n) is Ω(g(n)). f(n) is Ω(g(n)) and f(n) is o(g(n)). f(n) is θ(g(n)) or f(n) is o(g(n)). f(n) is θ(g(n)) or f(n) is ω(g(n)). f(n) is not θ(g(n)) and f(n) is not o(g(n)). f(n) is not θ(g(n)) and f(n) is not ω(g(n)). f(n) is not Ο(g(n)) and f(n) is not Ω(g(n)). f(n) is not O(g(n)). f(n) is not Ω(g(n)). f(n) is θ(g(n)) or f(n) is not Ο(g(n)).
4 5. State the running time of each code fragment as a simplest θ function of n. [9 points] for (k=1; k<=n*n*n; k++) θ(n 3 ) for (k=2; k<=n; k*=k) θ(lg lg n) for (j=1; j<=n; j++) if (j>100 && j<n 100) for (k=1; k<=n; k++) print (j, k); θ(n 2 ) m=1; for (j=1; j<=n; j++) m*=m; for (k=1; k<=m; k++) θ(n) for (k=1; k*k*k <=n; k++) 33 θ( nn) for (k=1; k<=n; k+=k) θ(lg n) for (j=1; j<=n; j++) if (j<100 j>n 100) for (k=1; k<=n; k++) print (j, k); θ(n) m=2; for (j=1; j<=n; j++) m*=m; for (k=1; k<=m; k++) θ(22 22nn ) x=0; y=1; for (z=1; z<=n; x+=6, y+=x, z+=y) print (z); 33 z = 1, 8, 27, 64, 125,, n θ( nn)
5 6. Questions about divide-and-conquer recurrences. [9 points] Solve each recurrence, and write the solution as a simplest θ function of n: a. T(n) = 625 T(n/5) + n 4. T(n) = θ(n 4 lg n) b. T(n) = 1000 T(n/10) + n 4. T(n) = θ(n 4 ) c. T(n) = 32 T(n/2) + n 4. T(n) = θ(n 5 ) Toom-k is a divide-and-conquer algorithm for polynomial multiplication that works as follows: Split each n-term polynomial into k pieces that each have n/k terms. Perform 2k 1 recursive multiplications, along with a constant number of additions and/or subtractions on polynomials that each have n/k terms. d. When k=3, we have Toom-3 or Toom-Cook. Write a recurrence for T(n), and also solve for T(n). T(n) = 5 T(n/3) + θ(n) T(n) = θ(nn llllll ) e. Next consider Toom-k with arbitrary k. Write a recurrence for T(n), and also solve for T(n). T(n) = (2k 1) T(n/k) + θ(n) T(n) = θ(nn llllll kk( ) ) f. Show that for each ε>0 there exist values of k such that Toom-k has running time o(n 1+ε ). Also determine such values of k expressed in terms of ε. From part (e), the exponent logk (2k 1) < logk (2k) = logk k + logk 2 = 1 + logk 2 < 1+ε whenever logk 2 < ε 2 < k ε k > 2 1/ε
6 7. Use the array [9, 5, 8, 4, 7, 3, 6, 2] as input for each sorting algorithm below. [6 points] Which choice shows the array contents after two passes of insertion sort? A Which choice shows the array contents after four passes of insertion sort? H Which choice shows the array contents after two passes of selection sort? D Which choice shows the array contents after four passes of selection sort? E A. [5, 8, 9, 4, 7, 3, 6, 2] B. [4, 3, 5, 2, 6, 7, 8, 9] C. [4, 5, 3, 6, 2, 7, 8, 9] D. [2, 3, 8, 4, 7, 5, 6, 9] E. [2, 3, 4, 5, 7, 8, 6, 9] F. [4, 5, 8, 9, 7, 3, 6, 2] G. [5, 4, 7, 3, 6, 2, 8, 9] H. [4, 5, 7, 8, 9, 3, 6, 2] I. [9, 5, 8, 4, 2, 3, 6, 7] J. [2, 3, 4, 8, 7, 5, 6, 9] K. None of the above Which choice shows the array contents after two passes of bubble sort? G Which choice shows the array contents after four passes of bubble sort? B 8. Use the input array [237, 564, 267, 534, 567, 234, 537, 264]. [4 points] Which choice shows the array after the first iteration of an (ascending-order) radix sort using radix r=10? C Which choice shows the array after the second iteration of an (ascending-order) radix sort using radix r=10? E Which choice shows the array after the first iteration of a descending-order radix sort using radix r=10? B Which choice shows the array after the second iteration of a descending-order radix sort using radix r=10? D A. [264, 537, 234, 567, 534, 267, 564, 237] B. [237, 267, 567, 537, 564, 534, 234, 264] C. [564, 534, 234, 264, 237, 267, 567, 537] D. [267, 567, 564, 264, 237, 537, 534, 234] E. [534, 234, 237, 537, 564, 264, 267, 567] F. [237, 267, 234, 264, 564, 534, 567, 537] G. [237, 234, 534, 537, 267, 264, 564, 567] H. [564, 534, 567, 537, 237, 267, 234, 264] I. [564, 567, 267, 264, 534, 537, 237, 234] J. [234, 237, 264, 267, 534, 537, 564, 567] K. [567, 564, 537, 534, 267, 264, 237, 234] L. None of the above
7 9. Use the array [7, 5, 8, 4, 1, 6, 2, 0, 3] as input for each sorting algorithm below. [7 points] Which choice shows the two subarray parameters sent to the two recursive calls of merge sort? E Which choice shows the two subarrays returned from the two recursive calls of merge sort? G For the following, choose the pivot using median-of- 3 with the low, middle, and high indexes. What is the pivot value? 3 Which choice shows the two subarray parameters sent to the two recursive calls of (not-in-place) quick sort? C A. [0, 1, 2, 3] and [4, 5, 6, 7, 8] B. [0, 1, 2, 3] and [5, 6, 7, 8] C. [1, 2, 0] and [7, 5, 8, 4, 6] D. [4, 5, 7, 8] and [0, 1, 2, 3, 6] E. [7, 5, 8, 4, 1] and [6, 2, 0, 3] F. [3, 0, 2, 1] and [4, 6, 8, 5, 7] G. [1, 4, 5, 7, 8] and [0, 2, 3, 6] H. [0, 1, 2, 3, 4] and [5, 6, 7, 8] I. [7, 5, 8, 4] and [1, 6, 2, 0, 3] J. [0, 1, 2] and [4, 5, 6, 7, 8] K. None of the above Which choice shows the two subarrays returned from the two recursive calls of (not-in-place) quick sort? J Which choice shows the two subarray parameters sent to the two recursive calls of in-place quick sort? F Which choice shows the two subarrays returned from the two recursive calls of in-place quick sort? A 10. Questions about sorting algorithms. [4 points] Which algorithm has θ(n lg n) worst-case running time and is also stable? G Which algorithm has θ(n lg n) worst-case running time and is also in-place? A Which two algorithms are simultaneously both stable and in-place? D and F A. Heap sort B. Counting sort C. Bin sort D. Insertion sort E. Selection sort F. Bubble sort G. Merge sort H. Quick sort I. Radix sort J. None of the above
8 11. Use the input array [5, 7, 3, 6, 8, 4, 2]. [4 points] Which choice shows the result after inserting each given array element (in the given order) into an array-based minordered binary heap? E Which choice shows the result after inserting each given array element (in the given order) into an array-based maxordered binary heap? G Which choice shows the result after applying Build-Min- Heap to the given input array? D Which choice shows the result after applying Build-Max- Heap to the given input array? F A. [2, 4, 8, 6, 3, 7, 5] B. [2, 3, 4, 5, 6, 7, 8] C. [8, 7, 6, 5, 4, 3, 2] D. [2, 6, 3, 7, 8, 4, 5] E. [2, 6, 3, 7, 8, 5, 4] F. [8, 7, 4, 6, 5, 3, 2] G. [8, 7, 4, 5, 6, 3, 2] H. [2, 3, 6, 4, 5, 7, 8] I. [2, 3, 6, 5, 4, 7, 8] J. [8, 4, 7, 3, 2, 6, 5] K. [8, 4, 7, 3, 2, 5, 6] L. None of the above
9 12. Suppose you are given a sorted list of n elements followed by f(n) additional randomly ordered elements. Make no assumptions about the type or range of the elements. [10 points] a. Explain how to sort the entire list in O(n) time when f(n) is O(1). Justify your answer. Insert each of the additional f(n) elements, as in insertion sort. This takes O(f(n))*O(n) = O(1)*O(n) = O(n) total time. b. Explain how to sort the entire list in O(n) time when f(n) is O( n). Justify your answer. First sort the additional f(n) elements using insertion sort or selection sort or bubble sort. Then merge the original n elements with the additional f(n) elements, as in merge sort. The first step takes O( nn 2 ) = O(n) time, and the second step takes O(n + f(n)) = O(n) time. c. How large can f(n) be for the entire list still to be sortable in O(n) time? Justify your answer. First sort the additional f(n) elements using either merge sort or heap sort. Then merge the original n elements with the additional f(n) elements, as in merge sort. The first step takes O(f(n) lg f(n)) time, and the second step takes O(n + f(n)) time. If the total time must be O(n), then we must have f(n) is O(n / lg n). Because O((n / lg n) lg (n / lg n)) = O((n / lg n) (lg n lg lg n)) = O((n / lg n) (lg n)) = O(n).
10 13. Divide-and-conquer: [15 points] a. First write the in-place partition function that is used by the in-place quick sort algorithm. You may then call this function as a helper function in parts (b) and (c) below. Partition (A[ ], low, high, pivot) { j=low; k=high; while (true) { while (A[j]<pivot) j++; while (A[k]>pivot) k--; if (k<=j) return k; swap (A[j], A[k]); j++; k--; b. Write a recursive divide-and-conquer algorithm for the selection problem that runs in O(n) average-case time and that uses O(lg n) average-case extra space beyond the input array. Select (A[1 n], k) { // where 1 k n Select (A, k, 1, n); Select (A[ ], k, low, high) { if (low==high) return A[low]; pivot = median (A[low], A[(low+high)/2], A[high]); index = Partition (A, low, high, pivot); if (k<=index low+1) return Select (A, k, low, index); else return Select (A, k index+low 1, index+1, high); // or other strategy c. Write an algorithm for the selection problem that runs in O(n) average-case time and that uses O(1) extra space beyond the input array. Hint: each function call uses some space. Select (A[1 n], k) { // where 1 k n low=1; high=n; while (low<high) { pivot = median (A[low], A[(low+high)/2], A[high]); index = Partition (A, low, high, pivot); if (k<=index low+1) high=index; else { k=k index+low 1; low=index+1; return A[low]; // or other strategy
1. Answer the following questions about each code fragment below. [8 points]
CS 360 Exam 1 Fall 2014 Solution 1. Answer the following questions about each code fragment below. [8 points] for (v=1; v
More information1. Answer the following questions about each code fragment below. [8 points]
CS 360 Exam 1 Fall 2014 Solution 1. Answer the following questions about each code fragment below. [8 points] for (r=1; r
More informationCS 360 Exam 1 Fall 2014 Name. 1. Answer the following questions about each code fragment below. [8 points]
CS 360 Exam 1 Fall 2014 Name 1. Answer the following questions about each code fragment below. [8 points] for (v=1; v
More informationChapter 2: Complexity Analysis
Chapter 2: Complexity Analysis Objectives Looking ahead in this chapter, we ll consider: Computational and Asymptotic Complexity Big-O Notation Properties of the Big-O Notation Ω and Θ Notations Possible
More informationWe can use a max-heap to sort data.
Sorting 7B N log N Sorts 1 Heap Sort We can use a max-heap to sort data. Convert an array to a max-heap. Remove the root from the heap and store it in its proper position in the same array. Repeat until
More informationPROGRAM EFFICIENCY & COMPLEXITY ANALYSIS
Lecture 03-04 PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS By: Dr. Zahoor Jan 1 ALGORITHM DEFINITION A finite set of statements that guarantees an optimal solution in finite interval of time 2 GOOD ALGORITHMS?
More information4.4 Algorithm Design Technique: Randomization
TIE-20106 76 4.4 Algorithm Design Technique: Randomization Randomization is one of the design techniques of algorithms. A pathological occurence of the worst-case inputs can be avoided with it. The best-case
More informationSorting. Bubble Sort. Pseudo Code for Bubble Sorting: Sorting is ordering a list of elements.
Sorting Sorting is ordering a list of elements. Types of sorting: There are many types of algorithms exist based on the following criteria: Based on Complexity Based on Memory usage (Internal & External
More informationThe Limits of Sorting Divide-and-Conquer Comparison Sorts II
The Limits of Sorting Divide-and-Conquer Comparison Sorts II CS 311 Data Structures and Algorithms Lecture Slides Monday, October 12, 2009 Glenn G. Chappell Department of Computer Science University of
More informationCS S-11 Sorting in Θ(nlgn) 1. Base Case: A list of length 1 or length 0 is already sorted. Recursive Case:
CS245-2015S-11 Sorting in Θ(nlgn) 1 11-0: Merge Sort Recursive Sorting Base Case: A list of length 1 or length 0 is already sorted Recursive Case: Split the list in half Recursively sort two halves Merge
More informationCS 303 Design and Analysis of Algorithms
Mid-term CS 303 Design and Analysis of Algorithms Review For Midterm Dong Xu (Based on class note of David Luebke) 12:55-1:55pm, Friday, March 19 Close book Bring your calculator 30% of your final score
More informationAnalysis of Algorithms. Unit 4 - Analysis of well known Algorithms
Analysis of Algorithms Unit 4 - Analysis of well known Algorithms 1 Analysis of well known Algorithms Brute Force Algorithms Greedy Algorithms Divide and Conquer Algorithms Decrease and Conquer Algorithms
More informationEECS 2011M: Fundamentals of Data Structures
M: Fundamentals of Data Structures Instructor: Suprakash Datta Office : LAS 3043 Course page: http://www.eecs.yorku.ca/course/2011m Also on Moodle Note: Some slides in this lecture are adopted from James
More informationCS:3330 (22c:31) Algorithms
What s an Algorithm? CS:3330 (22c:31) Algorithms Introduction Computer Science is about problem solving using computers. Software is a solution to some problems. Algorithm is a design inside a software.
More informationLecture 9: Sorting Algorithms
Lecture 9: Sorting Algorithms Bo Tang @ SUSTech, Spring 2018 Sorting problem Sorting Problem Input: an array A[1..n] with n integers Output: a sorted array A (in ascending order) Problem is: sort A[1..n]
More informationData Structures and Algorithms
Data Structures and Algorithms Session 24. Earth Day, 2009 Instructor: Bert Huang http://www.cs.columbia.edu/~bert/courses/3137 Announcements Homework 6 due before last class: May 4th Final Review May
More informationSorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?
Sorting Binary search works great, but how do we create a sorted array in the first place? Sorting in Arrays Sorting algorithms: Selection sort: O(n 2 ) time Merge sort: O(nlog 2 (n)) time Quicksort: O(n
More information[ 11.2, 11.3, 11.4] Analysis of Algorithms. Complexity of Algorithms. 400 lecture note # Overview
400 lecture note #0 [.2,.3,.4] Analysis of Algorithms Complexity of Algorithms 0. Overview The complexity of an algorithm refers to the amount of time and/or space it requires to execute. The analysis
More informationLecture 5: Sorting Part A
Lecture 5: Sorting Part A Heapsort Running time O(n lg n), like merge sort Sorts in place (as insertion sort), only constant number of array elements are stored outside the input array at any time Combines
More informationAlgorithm Analysis. Spring Semester 2007 Programming and Data Structure 1
Algorithm Analysis Spring Semester 2007 Programming and Data Structure 1 What is an algorithm? A clearly specifiable set of instructions to solve a problem Given a problem decide that the algorithm is
More informationCS302 Topic: Algorithm Analysis #2. Thursday, Sept. 21, 2006
CS302 Topic: Algorithm Analysis #2 Thursday, Sept. 21, 2006 Analysis of Algorithms The theoretical study of computer program performance and resource usage What s also important (besides performance/resource
More informationQuiz 1 Solutions. Asymptotic growth [10 points] For each pair of functions f(n) and g(n) given below:
Introduction to Algorithms October 15, 2008 Massachusetts Institute of Technology 6.006 Fall 2008 Professors Ronald L. Rivest and Sivan Toledo Quiz 1 Solutions Problem 1. Asymptotic growth [10 points]
More informationINTRODUCTION. Analysis: Determination of time and space requirements of the algorithm
INTRODUCTION A. Preliminaries: Purpose: Learn the design and analysis of algorithms Definition of Algorithm: o A precise statement to solve a problem on a computer o A sequence of definite instructions
More information4. Sorting and Order-Statistics
4. Sorting and Order-Statistics 4. Sorting and Order-Statistics The sorting problem consists in the following : Input : a sequence of n elements (a 1, a 2,..., a n ). Output : a permutation (a 1, a 2,...,
More informationCS S-02 Algorithm Analysis 1
CS245-2008S-02 Algorithm Analysis 1 02-0: Algorithm Analysis When is algorithm A better than algorithm B? 02-1: Algorithm Analysis When is algorithm A better than algorithm B? Algorithm A runs faster 02-2:
More informationThe divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems.
2.3 Designing algorithms There are many ways to design algorithms. Insertion sort uses an incremental approach: having sorted the subarray A[1 j - 1], we insert the single element A[j] into its proper
More informationDivide and Conquer. Algorithm D-and-C(n: input size)
Divide and Conquer Algorithm D-and-C(n: input size) if n n 0 /* small size problem*/ Solve problem without futher sub-division; else Divide into m sub-problems; Conquer the sub-problems by solving them
More informationPseudo code of algorithms are to be read by.
Cs502 Quiz No1 Complete Solved File Pseudo code of algorithms are to be read by. People RAM Computer Compiler Approach of solving geometric problems by sweeping a line across the plane is called sweep.
More informationSorting. Bringing Order to the World
Lecture 10 Sorting Bringing Order to the World Lecture Outline Iterative sorting algorithms (comparison based) Selection Sort Bubble Sort Insertion Sort Recursive sorting algorithms (comparison based)
More informationDesign and Analysis of Algorithms - - Assessment
X Courses» Design and Analysis of Algorithms Week 1 Quiz 1) In the code fragment below, start and end are integer values and prime(x) is a function that returns true if x is a prime number and false otherwise.
More informationJana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides
Jana Kosecka Linear Time Sorting, Median, Order Statistics Many slides here are based on E. Demaine, D. Luebke slides Insertion sort: Easy to code Fast on small inputs (less than ~50 elements) Fast on
More informationCSC 273 Data Structures
CSC 273 Data Structures Lecture 6 - Faster Sorting Methods Merge Sort Divides an array into halves Sorts the two halves, Then merges them into one sorted array. The algorithm for merge sort is usually
More informationData Structures and Algorithms CSE 465
Data Structures and Algorithms CSE 465 LECTURE 4 More Divide and Conquer Binary Search Exponentiation Multiplication Sofya Raskhodnikova and Adam Smith Review questions How long does Merge Sort take on
More informationThe divide and conquer strategy has three basic parts. For a given problem of size n,
1 Divide & Conquer One strategy for designing efficient algorithms is the divide and conquer approach, which is also called, more simply, a recursive approach. The analysis of recursive algorithms often
More informationBig-O-ology. Jim Royer January 16, 2019 CIS 675. CIS 675 Big-O-ology 1/ 19
Big-O-ology Jim Royer January 16, 2019 CIS 675 CIS 675 Big-O-ology 1/ 19 How do you tell how fast a program is? Answer? Run some test cases. Problem You can only run a few test cases. There will be many
More informationCOSC242 Lecture 7 Mergesort and Quicksort
COSC242 Lecture 7 Mergesort and Quicksort We saw last time that the time complexity function for Mergesort is T (n) = n + n log n. It is not hard to see that T (n) = O(n log n). After all, n + n log n
More informationData Structures and Algorithms Chapter 2
1 Data Structures and Algorithms Chapter 2 Werner Nutt 2 Acknowledgments The course follows the book Introduction to Algorithms, by Cormen, Leiserson, Rivest and Stein, MIT Press [CLRST]. Many examples
More informationData Structures and Algorithms. Part 2
1 Data Structures and Algorithms Part 2 Werner Nutt 2 Acknowledgments The course follows the book Introduction to Algorithms, by Cormen, Leiserson, Rivest and Stein, MIT Press [CLRST]. Many examples displayed
More informationCSC Design and Analysis of Algorithms
CSC 8301- Design and Analysis of Algorithms Lecture 6 Divide and Conquer Algorithm Design Technique Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide a problem instance into two
More informationCS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics
CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics 1 Sorting 1.1 Problem Statement You are given a sequence of n numbers < a 1, a 2,..., a n >. You need to
More informationCS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014
CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014 Study: Chapter 4 Analysis of Algorithms, Recursive Algorithms, and Recurrence Equations 1. Prove the
More informationCSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer
CSC 8301- Design and Analysis of Algorithms Lecture 6 Divide and Conquer Algorithm Design Technique Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide a problem instance into two
More informationO(n): printing a list of n items to the screen, looking at each item once.
UNIT IV Sorting: O notation efficiency of sorting bubble sort quick sort selection sort heap sort insertion sort shell sort merge sort radix sort. O NOTATION BIG OH (O) NOTATION Big oh : the function f(n)=o(g(n))
More informationDATA STRUCTURES AND ALGORITHMS
DATA STRUCTURES AND ALGORITHMS For COMPUTER SCIENCE DATA STRUCTURES &. ALGORITHMS SYLLABUS Programming and Data Structures: Programming in C. Recursion. Arrays, stacks, queues, linked lists, trees, binary
More informationCSE 3101: Introduction to the Design and Analysis of Algorithms. Office hours: Wed 4-6 pm (CSEB 3043), or by appointment.
CSE 3101: Introduction to the Design and Analysis of Algorithms Instructor: Suprakash Datta (datta[at]cse.yorku.ca) ext 77875 Lectures: Tues, BC 215, 7 10 PM Office hours: Wed 4-6 pm (CSEB 3043), or by
More informationCS 5321: Advanced Algorithms Sorting. Acknowledgement. Ali Ebnenasir Department of Computer Science Michigan Technological University
CS 5321: Advanced Algorithms Sorting Ali Ebnenasir Department of Computer Science Michigan Technological University Acknowledgement Eric Torng Moon Jung Chung Charles Ofria Nishit Chapter 22 Bill 23 Martin
More informationChapter 4. Divide-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 4 Divide-and-Conquer Copyright 2007 Pearson Addison-Wesley. All rights reserved. Divide-and-Conquer The most-well known algorithm design strategy: 2. Divide instance of problem into two or more
More informationAlgorithms and Data Structures
Algorithms and Data Structures Spring 2019 Alexis Maciel Department of Computer Science Clarkson University Copyright c 2019 Alexis Maciel ii Contents 1 Analysis of Algorithms 1 1.1 Introduction.................................
More informationHow much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;
How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A; } if (n
More informationLecture 8: Mergesort / Quicksort Steven Skiena
Lecture 8: Mergesort / Quicksort Steven Skiena Department of Computer Science State University of New York Stony Brook, NY 11794 4400 http://www.cs.stonybrook.edu/ skiena Problem of the Day Give an efficient
More informationCS 171: Introduction to Computer Science II. Quicksort
CS 171: Introduction to Computer Science II Quicksort Roadmap MergeSort Recursive Algorithm (top-down) Practical Improvements Non-recursive algorithm (bottom-up) Analysis QuickSort Algorithm Analysis Practical
More informationDivide and Conquer Algorithms: Advanced Sorting
Divide and Conquer Algorithms: Advanced Sorting (revisit) Properties of Growth-rate functions(1/3) 1. You can ignore low-order terms in an algorithm's growth-rate function. O(n 3 +4n 2 +3n) it is also
More informationQuicksort. Repeat the process recursively for the left- and rightsub-blocks.
Quicksort As the name implies, this is the fastest known sorting algorithm in practice. It is excellent for average input but bad for the worst-case input. (you will see later). Basic idea: (another divide-and-conquer
More informationFinal Exam in Algorithms and Data Structures 1 (1DL210)
Final Exam in Algorithms and Data Structures 1 (1DL210) Department of Information Technology Uppsala University February 0th, 2012 Lecturers: Parosh Aziz Abdulla, Jonathan Cederberg and Jari Stenman Location:
More information9/10/12. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)
Outline Part 5. Computational Complexity (2) Complexity of Algorithms Efficiency of Searching Algorithms Sorting Algorithms and Their Efficiencies CS 200 Algorithms and Data Structures 1 2 (revisit) Properties
More informationPlotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1
CS241 - week 1 review Special classes of algorithms: logarithmic: O(log n) linear: O(n) quadratic: O(n 2 ) polynomial: O(n k ), k 1 exponential: O(a n ), a > 1 Classifying algorithms is generally done
More informationDivide and Conquer 4-0
Divide and Conquer 4-0 Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances recursively 3. Obtain
More informationIntroduction to Algorithms March 12, 2008 Massachusetts Institute of Technology Spring 2008 Professors Srini Devadas and Erik Demaine Quiz 1
Introduction to Algorithms March 12, 2008 Massachusetts Institute of Technology 6.006 Spring 2008 Professors Srini Devadas and Erik Demaine Quiz 1 Quiz 1 Do not open this quiz booklet until you are directed
More informationAnother Sorting Algorithm
1 Another Sorting Algorithm What was the running time of insertion sort? Can we do better? 2 Designing Algorithms Many ways to design an algorithm: o Incremental: o Divide and Conquer: 3 Divide and Conquer
More information17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer
Module 2: Divide and Conquer Divide and Conquer Control Abstraction for Divide &Conquer 1 Recurrence equation for Divide and Conquer: If the size of problem p is n and the sizes of the k sub problems are
More informationCOT 5407: Introduction to Algorithms. Giri Narasimhan. ECS 254A; Phone: x3748
COT 5407: Introduction to Algorithms Giri Narasimhan ECS 254A; Phone: x3748 giri@cis.fiu.edu http://www.cis.fiu.edu/~giri/teach/5407s17.html https://moodle.cis.fiu.edu/v3.1/course/view.php?id=1494 8/28/07
More informationMergesort again. 1. Split the list into two equal parts
Quicksort Mergesort again 1. Split the list into two equal parts 5 3 9 2 8 7 3 2 1 4 5 3 9 2 8 7 3 2 1 4 Mergesort again 2. Recursively mergesort the two parts 5 3 9 2 8 7 3 2 1 4 2 3 5 8 9 1 2 3 4 7 Mergesort
More informationProblem. 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.
Problem 5. Sorting Simple Sorting, Quicksort, Mergesort 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. 98 99 Selection Sort
More informationCS61B Lectures # Purposes of Sorting. Some Definitions. Classifications. Sorting supports searching Binary search standard example
Announcements: CS6B Lectures #27 28 We ll be runningapreliminarytestingrun for Project #2 on Tuesday night. Today: Sorting algorithms: why? Insertion, Shell s, Heap, Merge sorts Quicksort Selection Distribution
More informationSAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms
SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6 6.0 Introduction Sorting algorithms used in computer science are often classified by: Computational complexity (worst, average and best behavior) of element
More informationDivide and Conquer Algorithms: Advanced Sorting. Prichard Ch. 10.2: Advanced Sorting Algorithms
Divide and Conquer Algorithms: Advanced Sorting Prichard Ch. 10.2: Advanced Sorting Algorithms 1 Sorting Algorithm n Organize a collection of data into either ascending or descending order. n Internal
More informationFaster Sorting Methods
Faster Sorting Methods Chapter 9 Contents Merge Sort Merging Arrays Recursive Merge Sort The Efficiency of Merge Sort Iterative Merge Sort Merge Sort in the Java Class Library Contents Quick Sort The Efficiency
More informationModule 1: Asymptotic Time Complexity and Intro to Abstract Data Types
Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types Dr. Natarajan Meghanathan Professor of Computer Science Jackson State University Jackson, MS 39217 E-mail: natarajan.meghanathan@jsums.edu
More informationClassic Data Structures Introduction UNIT I
ALGORITHM SPECIFICATION An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. All algorithms must satisfy the following criteria: Input. An algorithm has zero
More informationCSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014
CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting Aaron Bauer Winter 2014 The main problem, stated carefully For now, assume we have n comparable elements in an array and we want
More informationCOMP Data Structures
COMP 2140 - Data Structures Shahin Kamali Topic 5 - Sorting University of Manitoba Based on notes by S. Durocher. COMP 2140 - Data Structures 1 / 55 Overview Review: Insertion Sort Merge Sort Quicksort
More informationSorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort
Principles of Imperative Computation V. Adamchik CS 15-1 Lecture Carnegie Mellon University Sorting Sorting Sorting is ordering a list of objects. comparison non-comparison Hoare Knuth Bubble (Shell, Gnome)
More informationAlgorithms in Systems Engineering ISE 172. Lecture 12. Dr. Ted Ralphs
Algorithms in Systems Engineering ISE 172 Lecture 12 Dr. Ted Ralphs ISE 172 Lecture 12 1 References for Today s Lecture Required reading Chapter 6 References CLRS Chapter 7 D.E. Knuth, The Art of Computer
More informationCpt S 122 Data Structures. Sorting
Cpt S 122 Data Structures Sorting Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Sorting Process of re-arranging data in ascending or descending order Given
More informationAsymptotic Analysis Spring 2018 Discussion 7: February 27, 2018
CS 61B Asymptotic Analysis Spring 2018 Discussion 7: February 27, 2018 1 Asymptotic Notation 1.1 Order the following big-o runtimes from smallest to largest. O(log n), O(1), O(n n ), O(n 3 ), O(n log n),
More informationBetter sorting algorithms (Weiss chapter )
Better sorting algorithms (Weiss chapter 8.5 8.6) Divide and conquer Very general name for a type of recursive algorithm You have a problem to solve. Split that problem into smaller subproblems Recursively
More informationAssignment 1 (concept): Solutions
CS10b Data Structures and Algorithms Due: Thursday, January 0th Assignment 1 (concept): Solutions Note, throughout Exercises 1 to 4, n denotes the input size of a problem. 1. (10%) Rank the following functions
More informationData Structures Lecture 8
Fall 2017 Fang Yu Software Security Lab. Dept. Management Information Systems, National Chengchi University Data Structures Lecture 8 Recap What should you have learned? Basic java programming skills Object-oriented
More informationQuicksort (Weiss chapter 8.6)
Quicksort (Weiss chapter 8.6) Recap of before Easter We saw a load of sorting algorithms, including mergesort To mergesort a list: Split the list into two halves Recursively mergesort the two halves Merge
More informationProgramming in Haskell Aug-Nov 2015
Programming in Haskell Aug-Nov 2015 LECTURE 11 SEPTEMBER 10, 2015 S P SURESH CHENNAI MATHEMATICAL INSTITUTE Measuring efficiency Measuring efficiency Computation is reduction Application of definitions
More informationDIVIDE & CONQUER. Problem of size n. Solution to sub problem 1
DIVIDE & CONQUER Definition: Divide & conquer is a general algorithm design strategy with a general plan as follows: 1. DIVIDE: A problem s instance is divided into several smaller instances of the same
More informationDesign and Analysis of Algorithms
Design and Analysis of Algorithms CSE 5311 Lecture 8 Sorting in Linear Time Junzhou Huang, Ph.D. Department of Computer Science and Engineering CSE5311 Design and Analysis of Algorithms 1 Sorting So Far
More informationMUHAMMAD FAISAL MIT 4 th Semester Al-Barq Campus (VGJW01) Gujranwala
MUHAMMAD FAISAL MIT 4 th Semester Al-Barq Campus (VGJW01) Gujranwala faisalgrw123@gmail.com Reference MCQ s For MIDTERM EXAMS CS502- Design and Analysis of Algorithms 1. For the sieve technique we solve
More informationSorting Algorithms. + Analysis of the Sorting Algorithms
Sorting Algorithms + Analysis of the Sorting Algorithms Insertion Sort What if first k elements of array are already sorted? 4, 7, 12, 5, 19, 16 We can shift the tail of the sorted elements list down and
More informationCS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK
CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK Page 1 UNIT I INTRODUCTION 2 marks 1. Why is the need of studying algorithms? From a practical standpoint, a standard set of algorithms from different
More informationQuiz 1 Practice Problems
Introduction to Algorithms: 6.006 Massachusetts Institute of Technology March 7, 2008 Professors Srini Devadas and Erik Demaine Handout 6 1 Asymptotic Notation Quiz 1 Practice Problems Decide whether these
More information1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1
Asymptotics, Recurrence and Basic Algorithms 1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1 2. O(n) 2. [1 pt] What is the solution to the recurrence T(n) = T(n/2) + n, T(1)
More informationCS 310 Advanced Data Structures and Algorithms
CS 310 Advanced Data Structures and Algorithms Sorting June 13, 2017 Tong Wang UMass Boston CS 310 June 13, 2017 1 / 42 Sorting One of the most fundamental problems in CS Input: a series of elements with
More informationCS61BL. Lecture 5: Graphs Sorting
CS61BL Lecture 5: Graphs Sorting Graphs Graphs Edge Vertex Graphs (Undirected) Graphs (Directed) Graphs (Multigraph) Graphs (Acyclic) Graphs (Cyclic) Graphs (Connected) Graphs (Disconnected) Graphs (Unweighted)
More informationAlgorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms
Algorithm Efficiency & Sorting Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms Overview Writing programs to solve problem consists of a large number of decisions how to represent
More informationSorting. There exist sorting algorithms which have shown to be more efficient in practice.
Sorting Next to storing and retrieving data, sorting of data is one of the more common algorithmic tasks, with many different ways to perform it. Whenever we perform a web search and/or view statistics
More informationCS 171: Introduction to Computer Science II. Quicksort
CS 171: Introduction to Computer Science II Quicksort Roadmap MergeSort Analysis of Recursive Algorithms QuickSort Algorithm Analysis Practical improvements Java Array.sort() methods Quick Sort Partition
More informationRemember to also pactice: Homework, quizzes, class examples, slides, reading materials.
Exam 1 practice problems Remember to also pactice: Homework, quizzes, class examples, slides, reading materials. P1 (MC) For all the questions below (except for the True or False questions), the answer
More informationQuiz 1 Practice Problems
Introduction to Algorithms: 6.006 Massachusetts Institute of Technology March 7, 2008 Professors Srini Devadas and Erik Demaine Handout 6 1 Asymptotic Notation Quiz 1 Practice Problems Decide whether these
More informationCS4311 Design and Analysis of Algorithms. Lecture 1: Getting Started
CS4311 Design and Analysis of Algorithms Lecture 1: Getting Started 1 Study a few simple algorithms for sorting Insertion Sort Selection Sort Merge Sort About this lecture Show why these algorithms are
More informationCS473 - Algorithms I
CS473 - Algorithms I Lecture 4 The Divide-and-Conquer Design Paradigm View in slide-show mode 1 Reminder: Merge Sort Input array A sort this half sort this half Divide Conquer merge two sorted halves Combine
More informationMultiple-choice (35 pt.)
CS 161 Practice Midterm I Summer 2018 Released: 7/21/18 Multiple-choice (35 pt.) 1. (2 pt.) Which of the following asymptotic bounds describe the function f(n) = n 3? The bounds do not necessarily need
More informationSorting. Task Description. Selection Sort. Should we worry about speed?
Sorting Should we worry about speed? Task Description We have an array of n values in any order We need to have the array sorted in ascending or descending order of values 2 Selection Sort Select the smallest
More information7. Sorting I. 7.1 Simple Sorting. Problem. Algorithm: IsSorted(A) 1 i j n. Simple Sorting
Simple Sorting 7. Sorting I 7.1 Simple Sorting Selection Sort, Insertion Sort, Bubblesort [Ottman/Widmayer, Kap. 2.1, Cormen et al, Kap. 2.1, 2.2, Exercise 2.2-2, Problem 2-2 19 197 Problem Algorithm:
More informationCS : Data Structures
CS 600.226: Data Structures Michael Schatz Nov 30, 2016 Lecture 35: Topological Sorting Assignment 10: Due Monday Dec 5 @ 10pm Remember: javac Xlint:all & checkstyle *.java & JUnit Solutions should be
More information