Unit-2 Divide and conquer 2016

Size: px
Start display at page:

Download "Unit-2 Divide and conquer 2016"

Transcription

1 2 Divide and conquer Overview, Structure of divide-and-conquer algorithms, binary search, quick sort, Strassen multiplication. 13% 05 Divide-and- conquer The Divide and Conquer Paradigm, is a method of designing algorithms, a general approach to construct an efficient solution to a problem. The Divide and Conquer Paradigm is an algorithm design paradigm which uses this simple process: It Divides the problem into smaller sub-parts until these sub-parts become simple enough to be solved, and then the sub parts are solved recursively, and then the solutions to these sub-parts can be combined to give a solution to the original problem. In other words, this paradigm follows three basic steps; Step 1: Break down the problem into smaller sub-problems, Step 2: Solve these separate sub-problems, and Step 3: Combine the solutions of the sub-problems. Advantages: Solving difficult problems : Divide and conquer is a powerful tool for solving conceptually difficult problems: all it requires is a way of breaking the problem into sub-problems, of solving the trivial cases and of combining sub-problems to the original problem. Similarly, decrease and conquer only requires reducing the problem to a single smaller problem, such as the classic Tower of Hanoi puzzle, which reduces moving a tower of height n to moving a tower of height n 1. Algorithm efficiency : The divide-and-conquer paradigm often helps in the discovery of efficient algorithms. It was the key, for example, to Karatsuba's fast multiplication method, the quicksort and mergesort algorithms, the Strassen algorithm for matrix multiplication, and fast Fourier transforms. Parallelism : Divide and conquer algorithms are naturally adapted for execution in multiprocessor machines, especially shared-memory systems where the communication of data between processors does not need to be planned in advance, because distinct sub-problems can be executed on different processors. D & C mainly have three parts Divide : divide the problem into smaller sub problems. Conquer : the sub problem solving them recursively. If they are small enough solve the sub problem as base case. Combine : merge the solution of sub problem to find solution of actual problem. Analysis and Design of Algorithm Page 1

2 If we expand out two more recursive steps, it looks like this: Because divide-and-conquer creates at least two subproblems, a divide-and-conquer algorithm makes multiple recursive calls. Analysis and Design of Algorithm Page 2

3 Applications: Merge Sort Quick Sort Binary Search Strassen's Matrix Multiplication Closest pair (points) Binary Search: Binary search is a fast search algorithm with run-time complexity of Ο(log n). This search algorithm works on the principle of divide and conquer. For this algorithm to work properly the data collection should be in sorted form. Binary search search a particular item by comparing the middle most item of the collection. If match occurs then index of item is returned. If middle item is greater than item then item is searched in sub-array to the right of the middle item other wise item is search in sub-array to the left of the middle item. This process continues on sub-array as well until the size of subarray reduces to zero. How binary search works? For a binary search to work, it is mandatory for the target array to be sorted. Here is the process of binary search with an pictorial example. The below given is our sorted array and assume that we need to search location of value 31 using binary search. First, we shall determine the half of the array by using this formula mid = low + (high - low) / 2 Here it is, 0 + (9-0 ) / 2 = 4 (integer value of 4.5). So 4 is the mid of array. Now we compare the value stored at location 4, with the value being searched i.e. 31. We find that value at location 4 is 27, which is not a match. Because value is greater than 27 and Analysis and Design of Algorithm Page 3

4 we have a sorted array so we also know that target value must be in upper portion of the array. We change our low to mid + 1 and find the new mid value again. low = mid + 1 mid = low + (high - low) / 2 Our new mid is 7 now. We compare the value stored at location 7 with our target value 31. The value stored at location 7 is not a match, rather it is less that what we are looking for. So the value must be in lower part from this location. So we calculate the mid again. This time it is 5. We compare the value stored ad location 5 with our target value. We find that it is a match. We conclude that the target value 31 is stored at location 5. Binary search halves the searchable items and thus reduces the count of comparisons to be made to very less numbers. Pseudo code The pseudo code of binary search algorithm should look like this Analysis and Design of Algorithm Page 4

