# Merge Sort Goodrich, Tamassia. Merge Sort 1

Save this PDF as:

Size: px
Start display at page:

## Transcription

1 Merge Sort Merge Sort 1

2 Divide-and-Conquer Divide-and conquer is a general algorithm design paradigm: Divide: divide the input data S in two disjoint subsets S 1 and S 2 Recur: solve the subproblems associated with S 1 and S 2 Conquer: combine the solutions for S 1 and S 2 into a solution for S The base case for the recursion are subproblems of size 0 or 1 Merge-sort is a sorting algorithm based on the divideand-conquer paradigm It accesses data in a sequential manner (suitable for sorting data on a disk) Merge Sort 2

3 Merge-Sort Merge-sort on an input sequence S with n elements consists of three steps: Divide: partition S into two sequences S 1 and S 2 of about n/2 elements each Recur: recursively sort S 1 and S 2 Conquer: merge S 1 and S 2 into a unique sorted sequence Merge Sort 3

4 Merging sorted arrays array A: array B: lowptr highptr workspace: j Merge Sort 4

5 Merging sorted arrays array A: array B: lowptr highptr workspace: 17 j Merge Sort 5

6 Merging sorted arrays array A: array B: lowptr highptr workspace: j Merge Sort 6

7 Merging sorted arrays array A: array B: lowptr highptr workspace: j Merge Sort 7

8 Merging sorted arrays array A: array B: lowptr highptr workspace: j Merge Sort 8

9 Merging sorted arrays array A: array B: lowptr highptr workspace: j Merge Sort 9

10 Merging sorted arrays array A: array B: 70 lowptr highptr workspace: j Merge Sort 10

11 Merging sorted arrays array A: array B: lowptr highptr workspace: j Merge Sort 11

12 Merging halves of an array Pass boundaries of sub-arrays to the algorithm instead of new arrays: l m m+1 r lowptr highptr Merge Sort 12

13 Implementation public static void recmergesort (int[] arr, int[] workspace, int l, int r) { if (l == r) { return; } else { int m = (l+r) / 2; recmergesort(arr, workspace, l, m); recmergesort(arr, workspace, m+1, r); merge(arr, workspace, l, m+1, r); } } Merge Sort 13

14 Merge-Sort Tree An execution of merge-sort is depicted by a binary tree each node represents a recursive call of merge-sort and stores unsorted sequence before the execution and its partition sorted sequence at the end of the execution the root is the initial call the leaves are calls on subsequences of size 0 or Merge Sort 14

15 Execution Example Partition Merge Sort 15

16 Execution Example (cont.) Recursive call, partition Merge Sort 16

17 Execution Example (cont.) Recursive call, partition Merge Sort 17

18 Execution Example (cont.) Recursive call, base case Merge Sort 18

19 Execution Example (cont.) Recursive call, base case Merge Sort 19

20 Execution Example (cont.) Merge Merge Sort 20

21 Execution Example (cont.) Recursive call,, base case, merge Merge Sort 21

22 Execution Example (cont.) Merge Merge Sort 22

23 Execution Example (cont.) Recursive call,, merge, merge Merge Sort 23

24 Execution Example (cont.) Merge Merge Sort 24

25 Analysis of Merge-Sort The height h of the merge-sort tree is O(log n) at each recursive call we divide in half the sequence, The overall amount or work done at the nodes of depth i is O(n) we partition and merge 2 i sequences of size n/2 i we make 2 i+1 recursive calls Thus, the total running time of merge-sort is O(n log n) depth 0 1 i #seqs i size n n/2 n/2 i Merge Sort 25

26 Using merge sort Fast sorting method for arrays: this is what Java s Arrays.sort uses, for example Good for sorting data in external memory because works with adjacent indices in the array Not so good with lists: relies on constant time access to the middle of the sequence Merge Sort 26

27 Quick-Sort Merge Sort 27

28 Quick-Sort Quick-sort is a randomized sorting algorithm based on the divide-and-conquer paradigm: x Divide: pick a random element x (called pivot) and partition S into x L elements less than x E elements equal x L E G G elements greater than x Recur: sort L and G Conquer: join L, E and G x Merge Sort 28

