Design and Analysis of Algorithms Quicksort Haidong Xue Summer 2012, at GSU.

Slides:



Advertisements
Similar presentations
Sorting in Linear Time Introduction to Algorithms Sorting in Linear Time CSE 680 Prof. Roger Crawfis.
Advertisements

Design and Analysis of Algorithms Introduction to Divide-and-conquer Haidong Xue Summer 2012, at GSU.
Introduction to Algorithms Quicksort
Design and Analysis of Algorithms Heapsort Haidong Xue Summer 2012, at GSU.
Design and Analysis of Algorithms Review on time complexity, “in place”, “stable” Haidong Xue Summer 2012, at GSU.
BY Lecturer: Aisha Dawood. Heapsort  O(n log n) worst case like merge sort.  Sorts in place like insertion sort.  Combines the best of both algorithms.
Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
Using Divide and Conquer for Sorting
Design and Analysis of Algorithms Quicksort Haidong Xue Summer 2012, at GSU.
Sorting Algorithms and Average Case Time Complexity
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
CS203 Programming with Data Structures Sorting California State University, Los Angeles.
September 19, Algorithms and Data Structures Lecture IV Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Lecture 5: Linear Time Sorting Shang-Hua Teng. Sorting Input: Array A[1...n], of elements in arbitrary order; array size n Output: Array A[1...n] of the.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
CS 253: Algorithms Chapter 6 Heapsort Appendix B.5 Credit: Dr. George Bebis.
Analysis of Algorithms CS 477/677
More sorting algorithms: Heap sort & Radix sort. Heap Data Structure and Heap Sort (Chapter 7.6)
Comp 122, Spring 2004 Heapsort. heapsort - 2 Lin / Devi Comp 122 Heapsort  Combines the better attributes of merge sort and insertion sort. »Like merge.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 7 Heapsort and priority queues Motivation Heaps Building and maintaining heaps.
Analysis of Algorithms CS 477/677 Midterm Exam Review Instructor: George Bebis.
Lecture 5: Master Theorem and Linear Time Sorting
3-Sorting-Intro-Heapsort1 Sorting Dan Barrish-Flood.
CSC 2300 Data Structures & Algorithms March 20, 2007 Chapter 7. Sorting.
HEAPSORT COUNTING SORT RADIX SORT. HEAPSORT O(nlgn) worst case like Merge sort. Like Insertion Sort, but unlike Merge Sort, Heapsort sorts in place: Combines.
CSE 373 Data Structures Lecture 19
Lower Bounds for Comparison-Based Sorting Algorithms (Ch. 8)
Design and Analysis of Algorithms Review of algorithm analysis Haidong Xue Summer 2012, at GSU.
Design and Analysis of Algorithms Non-comparison sort (sorting in linear time) Haidong Xue Summer 2012, at GSU.
Sorting in Linear Time Lower bound for comparison-based sorting
Sorting Algorithms (Part II) Slightly modified definition of the sorting problem: input: A collection of n data items where data item a i has a key, k.
2IL50 Data Structures Spring 2015 Lecture 3: Heaps.
Ch. 6: Heapsort n nodes. Heap -- Nearly binary tree of these n nodes (not just leaves) Heap property If max-heap, the max-heap property is that for every.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
2IL50 Data Structures Fall 2015 Lecture 3: Heaps.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
Sorting Fun1 Chapter 4: Sorting     29  9.
September 29, Algorithms and Data Structures Lecture V Simonas Šaltenis Aalborg University
Lecture 2 Sorting. Sorting Problem Insertion Sort, Merge Sort e.g.,
COMP 171 Data Structures and Algorithms Tutorial 3 Merge Sort & Quick Sort.
Computer Sciences Department1. Sorting algorithm 3 Chapter 6 3Computer Sciences Department Sorting algorithm 1  insertion sort Sorting algorithm 2.
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
Sorting CSIT 402 Data Structures II. 2 Sorting (Ascending Order) Input ›an array A of data records ›a key value in each data record ›a comparison function.
CPSC 311 Section 502 Analysis of Algorithm Fall 2002 Department of Computer Science Texas A&M University.
Chapter 6: Heapsort Combines the good qualities of insertion sort (sort in place) and merge sort (speed) Based on a data structure called a “binary heap”
Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Analysis of Algorithms CS 477/677 Lecture 8 Instructor: Monica Nicolescu.
CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
Chapter 4, Part II Sorting Algorithms. 2 Heap Details A heap is a tree structure where for each subtree the value stored at the root is larger than all.
Sept Heapsort What is a heap? Max-heap? Min-heap? Maintenance of Max-heaps -MaxHeapify -BuildMaxHeap Heapsort -Heapsort -Analysis Priority queues.
Heapsort Lecture 4 Asst. Prof. Dr. İlker Kocabaş.
6.Heapsort. Computer Theory Lab. Chapter 6P.2 Why sorting 1. Sometimes the need to sort information is inherent in a application. 2. Algorithms often.
Sorting Cont. Quick Sort As the name implies quicksort is the fastest known sorting algorithm in practice. Quick-sort is a randomized sorting algorithm.
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Introduction to Algorithms
Design and Analysis of Algorithms Heapsort
CS200: Algorithm Analysis
Heapsort.
Lecture 3 / 4 Algorithm Analysis
CSE 326: Data Structures Sorting
Topic 5: Heap data structure heap sort Priority queue
Sorting And Searching CSE116A,B 4/7/2019 B.Ramamurthy.
The Selection Problem.
CSE 332: Sorting II Spring 2016.
Divide and Conquer Merge sort and quick sort Binary search
Presentation transcript:

Design and Analysis of Algorithms Quicksort Haidong Xue Summer 2012, at GSU

Review of insertion sort and merge sort Insertion sort – Algorithm – Worst case number of comparisons = O(?) Merge sort – Algorithm – Worst case number of comparisons = O(?)

Sorting Algorithms AlgorithmWorst TimeExpected TimeExtra Memory Insertion sortO(1) Merge sortO(n) Quick sortO(1) Heap sortO(1)

Quicksort Algorithm Input: A[1, …, n] Output: A[1,.., n], where A[1]<=A[2]…<=A[n] Quicksort: 1.if(n<=1) return; 2.Choose the pivot p = A[n] 3.Put all elements less than p on the left; put all elements lager than p on the right; put p at the middle. (Partition) 4. Quicksort(the array on the left of p) 5. Quicksort(the array on the right of p)

Quicksort Algorithm Quicksort example Current pivots Previous pivots Quicksort Hi, I am nothing Nothing Jr. Nothing 3rd

Quicksort Algorithm More detail about partition Input: A[1, …, n] (p=A[n]) Output: A[1,…k-1, k, k+1, … n], where A[1, …, k-1] A[k], A[k]=p Partition: 1.t = the tail of smaller array 2.from i= 1 to n-1{ if(A[i]<p) { exchange A[t+1] with A[i]; update t to the new tail; } 3.exchange A[t+1] with A[n];

Quicksort Algorithm Partition example tail Exchange 2 with A[tail+1] Do nothing

Quicksort Algorithm Partition example tail Exchange 1 with A[tail+1] tail Exchange 3 with A[tail+1] Do nothing tail Do nothing Final step: exchange A[n] with A[tail+1]

Quicksort Algorithm The final version of quick sort: Quicksort(A, p, r){ if(p<r){ //if(n<=1) return; q = partition(A, p, r); //small ones on left; //lager ones on right Quicksort(A, p, q-1); Quicksort(A, q+1, r); }

Analysis of Quicksort Time complexity – Worst case – Expected Space complexity - extra memory – 0 = O(1)

Analysis of Quicksort

Strict proof of the worst case time complexity

Strict proof of the expected time complexity

Java implementation of Quicksort Professional programmers DO NOT implement sorting algorithms by themselves We do it for practicing algorithm implementation techniques and understanding those algorithms Code quicksort – Sort.java // the abstract class – Quicksort_Haydon.java // my quicksort implementation – SortingTester.java // correctness tester

Design and Analysis of Algorithms Review on time complexity, “in place”, “stable” Haidong Xue Summer 2012, at GSU

Time complexity of algorithms Execution time? – Pros: easy to obtain; Cons: not accurate Number Instructions? – Pros: very accurate; Cons: calculation is not straightforward Number of certain operations? – Pros: easy to calculate, generally accurate; Cons: not very calculate Asymptotic Notations Pros? Cons?

Time complexity of algorithms In the worst case for(int i=0; i<A.length; i++) C1: cost of “assign a value ” C2: cost of “<” C3: cost of “increment by 1” C4: cost of “==” C5: cost of “return” C1 + (length+1)*C2+ length*C3 length*C4 0*C5 C5 Assuming C4 >> C1, C2, C3, C5 Worst case T(n) = n*C4 =  (n) Worst case T(n) = C1+C2+ C5 + n(C2+C3+C4)+ =  (n)

“in place” and “stable” in sorting algorithms AlgorithmWorst TimeExpected TimeExtra MemoryStable Insertion sortO(1) (in place)Can be Merge sortO(n)Can be Quick sortO(1) (in place)Can be Heap sortO(1) (in place)No Stable Not stable

Design and Analysis of Algorithms Heapsort Haidong Xue Summer 2012, at GSU

Max-Heap A complete binary tree, and … Yes No every level is completely filled, except possibly the last, which is filled from left to right

Max-Heap Satisfy max-heap property: parent >= children Since it is a complete tree, it can be put into an array without lose its structure information.

Max-Heap

Max-Heap Use an array as a heap For element at i: Parent index =parent(i)= floor(i/2); Left child index = left(i)=2*i; Right child index =right(i)=2*i +1 Last non-leaf node = floor(length/2) i=3 2*i = 6 2*i+1=7 floor(i/2)=floor(1.5)=1 floor(length/2)=4

Max-Heapify Input: A compete binary tree rooted at i, whose left and right sub trees are max-heaps; last node index Output: A max-heap rooted at i. Algorithm: 1. Choose the largest node among node i, left(i), right(i). 2. if(the largest node is not i){ – Exchange i with the largest node – Max-Heapify the affected subtree }

Max-Heapify Example

Heapsort for a heap Input: a heap A Output: a sorted array A Algorithm: 1. Last node index l = A’s last node index 2. From the last element to the second{ exchange (current, root); l--; Max-Heapify(A, root, l); }

Heapsort example

Heapsort example

Heapsort example

Heapsort example

Heapsort example

Heapsort example

Heapsort example

Array -> Max-Heap Input: a array A Output: a Max-Heap A Algorithm: Considering A as a complete binary tree, from the last non-leaf node to the first one{ Max-Heapify(A, current index, last index); }

Build Heap Example

Heapsort Input: array A Output: sorted array A Algorithm: 1. Build-Max-Heap(A) 2. Last node index l = A’s last node index 3. From the last element to the second{ exchange (current, root); l--; Max-Heapify(A, root, l); } Let’s try it

Analysis of Heapsort Input: array A Output: sorted array A Algorithm: 1. Build-Max-Heap(A) 2. Last node index l = A’s last node index 3. From the last element to the second{ exchange (current, root); l--; Max-Heapify(A, root, l); } O(n) or O(nlgn) O(nlgn) O(n)

Design and Analysis of Algorithms Non-comparison sort (sorting in linear time) Haidong Xue Summer 2012, at GSU

Comparison based sorting Algorithms that determine sorted order based only on comparisons between the input elements AlgorithmWorst TimeExpected TimeExtra MemoryStable Insertion sortO(1) (in place)Can be Merge sortO(n)Can be Quick sortO(1) (in place)Can be Heap sortO(1) (in place)No What is the lower bound?

Lower bounds for comparison based sorting <? Y N Y …… ………………………….. Done N Y For n element array, how many possible inputs are there? Factorial of n n! What is the shortest tree can have n! leaves? As a result ……

Sorting in linear time Can we sort an array in linear time? Yes, but not for free E.g. sort cards with 13 slots What if there are more than one elements in the same slot?

Counting Sort Input: array A[1, …, n]; k (elements in A have values from 1 to k) Output: sorted array A Algorithm: 1.Create a counter array C[1, …, k] 2.Create an auxiliary array B[1, …, n] 3.Scan A once, record element frequency in C 4.Calculate prefix sum in C 5.Scan A in the reverse order, copy each element to B at the correct position according to C. 6.Copy B to A

Counting Sort A: C: B: Position indicator:

Analysis of Counting Sort Input: array A[1, …, n]; k (elements in A have values from 1 to k) Output: sorted array A Algorithm: 1.Create a counter array C[1, …, k] 2.Create an auxiliary array B[1, …, n] 3.Scan A once, record element frequency in C 4.Calculate prefix sum in C 5.Scan A in the reverse order, copy each element to B at the correct position according to C. 6.Copy B to A Time O(n) O(k) O(n) O(n+k)=O(n) (if k=O(n)) Space O(k) O(n) O(n+k)=O(n) (if k=O(n))

Radix-Sort Input: array A[1, …, n]; d (number of digit a element has) Output: sorted array A Algorithm: for each digit{ use a stable sort to sort A on a digit } T(n)=O(d(n+k))

Summary AlgorithmWorst TimeExpected TimeExtra MemoryStable Insertion sortO(1) (in place)Yes Merge sortO(n)Yes Quick sortO(1) (in place)Yes Heap sortO(1) (in place)No Counting sortYes Design strategies: Divide and conquer Employ certain special data structure Tradeoff between time and space

Knowledge tree Algorithms Analysis Design Algorithms for classic problems Classic data structure Asymptotic notations Probabilisti c analysis Sorting Shortest path Matrix multiplication … Divide & Conquer Greedy Dynamic Programming O(), o(),  (),  (),  () Heap, Hashing, Binary Tree, RBT, …. Quicksort, Heapsort, Mergesort, … ……… … … … ………… … … … … … ……… …