5 Procedure binary_search A sorted array n size of array x value ot be searched Set lowerbound = 1 Set upperbound = n while x not found if upperbound < lowerbound EXIT: x does not exists. set midpoint = lowerbound + ( upperbound - lowerbound ) / 2 if A[midPoint] < x set lowerbound = midpoint + 1 if A[midPoint] > x set upperbound = midpoint - 1 if A[midPoint] = x EXIT: x found at location midpoint end while end procedure Time Complexity is Ο(log n). Analysis and Design of Algorithm Page 5

6 Quick Sort: Quick sort is a highly efficient sorting algorithm and is based on partitioning of array of data into smaller arrays. A large array is partitioned into two arrays one of which holds values smaller than specified value say pivot based on which the partition is made and another array holds values greater than pivot value. The quick sort partitions an array and then calls itself recursively twice to sort the resulting two subarray. This algorithm is quite efficient for large sized data sets as its average and worst case complexity are of O(nlogn) where n are no. of items. The three steps of Quicksort are as follows: Divide: Rearrange the elements and split the array into two subarrays and an element in between such that so that each element in the left subarray is less than or equal the middle element and each element in the right subarray is greater than the middle element. Conquer: Recursively sort the two subarrays. Combine: None 1. Features o Divide-and-conquer recursive algorithm o One of the fastest sorting algorithms o Average running time O(NlogN) o Worst-case running time O(N 2 ) 2. Basic idea 1. Pick one element in the array, which will be the pivot. 2. Make one pass through the array, called a partition step, re-arranging the entries so that: the pivot is in its proper place. entries smaller than the pivot are to the left of the pivot. entries larger than the pivot are to its right. 3. Recursively apply quicksort to the part of the array that is to the left of the pivot, and to the right part of the array. Here we don't have the merge step, at the end all the elements are in the proper order. Algorithm STEP 1. Choosing the pivot Analysis and Design of Algorithm Page 6

7 Choosing the pivot is an essential step. Depending on the pivot the algorithm may run very fast, or in quadric time.: Example: 1. Some fixed element: e.g. the first, the last, the one in the middle This is a bad choice - the pivot may turn to be the smallest or the largest element, then one of the partitions will be empty. 2. Randomly chosen (by random generator ) - still a bad choice. 3. The median of the array (if the array has N numbers, the median is the [N/2] largest number. This is difficult to compute - increases the complexity. 4. The median-of-three choice: take the first, the last and the middle element. Choose the median of these three elements. 8, 3, 25, 6, 10, 17, 1, 2, 18, 5 The first element is 8, the middle - 10, the last - 5. The median of [8, 10, 5] is 8 STEP 2. Partitioning Partitioning is illustrated on the above example. 1. The first action is to get the pivot out of the way - swap it with the last element 5, 3, 25, 6, 10, 17, 1, 2, 18, 8 2. We want larger elements to go to the right and smaller elements to go to the left. Two "fingers" are used to scan the elements from left to right and from right to left: [5, 3, 25, 6, 10, 17, 1, 2, 18, 8] ^ ^ i j o While i is to the left of j, we move i right, skipping all the elements less than the pivot. If an element is found greater then the pivot, i stops Analysis and Design of Algorithm Page 7

8 o o o While j is to the right of i, we move j left, skipping all the elements greater than the pivot. If an element is found less then the pivot, j stops When both i and j have stopped, the elements are swapped. When i and j have crossed, no swap is performed, scanning stops, and the element pointed to by i is swapped with the pivot. In the example the first swapping will be between 25 and 2, the second between 10 and Restore the pivot. After restoring the pivot we obtain the following partitioning into three groups: [5, 3, 2, 6, 1] [ 8 ] [10, 25, 18, 17] STEP 3. Recursively quicksort the left and the right parts Partition in Quicksort The pivot value divides the list in to two parts. And recursively we find pivot for each sublists until all lists contains only one element. QuickSort Pivot Algorithm Based on our understanding of partitioning in quicksort, we should now try to write an algorithm for it here. Step 1 Choose the highest index value as pivot Step 2 Take two variables to point left and right of the list excluding pivot Step 3 left points to the low index Step 4 right points to the high Step 5 while value at left is less than pivot move right Step 6 while value at right is greater than pivot move left Step 7 if both step 5 and step 6 does not match swap left and right Step 8 if left right, the point where they met is new pivot QuickSort Pivot Pseudocode The pseudocode for the above algorithm can be derived as function partitionfunc(left, right, pivot) leftpointer = left -1 Analysis and Design of Algorithm Page 8

9 rightpointer = right while True do while A[++leftPointer] < pivot do //do-nothing end while while rightpointer > 0 && A[--rightPointer] > pivot do //do-nothing end while if leftpointer >= rightpointer break else swap leftpointer,rightpointer end if end while swap leftpointer,right return leftpointer end function QuickSort Algorithm Using pivot algorithm recursively we end-up with smaller possible partitions. Each partition then processed for quick sort. We define recursive algorithm for quicksort as below Step 1 Make the right-most index value pivot Step 2 partition the array using pivot value Step 3 quicksort left partition recursively Step 4 quicksort right partition recursively QuickSort Pseudocode To get more into it, let see the pseudocode for quick sort algorithm procedure quicksort(left, right) Analysis and Design of Algorithm Page 9

10 if right-left <= 0 return else pivot = A[right] partition = partitionfunc(left, right, pivot) quicksort(left,partition-1) quicksort(partition+1,right) end if end procedure Example: Analysis and Design of Algorithm Page 10

11 Analysis and Design of Algorithm Page 11

12 Analysis and Design of Algorithm Page 12

13 Analysis and Design of Algorithm Page 13

14 Analysis and Design of Algorithm Page 14

15 Analysis and Design of Algorithm Page 15

16 Analysis and Design of Algorithm Page 16

17 Analysis and Design of Algorithm Page 17

18 Analysis and Design of Algorithm Page 18

19 Strassen multiplication: Naive Method Following is a simple way to multiply two matrices. void multiply(int A[][N], int B[][N], int C[][N]) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { C[i][j] = 0; for (int k = 0; k < N; k++) { C[i][j] += A[i][k]*B[k][j]; } } } } Time Complexity of above method is O(N 3 ). Let A, B be two square matrices. Calculate the matrix product C as Analysis and Design of Algorithm Page 19