29 Partition of lists (or using extra workspace) We partition an input sequence as follows: We remove, in turn, each element y from S and We insert y into L, E or G, depending on the result of the comparison with the pivot x Each insertion and removal is at the beginning or at the end of a sequence, and hence takes O(1) time Thus, the partition step of quick-sort takes O(n) time Merge Sort 29

30 Partitioning arrays Perform the partition using two indices to split S into L and E+G. j k Repeat until j and k cross: Scan j to the right until finding an element > x. Scan k to the left until finding an element < x. Swap elements at indices j and k (pivot = 6) j k Merge Sort 30

31 Implementation public static void recquicksort(int[] arr, int left, int right) { if (right - left <= 0) return; else { int border = partition(arr, left, right); recquicksort(arr, left, border-1); recquicksort(arr, border+1, right); } } Merge Sort 31

32 Quick-Sort Tree An execution of quick-sort is depicted by a binary tree Each node represents a recursive call of quick-sort and stores Unsorted sequence before the execution and its pivot Sorted sequence at the end of the execution The root is the initial call The leaves are calls on subsequences of size 0 or Merge Sort 32

33 Execution Example Pivot selection Merge Sort 33

34 Execution Example (cont.) Partition, recursive call, pivot selection Merge Sort 34

35 Execution Example (cont.) Partition, recursive call, base case Merge Sort 35

36 Execution Example (cont.) Recursive call,, base case, join Merge Sort 36

37 Execution Example (cont.) Recursive call, pivot selection Merge Sort 37

38 Execution Example (cont.) Partition,, recursive call, base case Merge Sort 38

39 Execution Example (cont.) Join, join Merge Sort 39

40 Worst-case Running Time The worst case for quick-sort occurs when the pivot is the unique minimum or maximum element One of L and G has size n 1 and the other has size 0 The running time is proportional to the sum n + (n 1) Thus, the worst-case running time of quick-sort is O(n 2 ) depth 0 1 time n n 1 n 1 1 Merge Sort 40

41 Best-case Running Time The best case for quick-sort occurs when the pivot is the median element The L and G parts are equal the sequence is split in halves, like in merge sort Thus, the best-case running time of quick-sort is O(n log n) Merge Sort 41

42 Average-case Running Time The average case for quick-sort: half of the times, the pivot is roughly in the middle Thus, the average-case running time of quick-sort is O(n log n) again Detailed proof in the textbook Merge Sort 42

43 Summary of Sorting Algorithms Algorithm selection-sort insertion-sort quick-sort merge-sort heap-sort (later in the module) Time O(n 2 ) O(n 2 ) O(n log n) expected O(n log n) O(n log n) Notes in-place (no extra memory) slow (good for small inputs) in-place (no extra memory) slow (good for small inputs) in-place (+stack), randomized fast (good for large inputs) sequential data access fast (good for huge inputs) in-place (see G & T for the in-place version) fast (good for large inputs) Merge Sort 43

44 Comparison sorting A sorting algorithm is a comparison sorting algorithm if it uses comparisons between elements in the sequence to determine in which order to place them Examples of comparison sorts: bubble sort, selection sort, insertion sort, heap sort, merge sort, quicksort. Example of not a comparison sort: bucket sort (Ch. 11.4). Runs in O(n), relies on knowing the range of values in the sequence (e.g. integers between 1 and 1000). Merge Sort 44

45 Lower bound for comparison sort We can model sorting which depends on comparisons between elements as a binary decision tree. At each node, a comparison between two elements is made; there are two possible outcomes and we find out a bit more about the correct order of items in the array. Finally arrive at full information about the correct order of the items in the array. Merge Sort 45

46 Comparison sorting a 2 < a 3? yes a 1,,a n : don t know what the correct order is. a 1 < a 2? yes no Correct order is (a 1,,a n ) (a 2, a 1,...,a n ) (total of n! possibilities) Merge Sort 46

