Lecture 6 Sorting II Divide-and-Conquer Algorithms.

Slides:



Advertisements
Similar presentations
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Advertisements

A simple example finding the maximum of a set S of n numbers.
CSC 213 – Large Scale Programming or. Today’s Goals  Begin by discussing basic approach of quick sort  Divide-and-conquer used, but how does this help?
Divide-and-Conquer The most-well known algorithm design strategy:
Divide and Conquer Strategy
Algorithm Design Paradigms
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
1 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.
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.
CS4413 Divide-and-Conquer
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Chapter 3: Divide and Conquer
Theory of Algorithms: Divide and Conquer
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
Foundations of Algorithms, Fourth Edition
Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search.
Analysis of Algorithms CS 477/677 Sorting – Part B Instructor: George Bebis (Chapter 7)
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 4 Some of the sides are exported from different sources.
Lecture 8 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Design and Analysis of Algorithms – Chapter 51 Divide and Conquer (I) Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
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.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Theory of Algorithms: Divide and Conquer James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape Town.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
HKOI 2006 Intermediate Training Searching and Sorting 1/4/2006.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
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.
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
Decision Problems Optimization problems : minimum, maximum, smallest, largest Satisfaction (SAT) problems : Traveling salesman, Clique, Vertex-Cover,
Divide and Conquer Strategy
1 Ch.19 Divide and Conquer. 2 BIRD’S-EYE VIEW Divide and conquer algorithms Decompose a problem instance into several smaller independent instances May.
Young CS 331 D&A of Algo. Topic: Divide and Conquer1 Divide-and-Conquer General idea: Divide a problem into subprograms of the same kind; solve subprograms.
 Design and Analysis of Algorithms تصميم وتحليل الخوارزميات (311 عال) Chapter 2 Sorting (insertion Sort, Merge Sort)
Sorting Quick, Merge & Radix Divide-and-conquer Technique subproblem 2 of size n/2 subproblem 1 of size n/2 a solution to subproblem 1 a solution to.
Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of.
Quicksort This is probably the most popular sorting algorithm. It was invented by the English Scientist C.A.R. Hoare It is popular because it works well.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 8b. Sorting(2): (n log n) Algorithms.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Chapter 4 Divide-and-Conquer
Lecture 4 Divide-and-Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
Divide and Conquer.
Divide-and-Conquer The most-well known algorithm design strategy:
CSCE 411 Design and Analysis of Algorithms
Chapter 4: Divide and Conquer
Unit-2 Divide and Conquer
Medians and Order Statistics
Topic: Divide and Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4.
Divide-and-Conquer The most-well known algorithm design strategy:
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Topic: Divide and Conquer
CSC 380: Design and Analysis of Algorithms
Divide and Conquer Merge sort and quick sort Binary search
Algorithm Course Algorithms Lecture 3 Sorting Algorithm-1
Presentation transcript:

Lecture 6 Sorting II Divide-and-Conquer Algorithms

Sorting Algorithms Quadratic O(n 2 ): Selection Sort, Insertion Sort, Bubble Sort. Divide-and-Conquer: Merge Sort, Quick Sort O(n log n) Using Special Data Structures: HeapSort O(n log n) Non-comparison based (Domain dependent): Radix Sort (complexity depends on domain size of numbers being sorted)

Merge Sort Pseudo code and applet, see class web page We did analysis last week. Remaining: algorithm for Merging two sorted lists in O(n) time

Merging two sorted lists using additional space Algorithm: –Compare the top elements of each list –Place the smallest in a new array –Continue until one list becomes empty Analysis: each operation (comparison) is charged to the element being moved to the new list. Total elements moved: n

Divide-and-Conquer Paradigm General technique for algorithm design Often leads to very efficient algorithms Paradigm: –Divide the problem into 2 or more smaller subproblems –Solve the subproblems recursively –Combine the solutions to the subproblems to obtain solution of original problem

Examples of Divide-and- Conquer Algorithms Binary Search Merge Sort Quick Sort Certain Convex Hull algorithms in Computational Geometry Many parallel algorithms

QuickSort Pseudocode and applet from Dominique’s page. Algorithm:Algorithm: –Choose an element of the array (the pivot). For example, the first element. –Scan the array and partition the elements into two subarrays: those smaller and those larger than the pivot, and place them in the array with the pivot between them. –Recursively sort the two subarrays.

Worst case analysis of Quick Sort Happens when one of the two subarrays is very small, the other very large: 1 and n-1 elements each. Recurrence: T(n)=T(1)+T(n-1)+n Solution to the recurrence: T(n)=O(n 2 )

Average case analysis of Quick Sort I won’t do the formal analysis, need background from probability theory (some done in Discrete) Idea: choose the pivot at random. On the average, half of the elements are larger, half are smaller than it. Hence on the average it splits the array in half, which leads to the known recurrence: T(n) = 2 T(n/2) + n Hence on average T(n)= O(n log n)

Other Sorting Algorithms next time (Heap Sort) Now: more divide-and-conquer algorithms

Selection Finding the minimum: n-1 steps Finding the second largest element: n-1+n-2=2n-3. Can you do better? Finding the third largest: 3n-6. Better? Median: n/2th element: O(n 2 ). Better? There exist O(n) time algorithms based on divide-and-conquer

Matrix Multiplication a11a12a13a14 a21a22a23a24 a31a32a33a34 a41a42a43a44 b11b12b13b14 b21b22b23b24 b31b32b33b34 b41b42b43b44 c11c12c13c14 c21c22c23c24 c31c32c33c34 c41c42c43c44 = x C ij =  n k=1 a ik b kj O(n 3 ) multiplications

Divide and Conquer I Pp textbook Divide the matrix in 2 by rows and columns (4 parts) Multiply –T(n)=8T(n/2) –T(1)=1 Solution? In-class exercise

Strassen’s Matrix Multiplication Algorithm m1=(a12-a22)(b21+b22) m2=(a11+a22)(b11+b22) m3=(a11-a21)(b11+b12) m4=(a11+a12)b22 m5=a11(b12-b22) m6=(a21+a22)b11 c11=m1+m2-m4+m6 c12= m4+m5 c21=m6+m7 c22=m2-m3+m5-m7 Time: T(n)=7T(n/2) T(1)=1 Solution? In-class exercise.

Convex Hull Algorithm Show applet pp textbook