20 If the matrices A, B are not of type 2 n 2 n we fill the missing rows and columns with zeros. We partition A, B and C into equally sized block matrices with then C i, j N k 1 a i, k b ThusT ( N) k, j N N N i1 j1 k 1 c cn 3 O( N 3 ) Strassen showed that 2x2 matrix multiplication can be accomplished in 7 multiplication and 18 additions or subtractions..(2 log 2 7 = ) This reduction can be done by Divide and Conquer Approach. A B = R Divide matrices into sub-matrices: A 0, A 1, A 2 etc Use blocked matrix multiply equations Recursively multiply sub-matrices Analysis and Design of Algorithm Page 20

21 Strassan Algorithm: C 11 = P 1 + P 4 - P 5 + P 7 = (A 11 + A 22 )(B 11 +B 22 ) + A 22 * (B 21 - B 11 ) - (A 11 + A 12 ) * B 22 + (A 12 - A 22 ) * (B 21 + B 22 ) = A 11 B 11 + A 11 B 22 + A 22 B 11 + A 22 B 22 + A 22 B 21 A 22 B 11 - A 11 B 22 -A 12 B 22 + A 12 B 21 + A 12 B 22 A 22 B 21 A 22 B 22 = A 11 B 11 + A 12 B 21 Complexity: Where N is the size of matrix. Analysis and Design of Algorithm Page 21

Divide and Conquer. Algorithm Fall Semester

Divide and Conquer. Algorithm Fall Semester Divide and Conquer Algorithm 2014 Fall Semester 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

More information

Chapter 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. 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 information

Classic Data Structures Introduction UNIT I

Classic 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 information

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

Divide-and-Conquer. The most-well known algorithm design strategy: smaller instances. combining these solutions 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 solution to original

More information

CSC Design and Analysis of Algorithms

CSC 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 information

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

CSC 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 information

Faster Sorting Methods

Faster 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 information

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

Quicksort. 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 information

Divide-and-Conquer. Dr. Yingwu Zhu

Divide-and-Conquer. Dr. Yingwu Zhu Divide-and-Conquer Dr. Yingwu Zhu Divide-and-Conquer The most-well known algorithm design technique: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances independently