47 How many comparisons? If a binary tree has n! leaves, than the minimal number of levels (assuming the tree is perfect) is (log 2 n!) +1. This shows that O(n log n) sorting algorithms are essentially optimal (log 2 n! is not equal to n log 2 n, but has the same growth rate modulo some hairy constants). Merge Sort 47

### The Selection Problem

The Selection Problem The selection problem: given an integer k and a list x 1,..., x n of n elements find the k-th smallest element in the list Example: the 3rd smallest element of the following list

### Sorting algorithms. Mergesort and Quicksort. Merge Sort. Partitioning Choice 1. Partitioning Choice 3. Partitioning Choice 2 2/5/2015

Sorting algorithms Mergesort and Quicksort Insertion, selection and bubble sort have quadratic worst case performance The faster comparison based algorithm? O(nlogn) Mergesort and Quicksort Merge Sort

### Writing programs to solve problem consists of a large. how to represent aspects of the problem for solution

Algorithm Efficiency & Sorting Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms 1 Overview 2 If a given ADT (i.e. stack or queue) is attractive as part of a solution How will

### Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Sorting revisited How did we use a binary search tree to sort an array of elements? Tree Sort Algorithm Given: An array of elements to sort 1. Build a binary search tree out of the elements 2. Traverse

### Algorithms Analysis Chapter 7 Quicksort. Quicksort

Algorithms Analysis Chapter 7 Quicksort 1 Quicksort Quicksort pros: Sorts in place Sorts O(n lg n) in the average case Very efficient in practice Quicksort cons: Sorts O(n 2 ) in the worst case not stable

### Converting a Number from Decimal to Binary

Converting a Number from Decimal to Binary Convert nonnegative integer in decimal format (base 10) into equivalent binary number (base 2) Rightmost bit of x Remainder of x after division by two Recursive

### CSC 143. Two important problems. Searching and Sorting. Review: Linear Search. Example. Review: Binary Search. How Efficient Is Linear Search?

CSC 43 Searching and Sorting [Chapter 9, pp. 402-432] Two important problems Search: finding something in a set of data Sorting: putting a set of data in order Both very common, very useful operations

### 6. Standard Algorithms

6. Standard Algorithms The algorithms we will examine perform Searching and Sorting. 6.1 Searching Algorithms Two algorithms will be studied. These are: 6.1.1. inear Search The inear Search The Binary

### 2 Exercises and Solutions

2 Exercises and Solutions Most of the exercises below have solutions but you should try first to solve them. Each subsection with solutions is after the corresponding subsection with exercises. 2.1 Sorting

### Quicksort is a divide-and-conquer sorting algorithm in which division is dynamically carried out (as opposed to static division in Mergesort).

Chapter 7: Quicksort Quicksort is a divide-and-conquer sorting algorithm in which division is dynamically carried out (as opposed to static division in Mergesort). The three steps of Quicksort are as follows:

### The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