More information

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1

DIVIDE & 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 information

CS 171: Introduction to Computer Science II. Quicksort

CS 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 information

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

17/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 information

Lecture 8: Mergesort / Quicksort Steven Skiena

Lecture 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 information

COMP Data Structures

COMP 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 information

Better sorting algorithms (Weiss chapter )

Better 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 information

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

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 S O R T I N G Sorting is interpreted as arranging data in some particular order. In this handout we discuss different sorting techniques for a list of elements implemented as an array. In all algorithms

More information

Sorting Algorithms. + Analysis of the Sorting Algorithms

Sorting 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 information

BM267 - Introduction to Data Structures

BM267 - Introduction to Data Structures BM267 - Introduction to Data Structures 7. Quicksort Ankara University Computer Engineering Department Bulent Tugrul Bm 267 1 Quicksort Quicksort uses a divide-and-conquer strategy A recursive approach

More information

QuickSort

QuickSort QuickSort 7 4 9 6 2 2 4 6 7 9 4 2 2 4 7 9 7 9 2 2 9 9 1 QuickSort QuickSort on an input sequence S with n elements consists of three steps: n n n Divide: partition S into two sequences S 1 and S 2 of about

More information

We can use a max-heap to sort data.

We 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 information

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.

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. 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 information

Sorting and Searching

Sorting and Searching Sorting and Searching Sorting o Simple: Selection Sort and Insertion Sort o Efficient: Quick Sort and Merge Sort Searching o Linear o Binary Reading for this lecture: http://introcs.cs.princeton.edu/python/42sort/

More information

ITEC2620 Introduction to Data Structures

ITEC2620 Introduction to Data Structures ITEC2620 Introduction to Data Structures Lecture 5a Recursive Sorting Algorithms Overview Previous sorting algorithms were O(n 2 ) on average For 1 million records, that s 1 trillion operations slow! What

More information

Cpt S 122 Data Structures. Sorting

Cpt 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 information

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

Sorting. 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 information

Sorting. Order in the court! sorting 1

Sorting. Order in the court! sorting 1 Sorting Order in the court! sorting 1 Importance of sorting Sorting a list of values is a fundamental task of computers - this task is one of the primary reasons why people use computers in the first place

More information

Quick Sort. Mithila Bongu. December 13, 2013

Quick Sort. Mithila Bongu. December 13, 2013 Quick Sort Mithila Bongu December 13, 2013 Abstract Quicksort is a sorting algorithm and also known as a Partion-Exchange Sort in which partition is carried out dynamically. Quicksort is a comparision

More information

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

Sorting. 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

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

Sorting. CSE 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation. CSE143 Au CSE 43 Java Sorting Reading: Ch. 3 & Sec. 7.3 Sorting Binary search is a huge speedup over sequential search But requires the list be sorted Slight Problem: How do we get a sorted list? Maintain the list

More information

SORTING, SETS, AND SELECTION

SORTING, SETS, AND SELECTION CHAPTER 11 SORTING, SETS, AND SELECTION ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM

More information

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

SAMPLE 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 information

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

Sorting. CSC 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation CSC Picture CSC 43 Java Sorting Reading: Sec. 9.3 Sorting Binary search is a huge speedup over sequential search But requires the list be sorted Slight Problem: How do we get a sorted list? Maintain the list in sorted

More information

Lecture #2. 1 Overview. 2 Worst-Case Analysis vs. Average Case Analysis. 3 Divide-and-Conquer Design Paradigm. 4 Quicksort. 4.

Lecture #2. 1 Overview. 2 Worst-Case Analysis vs. Average Case Analysis. 3 Divide-and-Conquer Design Paradigm. 4 Quicksort. 4. COMPSCI 330: Design and Analysis of Algorithms 8/28/2014 Lecturer: Debmalya Panigrahi Lecture #2 Scribe: Yilun Zhou 1 Overview This lecture presents two sorting algorithms, quicksort and mergesort, that

More information

Sorting. Order in the court! sorting 1