The Tower of Hanoi Recursion Solution recursion recursion recursion Recursive Thinking: ignore everything but the bottom disk. 1 2 Recursive Function Time Complexity Hanoi (n, src, dest, temp): If (n >

### Searching & Sorting. Searching & Sorting 1

Searching & Sorting Searching & Sorting 1 The Plan Searching Sorting Java Context Searching & Sorting 2 Search (Retrieval) Looking it up One of most fundamental operations Without computer Indexes Tables

### Binary Search. Search for x in a sorted array A.

Divide and Conquer A general paradigm for algorithm design; inspired by emperors and colonizers. Three-step process: 1. Divide the problem into smaller problems. 2. Conquer by solving these problems. 3.

### Binary Heap Algorithms

CS Data Structures and Algorithms Lecture Slides Wednesday, April 5, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks CHAPPELLG@member.ams.org 2005 2009 Glenn G. Chappell

### Quicksort An Example. When lo and hi cross, we are done. The final value of hi is the position in which the partitioning element ends up.

Quicksort An Example We sort the array A = (38 81 22 48 13 69 93 14 45 58 79 72) with quicksort, always choosing the pivot element to be the element in position (left+right)/2. The partitioning during

### Mergesort and Quicksort

Mergesort Mergesort and Quicksort Basic plan: Divide array into two halves. Recursively sort each half. Merge two halves to make sorted whole. mergesort mergesort analysis quicksort quicksort analysis

### 6 March 2007 1. Array Implementation of Binary Trees

Heaps CSE 0 Winter 00 March 00 1 Array Implementation of Binary Trees Each node v is stored at index i defined as follows: If v is the root, i = 1 The left child of v is in position i The right child of

### The 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

### 1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Answer the following 1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++ 2) Which data structure is needed to convert infix notations to postfix notations? Stack 3) The

### Biostatistics 615/815

Merge Sort Biostatistics 615/815 Lecture 8 Notes on Problem Set 2 Union Find algorithms Dynamic Programming Results were very ypositive! You should be gradually becoming g y g comfortable compiling, debugging

### Algorithms. Margaret M. Fleck. 18 October 2010

Algorithms Margaret M. Fleck 18 October 2010 These notes cover how to analyze the running time of algorithms (sections 3.1, 3.3, 4.4, and 7.1 of Rosen). 1 Introduction The main reason for studying big-o

### Sorting algorithm. Sorting algorithms used in computer science are often classified by:

Sorting algorithm In computer science and mathematics, a sorting algorithm is an algorithm that puts elements of a list in a certain order. The most used orders are numerical order and lexicographical

### Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Binary Search Trees Lecturer: Georgy Gimel farb COMPSCI 220 Algorithms and Data Structures 1 / 27 1 Properties of Binary Search Trees 2 Basic BST operations The worst-case time complexity of BST operations

### Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology 6.046J/18.410J Professors Erik Demaine and Shafi Goldwasser Quiz 1 Quiz 1 Do not open this quiz booklet until you are directed

### Sorting Algorithms. Nelson Padua-Perez Bill Pugh. Department of Computer Science University of Maryland, College Park

Sorting Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park Overview Comparison sort Bubble sort Selection sort Tree sort Heap sort Quick sort Merge

### Parallel Algorithms - sorting

Parallel Algorithms - sorting Fernando Silva DCC-FCUP (Some slides are based on those from the book Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers, 2nd

### Balanced search trees

Lecture 8 Balanced search trees 8.1 Overview In this lecture we discuss search trees as a method for storing data in a way that supports fast insert, lookup, and delete operations. (Data structures handling

### CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

CSC148 Lecture 8 Algorithm Analysis Binary Search Sorting Algorithm Analysis Recall definition of Big Oh: We say a function f(n) is O(g(n)) if there exists positive constants c,b such that f(n)

### CSE3358 Problem Set 4 Solution

CSE3358 Problem Set 4 Solution Problem : Srtng (a) ( points) Consider an array A of n numbers each of which is either or. We will refer to such an array as binary. Describe an asymptotically efficient

### External Sorting. Chapter 13. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

External Sorting Chapter 13 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Why Sort? A classic problem in computer science! Data requested in sorted order e.g., find students in increasing

### SEARCHING AND SORTING

SEARCHING AND SORTING Searching A basic operation in computer science Problem formulation Given: Collection (array) of values A property on values Find: a value having the property in the array Examples

Divide and Conquer Paradigm Melissa Manley California State University Northridge Author s address: 18111 Nordhoff Street, Northridge, CA, 91330 Permission to make digital/hard copy of all or part of this

### Format of Divide-and-Conquer algorithms:

CS 360: Data Structures and Algorithms Divide-and-Conquer (part 1) Format of Divide-and-Conquer algorithms: Divide: Split the array or list into smaller pieces Conquer: Solve the same problem recursively

### CMPS 102 Solutions to Homework 1

CMPS 0 Solutions to Homework Lindsay Brown, lbrown@soe.ucsc.edu September 9, 005 Problem..- p. 3 For inputs of size n insertion sort runs in 8n steps, while merge sort runs in 64n lg n steps. For which

### Algorithm Analysis [2]: if-else statements, recursive algorithms. COSC 2011, Winter 2004, Section N Instructor: N. Vlajic

1 Algorithm Analysis []: if-else statements, recursive algorithms COSC 011, Winter 004, Section N Instructor: N. Vlajic Algorithm Analysis for-loop Running Time The running time of a simple loop for (int

### Binary search algorithm

Binary search algorithm Definition Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than

### Searching and Sorting Algorithms

Searching and Sorting Algorithms CS117, Fall 2004 Supplementary Lecture Notes Written by Amy Csizmar Dalal 1 Introduction How do you find someone s phone number in the phone book? How do you find your

### Decrease-and-Conquer Algorithms

Decrease-and-Conquer Algorithms Joaquim Madeira Version 0.1 October 2015 U. Aveiro, October 2015 1 Overview Decrease-and-Conquer Linear Search Binary Search Ternary Search Interpolation Search The Selection

### SORTING. Review of Sorting. Merge Sort. Sets. sorting

Review of Sorting Merge Sort Sets SORTING 1 Sorting Algorithms Selection Sort uses a priority queue P implemented with an unsorted sequence: - Phase 1: the insertion of an item into P takes O(1) time;

### Data Structures Trees Sorting Algorithms

Data Structures Trees Sorting Algorithms Informatik I für D-MAVT (FS 2013) Christian Schumacher, chschuma@inf.ethz.ch Trees Binary Trees Binary Search Trees Sorting Algorithms Linked Lists & Trees A linked

### Computer Algorithms. Merge Sort. Analysis of Merge Sort. Recurrences. Recurrence Relations. Iteration Method. Lecture 6

Computer Algorithms Lecture 6 Merge Sort Sorting Problem: Sort a sequence of n elements into non-decreasing order. Divide: Divide the n-element sequence to be sorted into two subsequences of n/ elements

### Analysis of Algorithms - Recurrences (cont.) -

Analysis of Algoritms - Recurrences (cont.) - Andreas Ermedal MRTC (Mälardalens Real Time Researc Center) andreas.ermedal@md.se Autumn 004 Recurrences: Reersal Recurrences appear frequently in running

### Symbol Tables. IE 496 Lecture 13

Symbol Tables IE 496 Lecture 13 Reading for This Lecture Horowitz and Sahni, Chapter 2 Symbol Tables and Dictionaries A symbol table is a data structure for storing a list of items, each with a key and

### External Sorting. Why Sort? 2-Way Sort: Requires 3 Buffers. Chapter 13

External Sorting Chapter 13 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Why Sort? A classic problem in computer science! Data requested in sorted order e.g., find students in increasing

### Announcements. CSE332: Data Abstractions. Lecture 9: B Trees. Today. Our goal. M-ary Search Tree. M-ary Search Tree. Ruth Anderson Winter 2011

Announcements CSE2: Data Abstractions Project 2 posted! Partner selection due by 11pm Tues 1/25 at the latest. Homework due Friday Jan 28 st at the BEGINNING of lecture Lecture 9: B Trees Ruth Anderson

### Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Binary Heaps A binary heap is another data structure. It implements a priority queue. Priority Queue has the following operations: isempty add (with priority) remove (highest priority) peek (at highest

### Sorting Algorithms. Ananth Grama, Anshul Gupta, George Karypis, and Vipin Kumar

Sorting Algorithms Ananth Grama, Anshul Gupta, George Karypis, and Vipin Kumar To accompany the text Introduction to Parallel Computing, Addison Wesley, 2003. Topic Overview Issues in Sorting on Parallel

### Algorithms and Data Structures

Priority Queues and Heaps Page 1 - Priority Queues and Heaps Dr. Fall 2008 Priority Queues and Heaps Page 2 Outline Priority Queues Heaps Heap-Based Priority Queues Bottom-Up Heap Construction Priority

### Analysis of Binary Search algorithm and Selection Sort algorithm

Analysis of Binary Search algorithm and Selection Sort algorithm In this section we shall take up two representative problems in computer science, work out the algorithms based on the best strategy to

### For example, we have seen that a list may be searched more efficiently if it is sorted.

Sorting 1 Many computer applications involve sorting the items in a list into some specified order. For example, we have seen that a list may be searched more efficiently if it is sorted. To sort a group

### 8.2 Quicksort. 332 Chapter 8. Sorting

332 Chapter 8. Sorting For randomly ordered data, the operations count goes approximately as N 1.25,at least for N50, however, Quicksort is generally faster. The program follows: void shell(unsigned

### CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) 3 4 4 7 5 9 6 16 7 8 8 4 9 8 10 4 Total 92.

Name: Email ID: CSE 326, Data Structures Section: Sample Final Exam Instructions: The exam is closed book, closed notes. Unless otherwise stated, N denotes the number of elements in the data structure

### Divide-and-Conquer Algorithms Part Four

Divide-and-Conquer Algorithms Part Four Announcements Problem Set 2 due right now. Can submit by Monday at 2:15PM using one late period. Problem Set 3 out, due July 22. Play around with divide-and-conquer

### CS 4310 HOMEWORK SET 1

CS 4310 HOMEWORK SET 1 PAUL MILLER Section 2.1 Exercises Exercise 2.1-1. Using Figure 2.2 as a model, illustrate the operation of INSERTION-SORT on the array A = 31, 41, 59, 26, 41, 58. Solution: Assume

### In the example above, it is as though the elements are in two-dimensions such a table

2 1 3 2 4 4 5 5 6 6 7 8 9 10 11 12 13 Multiple-Dimensional Arrays Elements of arrays may have more than one index, i.e. In example above, it is as though elements are in two-dimensions such a table with

### CSE 5311 Homework 2 Solution

CSE 5311 Homework 2 Solution Problem 6.2-6 Show that the worst-case running time of MAX-HEAPIFY on a heap of size n is Ω(lg n). (Hint: For a heap with n nodes, give node values that cause MAX- HEAPIFY

### CS2223 Algorithms B Term 2013 Exam 2 Solutions

CS2223 Algorithms B Term 2013 Exam 2 Solutions Dec. 3, 2013 By Prof. Carolina Ruiz Dept. of Computer Science WPI PROBLEM 1: Solving Recurrences (15 points) Solve the recurrence T (n) = 2T (n/3) + n using

### Algorithms and recursion

Read: Chapter 13 from textbook Algorithms and recursion What programs implement is recipes for solving problems. These recipes are called algorithms. We have already seen (in the practice problems from

### THIS CHAPTER studies several important methods for sorting lists, both contiguous

Sorting 8 THIS CHAPTER studies several important methods for sorting lists, both contiguous lists and linked lists. At the same time, we shall develop further tools that help with the analysis of algorithms

### Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally

Recurrence Relations Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally modeled by recurrence relations. A recurrence relation is an equation which

### O(n) linear time This means that the algorithm requires a number of steps proportional to the size of the task.

Time complexity : Big O notation f(n) = O(g(n)) means There are positive constants c and k such that: 0

### AP Computer Science AB Syllabus 1

AP Computer Science AB Syllabus 1 Course Resources Java Software Solutions for AP Computer Science, J. Lewis, W. Loftus, and C. Cocking, First Edition, 2004, Prentice Hall. Video: Sorting Out Sorting,

### Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Questions 1 through 25 are worth 2 points each. Choose one best answer for each. 1. For the singly linked list implementation of the queue, where are the enqueues and dequeues performed? c a. Enqueue in

### Multi-Way Search Trees (B Trees)

Multi-Way Search Trees (B Trees) Multiway Search Trees An m-way search tree is a tree in which, for some integer m called the order of the tree, each node has at most m children. If n

### Intro. to the Divide-and-Conquer Strategy via Merge Sort CMPSC 465 CLRS Sections 2.3, Intro. to and various parts of Chapter 4

Intro. to the Divide-and-Conquer Strategy via Merge Sort CMPSC 465 CLRS Sections 2.3, Intro. to and various parts of Chapter 4 I. Algorithm Design and Divide-and-Conquer There are various strategies we

### Data Structures and Algorithms Week 3

Data Structures and Algorithms Week 3 1. Divide and conquer 2. Merge sort, repeated substitutions 3. Tiling 4. Recurrences Recurrences Running times of algorithms with recursive calls can be described

### Merge Sort and Recurrences. COSC 581, Algorithms January 14, 2014

Merge Sort and Recurrences COSC 581, Algorithms January 14, 2014 Reading Assignments Today s class: Chapter 2, 4.0, 4.4 Reading assignment for next class: Chapter 4.2, 4.5 3 Common Algorithmic Techniques

### Recursion, Search, Selection. Cheng, Wei COMP June 12, Title

Recursion, Search, Selection Cheng, Wei COMP110-001 June 12, 2014 Title Today Introduction to Recursion Introduction to Search & Selection Not a major focus of the final exam But you should be able to

### Trees. Tree Definitions: Let T = (V, E, r) be a tree. The size of a tree denotes the number of nodes of the tree.

Trees After lists (including stacks and queues) and hash tables, trees represent one of the most widely used data structures. On one hand trees can be used to represent objects that are recursive in nature

### 4.4 The recursion-tree method

4.4 The recursion-tree method Let us see how a recursion tree would provide a good guess for the recurrence = 3 4 ) Start by nding an upper bound Floors and ceilings usually do not matter when solving

### Memory Management. Memory Areas and their use. Memory Manager Tasks: Free List Implementations. acquire release

Memory Management Memory Areas and their use Memory Manager Tasks: acquire release Free List Implementations Singly Linked List Doubly Linked List Buddy Systems Memory Management Memory areas: In languages

### Priority Queues. Client, Implementation, Interface. Priority Queues. Abstract Data Types

Client, Implementation, Interface Priority Queues Priority Queue ADT Binary heaps Heapsort Reference: Chapter 6, Algorithms in Java, 3 rd Edition, Robert Sedgewick. Separate interface and implementation

### Data Structures and Data Manipulation

Data Structures and Data Manipulation What the Specification Says: Explain how static data structures may be used to implement dynamic data structures; Describe algorithms for the insertion, retrieval

### ! Insert a value with specified key. ! Search for value given key. ! Delete value with given key. ! O(log N) time per op.

Symbol Table Review 4.4 Balanced Trees Symbol table: key-value pair abstraction.! a value with specified key.! for value given key.! value with given key. Randomized BST.! O(log ) time per op. unless you

### Chapter Objectives. Chapter 9. Sequential Search. Search Algorithms. Search Algorithms. Binary Search

Chapter Objectives Chapter 9 Search Algorithms Data Structures Using C++ 1 Learn the various search algorithms Explore how to implement the sequential and binary search algorithms Discover how the sequential

### Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Binary Search Trees Data in each node Larger than the data in its left child Smaller than the data in its right child FIGURE 11-6 Arbitrary binary tree FIGURE 11-7 Binary search tree Data Structures Using

### CS 102: SOLUTIONS TO DIVIDE AND CONQUER ALGORITHMS (ASSGN 4)

CS 10: SOLUTIONS TO DIVIDE AND CONQUER ALGORITHMS (ASSGN 4) Problem 1. a. Consider the modified binary search algorithm so that it splits the input not into two sets of almost-equal sizes, but into three

### Pseudo code Tutorial and Exercises Teacher s Version

Pseudo code Tutorial and Exercises Teacher s Version Pseudo-code is an informal way to express the design of a computer program or an algorithm in 1.45. The aim is to get the idea quickly and also easy

### Algorithms. Theresa Migler-VonDollen CMPS 5P

Algorithms Theresa Migler-VonDollen CMPS 5P 1 / 32 Algorithms Write a Python function that accepts a list of numbers and a number, x. If x is in the list, the function returns the position in the list

### Week 7: Divide and Conquer

Agenda: Divide and Conquer technique Multiplication of large integers Exponentiation Matrix multiplication 1 2- Divide and Conquer : To solve a problem we can break it into smaller subproblems, solve each

### Problem Set #1 Solutions

CS161: Design and Analysis of Algorithms Summer 2004 Problem Set #1 Solutions General Notes Regrade Policy: If you believe an error has been made in the grading of your problem set, you may resubmit it

### Steven Skiena. skiena

Lecture 3: Recurrence Relations (1997) Steven Skiena Department of Computer Science State University of New York Stony Brook, NY 11794 4400 http://www.cs.sunysb.edu/ skiena Argue the solution to T(n) =

### Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Binary Search Trees A Generic Tree Nodes in a binary search tree ( B-S-T) are of the form P parent Key A Satellite data L R B C D E F G H I J The B-S-T has a root node which is the only node whose parent

### Balanced Trees Part One

Balanced Trees Part One Balanced Trees Balanced trees are surprisingly versatile data structures. Many programming languages ship with a balanced tree library. C++: std::map / std::set Java: TreeMap /

### Data Structures, Sample Test Questions for the Material after Test 2, with Answers

Data Structures, Sample Test Questions for the Material after Test 2, with Answers 1. Recall the public interfaces of classes List and ListIterator: typedef int ListItemType; class ListIterator{ public:

### UNIT 5C Merge Sort. Course Announcements

UNIT 5C Merge Sort 1 Course Announcements Exam rooms for Lecture 1, 2:30 3:20 Sections A, B, C, D at Rashid Sections E, F, G at Baker A51 (Giant Eagle Auditorium) Exam rooms for Lecture 2, 3:30 4:20 Sections

### Linear Time Selection

Chapter 2 Linear Time Selection Definition 1 Given a sequence x 1, x 2,..., x n of numbers, the ith order statistic is the ith smallest element. Maximum or Minimum When i = 1, it is the minimum element

### 1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D. base address 2. The memory address of fifth element of an array can be calculated

### Divide and Conquer My T. UF

Introduction to Algorithms Divide and Conquer @ UF General Approach Divide the problem into a number of subproblems that are smaller instances of the same problem Conquer the subproblems by solving them

### Exam study sheet for CS2711. List of topics

Exam study sheet for CS2711 Here is the list of topics you need to know for the final exam. For each data structure listed below, make sure you can do the following: 1. Give an example of this data structure

### Searching and Sorting. Searching. Binary Search. Linear Search. k N-1. <= x??? > x. x not in here???

Searching and Sorting It s a fundamental area of CS since the beginning of time. Well understood! ll professional programmers should have a good understanding of it. lso provides good example for discussion

### Divide-and-Conquer Algorithms Part One

Divide-and-Conquer Algorithms Part One Announcements Problem Set One completely due right now. Solutions distributed at the end of lecture. Programming section today in Gates B08 from from 3:45PM 5:00PM.

### CS473 - 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

### Algorithm Analysis Sorting II. Fall 2013 Carola Wenk

Algorithm Analysis Sorting II Fall 2013 Carola Wenk Is Selection Sort Practical? What is the running time of selection sort for lists that have thousands of items? The theoretical performance is not particularly

### Solving Recurrences. Lecture 13 CS2110 Summer 2009

Solving Recurrences Lecture 13 CS2110 Summer 2009 In the past Complexity: Big-O definition Non-recursive programs 2 Today Complexity of recursive programs Introduce recurrences Methods to solve recurrences

### Cpt S 223. School of EECS, WSU

Priority Queues (Heaps) 1 Motivation Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important or timely than others (higher priority)

### Algorithms, Integers

CHAPTER 3 Algorithms, Integers 3.1. Algorithms Consider the following list of instructions to find the maximum of three numbers a, b, c: 1. Assign variable x the value of a. 2. If b > x then assign x the

### Searching and Sorting

Linear Search Searching and Sorting Binary Search Selection Sort Insertion Sort Bubble (or Exchange) Sort Exercises Unit 27 1 Linear Search Searching is the process of determining whether or not a given