Sorting. Order in the court! sorting 1 Sorting Order in the court! sorting 1 Importance of sorting Sorting a list of values is a fundamental task of computers - this task is one of the primary reasons why people use computers in the first place

More information

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

Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang Acknowledgement The set of slides have use materials from the following resources Slides for textbook by Dr. Y.

More information

Divide and Conquer 4-0

Divide 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 information

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

Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Acknowledgement. Outline Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang Acknowledgement The set of slides have use materials from the following resources Slides for textbook by Dr. Y.

More information

Arrays, Vectors Searching, Sorting

Arrays, Vectors Searching, Sorting Arrays, Vectors Searching, Sorting Arrays char s[200]; //array of 200 characters different type than class string can be accessed as s[0], s[1],..., s[199] s[0]= H ; s[1]= e ; s[2]= l ; s[3]= l ; s[4]=

More information

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

Mergesort 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 information

DATA STRUCTURES AND ALGORITHMS

DATA STRUCTURES AND ALGORITHMS DATA STRUCTURES AND ALGORITHMS Fast sorting algorithms Shellsort, Mergesort, Quicksort Summary of the previous lecture Why sorting is needed? Examples from everyday life What are the basic operations in

More information

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

Key question: how do we pick a good pivot (and what makes a good pivot in the first place)? More on sorting Mergesort (v2) Quicksort Mergesort in place in action 53 2 44 85 11 67 7 39 14 53 87 11 50 67 2 14 44 53 80 85 87 14 87 80 50 29 72 95 2 44 80 85 7 29 39 72 95 Boxes with same color are

More information

Lecture 19 Sorting Goodrich, Tamassia

Lecture 19 Sorting Goodrich, Tamassia Lecture 19 Sorting 7 2 9 4 2 4 7 9 7 2 2 7 9 4 4 9 7 7 2 2 9 9 4 4 2004 Goodrich, Tamassia Outline Review 3 simple sorting algorithms: 1. selection Sort (in previous course) 2. insertion Sort (in previous

More information

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

Sorting. Lecture10: Sorting II. Sorting Algorithms. Performance of Sorting Algorithms Sorting (2013F) Lecture10: Sorting II Bohyung Han CSE, POSTECH bhhan@postech.ac.kr Important operation when organizing data Ordering of elements Finding duplicate elements Ranking elements (i.e., n th

More information

4. Sorting and Order-Statistics

4. 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 information

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

Bubble sort starts with very first two elements, comparing them to check which one is greater. Bubble Sorting: Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based algorithm in which each pair of adjacent elements is compared and the elements are swapped if they

More information

Sorting and Selection

Sorting and Selection Sorting and Selection Introduction Divide and Conquer Merge-Sort Quick-Sort Radix-Sort Bucket-Sort 10-1 Introduction Assuming we have a sequence S storing a list of keyelement entries. The key of the element

More information

Lecture Notes on Quicksort

Lecture Notes on Quicksort Lecture Notes on Quicksort 15-122: Principles of Imperative Computation Frank Pfenning Lecture 8 February 5, 2015 1 Introduction In this lecture we consider two related algorithms for sorting that achieve

More information

Lecture Notes on Quicksort

Lecture Notes on Quicksort Lecture Notes on Quicksort 15-122: Principles of Imperative Computation Frank Pfenning Lecture 8 September 20, 2012 1 Introduction In this lecture we first sketch two related algorithms for sorting that

More information

Sorting Goodrich, Tamassia Sorting 1

Sorting Goodrich, Tamassia Sorting 1 Sorting Put array A of n numbers in increasing order. A core algorithm with many applications. Simple algorithms are O(n 2 ). Optimal algorithms are O(n log n). We will see O(n) for restricted input in

More information

Back to Sorting More efficient sorting algorithms

Back to Sorting More efficient sorting algorithms Back to Sorting More efficient sorting algorithms Merge Sort Strategy break problem into smaller subproblems recursively solve subproblems combine solutions to answer Called divide-and-conquer we used

More information

Recursive Algorithms II

Recursive Algorithms II Recursive Algorithms II Margaret M. Fleck 23 October 2009 This lecture wraps up our discussion of algorithm analysis (section 4.4 and 7.1 of Rosen). 1 Recap Last lecture, we started talking about mergesort,

More information

Component 02. Algorithms and programming. Sorting Algorithms and Searching Algorithms. Matthew Robinson

Component 02. Algorithms and programming. Sorting Algorithms and Searching Algorithms. Matthew Robinson Component 02 Algorithms and programming Sorting Algorithms and Searching Algorithms 1 BUBBLE SORT Bubble sort is a brute force and iterative sorting algorithm where each adjacent item in the array is compared.

More information

Sorting. Two types of sort internal - all done in memory external - secondary storage may be used

Sorting. Two types of sort internal - all done in memory external - secondary storage may be used Sorting Sunday, October 21, 2007 11:47 PM Two types of sort internal - all done in memory external - secondary storage may be used 13.1 Quadratic sorting methods data to be sorted has relational operators

More information

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS CHAPTER 11 SORTING ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM NANCY M. AMATO AND

More information

Overview of Sorting Algorithms

Overview of Sorting Algorithms Unit 7 Sorting s Simple Sorting algorithms Quicksort Improving Quicksort Overview of Sorting s Given a collection of items we want to arrange them in an increasing or decreasing order. You probably have

More information

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Divide and Conquer

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Divide and Conquer Computer Science 385 Analysis of Algorithms Siena College Spring 2011 Topic Notes: Divide and Conquer Divide and-conquer is a very common and very powerful algorithm design technique. The general idea:

More information

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

Data structures. More sorting. Dr. Alex Gerdes DIT961 - VT 2018 Data structures More sorting Dr. Alex Gerdes DIT961 - VT 2018 Divide and conquer Very general name for a type of recursive algorithm You have a problem to solve: - Split that problem into smaller subproblems

More information

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

Homework Assignment #3. 1 (5 pts) Demonstrate how mergesort works when sorting the following list of numbers: 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),

More information

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

IS 709/809: Computational Methods in IS Research. Algorithm Analysis (Sorting) IS 709/809: Computational Methods in IS Research Algorithm Analysis (Sorting) Nirmalya Roy Department of Information Systems University of Maryland Baltimore County www.umbc.edu Sorting Problem Given an

More information

Quicksort (Weiss chapter 8.6)

Quicksort (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 information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 12: Sorting Algorithms MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Outline 2 Last week Implementation of the three tree depth-traversal algorithms Implementation of the BinarySearchTree

More information

Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved

Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved Recursion Chapter 7 Contents What Is Recursion? Tracing a Recursive Method Recursive Methods That Return a Value Recursively Processing an Array Recursively Processing a Linked Chain The Time Efficiency

More information

Data Structure Lecture#17: Internal Sorting 2 (Chapter 7) U Kang Seoul National University

Data Structure Lecture#17: Internal Sorting 2 (Chapter 7) U Kang Seoul National University Data Structure Lecture#17: Internal Sorting 2 (Chapter 7) U Kang Seoul National University U Kang 1 In This Lecture Main ideas and analysis of Merge sort Main ideas and analysis of Quicksort U Kang 2 Merge

More information

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

CIS 121 Data Structures and Algorithms with Java Spring Code Snippets and Recurrences Monday, January 29/Tuesday, January 30 CIS 11 Data Structures and Algorithms with Java Spring 018 Code Snippets and Recurrences Monday, January 9/Tuesday, January 30 Learning Goals Practice solving recurrences and proving asymptotic bounds

More information

Randomized Algorithms: Selection

Randomized Algorithms: Selection Randomized Algorithms: Selection CSE21 Winter 2017, Day 25 (B00), Day 16 (A00) March 15, 2017 http://vlsicad.ucsd.edu/courses/cse21-w17 Selection Problem: WHAT Given list of distinct integers a 1, a 2,,

More information

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

Lecture 7 Quicksort : Principles of Imperative Computation (Spring 2018) Frank Pfenning Lecture 7 Quicksort 15-122: Principles of Imperative Computation (Spring 2018) Frank Pfenning In this lecture we consider two related algorithms for sorting that achieve a much better running time than

More information

L14 Quicksort and Performance Optimization

L14 Quicksort and Performance Optimization L14 Quicksort and Performance Optimization Alice E. Fischer Fall 2018 Alice E. Fischer L4 Quicksort... 1/12 Fall 2018 1 / 12 Outline 1 The Quicksort Strategy 2 Diagrams 3 Code Alice E. Fischer L4 Quicksort...

More information

2/26/2016. Divide and Conquer. Chapter 6. The Divide and Conquer Paradigm

2/26/2016. Divide and Conquer. Chapter 6. The Divide and Conquer Paradigm Divide and Conquer Chapter 6 Divide and Conquer A divide and conquer algorithm divides the problem instance into a number of subinstances (in most cases 2), recursively solves each subinsance separately,

More information

Divide 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 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 information

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

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University CS 112 Introduction to Computing II Wayne Snyder Department Boston University Today Recursive Sorting Methods and their Complexity: Mergesort Conclusions on sorting algorithms and complexity Next Time:

More information

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

Chapter 3:- Divide and Conquer. Compiled By:- Sanjay Patel Assistant Professor, SVBIT. Chapter 3:- Divide and Conquer Compiled By:- Assistant Professor, SVBIT. Outline Introduction Multiplying large Integers Problem Problem Solving using divide and conquer algorithm - Binary Search Sorting

More information

Chapter 2 Divid d e-an -Conquer 1

Chapter 2 Divid d e-an -Conquer 1 Chapter 2 Divide-and-Conquerid d 1 A top-down approach Patterned after the strategy employed by Napoleon Divide an instance of a problem recursively into two or more smaller instances until the solutions

More information

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

CSC 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 Conuer Algorithm Design Techniue Divide-and-Conuer The most-well known algorithm design strategy: 1. Divide a problem instance into two

More information

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

Analysis 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 information

Algorithmic Analysis and Sorting, Part Two

Algorithmic Analysis and Sorting, Part Two Algorithmic Analysis and Sorting, Part Two Friday Four Square! 4:15PM, Outside Gates An Initial Idea: Selection Sort An Initial Idea: Selection Sort 4 1 2 7 6 An Initial Idea: Selection Sort 4 1 2 7 6

More information

Divide and Conquer Sorting Algorithms and Noncomparison-based

Divide and Conquer Sorting Algorithms and Noncomparison-based Divide and Conquer Sorting Algorithms and Noncomparison-based Sorting Algorithms COMP1927 16x1 Sedgewick Chapters 7 and 8 Sedgewick Chapter 6.10, Chapter 10 DIVIDE AND CONQUER SORTING ALGORITHMS Step 1

More information

CSE373: 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 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 information

CSE 373: Data Structures and Algorithms

CSE 373: Data Structures and Algorithms CSE 373: Data Structures and Algorithms Lecture 20: More Sorting Instructor: Lilian de Greef Quarter: Summer 2017 Today: More sorting algorithms! Merge sort analysis Quicksort Bucket sort Radix sort Divide

More information

10/21/ Linear Search The linearsearch Algorithm Binary Search The binarysearch Algorithm

10/21/ Linear Search The linearsearch Algorithm Binary Search The binarysearch Algorithm 13.1 Linear Search! A linear search simply examines each item in the search pool, one at a time, until either the target is found or until the pool is exhausted! This approach does not assume the items

More information

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc. Searching & Sorting Week 11 Gaddis: 8, 19.6,19.8 CS 5301 Fall 2014 Jill Seaman 1 Definitions of Search and Sort! Search: find a given item in a list, return the position of the item, or -1 if not found.!

More information

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Merge Sort & Quick Sort

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Merge Sort & Quick Sort Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 Merge Sort & Quick Sort 1 Divide-and-Conquer Divide-and conquer is a general algorithm

More information

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

COMP2012H Spring 2014 Dekai Wu. Sorting. (more on sorting algorithms: mergesort, quicksort, heapsort) COMP2012H Spring 2014 Dekai Wu Sorting (more on sorting algorithms: mergesort, quicksort, heapsort) Merge Sort Recursive sorting strategy. Let s look at merge(.. ) first. COMP2012H (Sorting) 2 COMP2012H

More information

e-pg PATHSHALA- Computer Science Design and Analysis of Algorithms Module 10

e-pg PATHSHALA- Computer Science Design and Analysis of Algorithms Module 10 e-pg PATHSHALA- Computer Science Design and Analysis of Algorithms Module 10 Component-I (B) Description of Module Items Subject Name Computer Science Description of Module Paper Name Module Name/Title

More information

COMP 352 FALL Tutorial 10

COMP 352 FALL Tutorial 10 1 COMP 352 FALL 2016 Tutorial 10 SESSION OUTLINE Divide-and-Conquer Method Sort Algorithm Properties Quick Overview on Sorting Algorithms Merge Sort Quick Sort Bucket Sort Radix Sort Problem Solving 2

More information

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

Sorting Algorithms. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University Sorting Algorithms CptS 223 Advanced Data Structures Larry Holder School of Electrical Engineering and Computer Science Washington State University 1 QuickSort Divide-and-conquer approach to sorting Like

More information

SORTING AND SELECTION

SORTING AND SELECTION 2 < > 1 4 8 6 = 9 CHAPTER 12 SORTING AND SELECTION ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER (WILEY 2016)

More information

CPS 616 TRANSFORM-AND-CONQUER 7-1

CPS 616 TRANSFORM-AND-CONQUER 7-1 CPS 616 TRANSFORM-AND-CONQUER 7-1 TRANSFORM AND CONQUER Group of techniques to solve a problem by first transforming the problem into one of: 1. a simpler/more convenient instance of the same problem (instance

More information

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

Quicksort. The divide-and-conquer strategy is used in quicksort. Below the recursion step is described: Quicksort Quicksort is a divide and conquer algorithm. Quicksort first divides a large list into two smaller sub-lists: the low elements and the high elements. Quicksort can then recursively sort the sub-lists.

More information

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

Algorithms and Data Structures (INF1) Lecture 7/15 Hua Lu Algorithms and Data Structures (INF1) Lecture 7/15 Hua Lu Department of Computer Science Aalborg University Fall 2007 This Lecture Merge sort Quick sort Radix sort Summary We will see more complex techniques

More information

University of the Western Cape Department of Computer Science

University of the Western Cape Department of Computer Science University of the Western Cape Department of Computer Science Algorithms and Complexity CSC212 Paper II Final Examination 13 November 2015 Time: 90 Minutes. Marks: 100. UWC number Surname, first name Mark

More information

Divide and Conquer. Algorithm D-and-C(n: input size)

Divide 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 information

Quick Sort. CSE Data Structures May 15, 2002

Quick Sort. CSE Data Structures May 15, 2002 Quick Sort CSE 373 - Data Structures May 15, 2002 Readings and References Reading Section 7.7, Data Structures and Algorithm Analysis in C, Weiss Other References C LR 15-May-02 CSE 373 - Data Structures

More information

Searching and Sorting

Searching and Sorting CS 211 SEARCH & SORT SEARCHING & SORTING Searching and Sorting Searching means that we have some collection of data, and we seek a particular value that might be contained within our collection. We provide

More information

Divide and Conquer Algorithms: Advanced Sorting

Divide 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 information

Quick-Sort. Quick-Sort 1

Quick-Sort. Quick-Sort 1 Quick-Sort 7 4 9 6 2 2 4 6 7 9 4 2 2 4 7 9 7 9 2 2 9 9 Quick-Sort 1 Outline and Reading Quick-sort ( 4.3) Algorithm Partition step Quick-sort tree Execution example Analysis of quick-sort (4.3.1) In-place

More information

Algorithms in Systems Engineering ISE 172. Lecture 12. Dr. Ted Ralphs

Algorithms 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 information

CSC 273 Data Structures

CSC 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 information

SORTING. Comparison of Quadratic Sorts

SORTING. Comparison of Quadratic Sorts SORTING Chapter 8 Comparison of Quadratic Sorts 2 1 Merge Sort Section 8.7 Merge A merge is a common data processing operation performed on two ordered sequences of data. The result is a third ordered

More information

Quick-Sort. Quick-sort is a randomized sorting algorithm based on the divide-and-conquer paradigm:

Quick-Sort. Quick-sort is a randomized sorting algorithm based on the divide-and-conquer paradigm: Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Quick-Sort 7 4 9 6 2 2 4 6 7 9 4 2 2 4 7 9

More information

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

9/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 information