Bin Sort. Sorting integers in Range [1,...,n] Add all elements to table and then

Similar documents
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.

4. Sorting and Order-Statistics

SORTING AND SELECTION

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

Module 3: Sorting and Randomized Algorithms. Selection vs. Sorting. Crucial Subroutines. CS Data Structures and Data Management

Module 3: Sorting and Randomized Algorithms

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Sorting Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Lecture 9: Sorting Algorithms

SORTING, SETS, AND SELECTION

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

Module 3: Sorting and Randomized Algorithms

CHAPTER 7 Iris Hui-Ru Jiang Fall 2008

Sorting. Bubble Sort. Pseudo Code for Bubble Sorting: Sorting is ordering a list of elements.

COMP171 Data Structures and Algorithms Fall 2006 Midterm Examination

COMP Data Structures

Unit Outline. Comparing Sorting Algorithms Heapsort Mergesort Quicksort More Comparisons Complexity of Sorting 2 / 33

Sorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort

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

EECS 2011M: Fundamentals of Data Structures

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

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

7. Sorting I. 7.1 Simple Sorting. Problem. Algorithm: IsSorted(A) 1 i j n. Simple Sorting

Sorting (I) Hwansoo Han

The divide and conquer strategy has three basic parts. For a given problem of size n,

II (Sorting and) Order Statistics

Divide and Conquer Sorting Algorithms and Noncomparison-based

Lecture 19 Sorting Goodrich, Tamassia

CSE 326: Data Structures Sorting Conclusion

8. Sorting II. 8.1 Heapsort. Heapsort. [Max-]Heap 6. Heapsort, Quicksort, Mergesort. Binary tree with the following properties Wurzel

Comparison Sorts. Chapter 9.4, 12.1, 12.2

O(n): printing a list of n items to the screen, looking at each item once.

Data Structures and Algorithms Week 4

Data Structures. Sorting. Haim Kaplan & Uri Zwick December 2013

Algorithms and Data Structures for Mathematicians

CPSC 311 Lecture Notes. Sorting and Order Statistics (Chapters 6-9)

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

Introduction to Algorithms and Data Structures. Lecture 12: Sorting (3) Quick sort, complexity of sort algorithms, and counting sort

Randomized Algorithms, Quicksort and Randomized Selection

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

08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58

Data Structures and Algorithms Chapter 4

Divide and Conquer 4-0

CS240 Fall Mike Lam, Professor. Quick Sort

Introduction. Sorting. Definitions and Terminology: Program efficiency. Sorting Algorithm Analysis. 13. Sorting. 13. Sorting.

MergeSort. Algorithm : Design & Analysis [5]

Deliverables. Quick Sort. Randomized Quick Sort. Median Order statistics. Heap Sort. External Merge Sort

Analysis of Algorithms - Quicksort -

Fast Sorting and Selection. A Lower Bound for Worst Case

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

Merge Sort

We can use a max-heap to sort data.

Lecture 5: Sorting Part A

CSE 373: Data Structures and Algorithms

Algorithms Lab 3. (a) What is the minimum number of elements in the heap, as a function of h?

Divide-and-Conquer CSE 680

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

Data Structures and Algorithms

DATA STRUCTURES AND ALGORITHMS

Sorting Algorithms. For special input, O(n) sorting is possible. Between O(n 2 ) and O(nlogn) E.g., input integer between O(n) and O(n)

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

Overview of Sorting Algorithms

Searching in General

Comparison Based Sorting Algorithms. Algorithms and Data Structures: Lower Bounds for Sorting. Comparison Based Sorting Algorithms

Deterministic and Randomized Quicksort. Andreas Klappenecker

Biostatistics 615/815 Lecture 5: Divide and Conquer Algorithms Sorting Algorithms

Syllabus. 3. Sorting. specification

Run Times. Efficiency Issues. Run Times cont d. More on O( ) notation

Algorithms and Data Structures: Lower Bounds for Sorting. ADS: lect 7 slide 1

BM267 - Introduction to Data Structures

Practical Session #11 - Sort properties, QuickSort algorithm, Selection

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

CS 561, Lecture 1. Jared Saia University of New Mexico

1 Probabilistic analysis and randomized algorithms

Question 7.11 Show how heapsort processes the input:

Motivation of Sorting

CS Sorting Terms & Definitions. Comparing Sorting Algorithms. Bubble Sort. Bubble Sort: Graphical Trace

CSci 231 Final Review

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

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

CS 303 Design and Analysis of Algorithms

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

CS Divide and Conquer

Searching, Sorting. part 1

CSE 3101: Introduction to the Design and Analysis of Algorithms. Office hours: Wed 4-6 pm (CSEB 3043), or by appointment.

Sorting. Data structures and Algorithms

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

Introduction. Sorting. Table of Contents

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

Quick Sort. CSE Data Structures May 15, 2002

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING

Quicksort (Weiss chapter 8.6)

Design and Analysis of Algorithms

Sorting. Popular algorithms: Many algorithms for sorting in parallel also exist.

QuickSort and Others. Data Structures and Algorithms Andrei Bulatov

Algorithms and Data Structures. Marcin Sydow. Introduction. QuickSort. Sorting 2. Partition. Limit. CountSort. RadixSort. Summary

Parallel Sorting Algorithms

CSE 326: Data Structures Lecture #16 Sorting Things Out. Unix Tutorial!!

COT 6405: Analysis of Algorithms. Giri Narasimhan. ECS 389; Phone: x3748

Total Points: 60. Duration: 1hr

Transcription:

Sorting1 Bin Sort Sorting integers in Range [1,...,n] Add all elements to table and then Retrieve in order 1, 2, 3,...,n Stable Sorting Method (repeated elements will end up in their original order) Numbers 1 2 3 Bin Add them to bin 1 2 3 Bin 1 i i n n n

Sorting2 Radix Sort Input list 216 521 425 116 91 515 124 34 96 24 After sorting on least significant digit 521 91 124 34 24 425 515 216 116 96 After sorting on 2nd least significant digit 515 216 116 521 124 24 425 34 91 96 After sorting on 3rd least significant digit 24 34 91 96 116 124 216 425 515 521

Sorting3 Sort n numbers whose values are in [0,1,...,n-1] can be done in O(n) time via bin sort (use n bins). Sort n numbers whose values are in [0,1,...,n 2 1] can be done on O(n) time via radix sort. Each key is log n bits long. There are two keys per value. xxxx }{{} xxxx }{{} number in [0,...,n 2 1] have key#2 key#1 2 log n bits. Sort n numbers whose values are in [0,1,...,n k 1] can be done in O(kn) time via radix sort. Each key is log n bits long. There are k keys per value. xxxx }{{} xxxx }{{}... } xxxx {{} number in key#k key#k 1 key#1 [0,...,n k 1] have k log n bits.

Sorting4 Heap Sort Sort x 1, x 2,...,x n for i=1 to n insert x_i in Heap for i=1 to n delete min from Heap Time complexity is O(n log n). As we discussed before, the first phase (insert n elements) can be done in O(n) time. (bottom up) Sorting method is Not Stable

Sorting5 Merge Sort [8][4][5][6][2][1][7][3] in array A [4, 8][5, 6][1, 2][3, 7] in array B, TC is O(n) [4, 5, 6, 8][1, 2, 3, 7] in array A, TC is O(n) [1, 2, 3, 4, 5, 6, 7, 8] in array B, TC is O(n) Number of passes is log 2 n Time complexity is T(n) = 2T(n/2) + cn when considering it top down. Solution to this recurrence relation is O(n log 2 n), and uses O(n) additional space. Stable Sorting method

Sorting6 Merge Sort Analysis Assume, n = 2 k for some positive integer k T(n) = T(1) n = 1 2T(n/2) + cn n > 1 T(n) 2T(n/2) + cn = 2(2T(n/4) + cn/2) + cn = 4T(n/4) + 2cn = 4(2T(n/8) + cn/4) + 2cn = 8T(n/8) + 3cn... (Stop when n/2 k = 1) = 2 k T(1) + kcn = n + cnlog n O(n log n) if 2 k < n 2 k+1, T(n) T(2 k+1 ) T(2n), which is O(n log n)

Sorting7 Quick Sort template<class T> T void QuickSort(T a[],int l,int r) {// Sort a[l:r], a[r+1] has large value. if (l >= r) return; int i = l, // left to right cursor j = r + 1; // right to left cursor T pivot = a[l]; // Partition wrt pivot while (true) { do {// find >= element on left side i = i + 1; } while (a[i] < pivot); do {// find <= element on right side j = j - 1; } while (a[j] > pivot); if (i >= j) break;//swap pair not found Swap(a[i], a[j]); } a[l] = a[j]; a[j] = pivot; QuickSort(a, l, j-1);//sort left segment QuickSort(a, j+1, r);//sort right segment }

Sorting8 QuickSort A[r+1] has value larger than all other elements. Why? Time complexity bound is O(n 2 ). O(n) additional space NONSTABLE Sorting method

Sorting9 QuickSort Worst Case 1 2 3 4... 10... n i=1 i which is O(n2 ) Many instances take Ω(n 2 ) NONSTABLE Sorting Method Space Complexity: (QS(n) = QuickSort on n elements) Ω(n) {}}{ QS(n), QS(n 1), QS(n 2),...,QS(1)

Sorting10 Reduce Space Complexity Make procedure non-recursive Solve the smaller problem first for example sort(1,80) size n sort (1,40), sort(42,80) size n/2 sort(42,62), sort(64,80) size n/4 sort(64,74), sort(76,80)... log 2 (n) space

Sorting11 Quick Sort Note: Changing the recursive program so that it calls the smallest subproblem first has space complexity Q(n). QS(a[],l,r)... if j-1-l <= r-(j+1) then QS(a,l,j-1) QS(a,j+1,r) else QS(a,j+1,r) QS(a,l,j-1) end QS Space n Recursive cells stack {}}{ QS(1, n)qs(2, n), QS(3, n),..., QS(n 1, n)

Sorting12 Iterative Quick Sort QS(...,1,n) S <- NULL PUSH (S,(1,n)) while S is not Empty do (l,r) <- POP(S) if l >= r go to end while... if j-1-l <= r-(j+1) then Push(S,(j+1,r)) //large Push(S,(l,j-1)) //small else Push(S,(l,j-1)) //large Push(S,(j+1,r)) //small endwhile end QS

Sorting13 Iterative Quick Sort Stack has length O(log n) Stack contents at different times for a problem with α values. α < α, α/2 < α, < α/2, α/4 < α, < α/2, < α/4, α/8... Stack has length O(log n).

Sorting14 Time Complexity Worst Case T(n) T(n 1) + cn = T(n 2) + c(n 1) + cn =... = T(1) + (c)2 + (c)3 +... + c(n 1) + c(n) = O(n 2 ) There are examples that show that T(n) is Ω(n 2 ). Best Case T(n)=2T(n/2)+cn O(n log n), like merge sort but constant is smaller

Sorting15 Average Case T(n) T(i) + T(n i 1) + cn A(n) = 2 n na(n) = 2 n 1 (A(j)) + cn j=0 n 1 j=0 A(j) + cn 2 Subtracting from last equation (n 1)A(n 1) = 2 n 2 j=0 A(j) + c(n 1)2 we know that na(n) (n 1)A(n 1) = 2A(n 1) + 2cn c na(n) = (n + 1)A(n 1) + 2cn A(n) n + 1 = A(n 1) n + 2c n + 1 Note that c is not significant!

Sorting16 Adding the following equations A(n) n + 1 A(n 1) n A(n 2) n 1. A(2) 3 = = =. A(n 1) n A(n 2) n 1 A(n 3) n 2. = A(1) 2 + 2c 3 + 2c n + 1 + 2c n + 2c n 1 We know that A(n) n+1 = A(1) 2 + 2c n+1 j=3 1 j The last summation is log e (n + 1) + 0.577 3 2 Therefore, A(n) n+1 = O(log n) Which is equivalent to A(n) = O(n log n) Randomized (random pivot) has expected time complexity equal to A(n).

Sorting17 Lower Bounds for Sorting Decision Trees: Binary tree Nodes represent a set of possible orderings A comparison is made between elements The result is two set of possible orderings Exp. T a < b < c < d a < c < b < d b<c F ordering comparison a < b < c < d a < c < b < d Orderings

Sorting18 Assume elements are distinct Every sorting method (that gains info only by comparing elements) may be viewed as a decision tree. T x1<x2<x3 x1<x3<x2 x2<x1<x3 x2<x3<x1 x3<x1<x2 x3<x2<x1 x1<x2 F x1<x2<x3 x1<x3<x2 x3<x1<x2 x2<x1<x3 x2<x3<x1 x3<x2<x1 x2<x3 x2<x3 T F T F x1<x2<x3 T x1<x3<x2 x1<x3<x2 x3<x1<x2 x1<x3 F x3<x1<x2 x2<x1<x3 x2<x3<x1 T x1<x3 x2<x1<x3 F x2<x3<x1 x3<x2<x1

Sorting19 Lower Bound Sorting n elements x 1, x 2, x 3,...,x n n! leaves in any decision tree Height h of decision tree h log 2 n! ) n/2 Since n! ( n 2 we know that h is Ω(n log n) Sorting method that gain info only by comparing keys take time Ω(n log n)

Sorting20 Lower Bound for Average TC The average number of comparison (for decision tree sorting) is equal to the Average Depth of a Leaf Theorem: For any decision tree with k leaves the ADL log 2 k. Proof: Proof by Contradiction Let T be the least height decision tree with ADL < log 2 k, where k=# of leaves in T. T: k = 1 Not least height T: k >= 2 n n n1 k1<k n2 k2<k k n1 Not least height

Sorting21 Lower Bound for Average TC: Cont Let k = k 1 + k 2, then by Ind. Hypothesis ADL(n 1 ) log 2 k 1, ADL(n 2 ) log 2 k 2, k = k 1 + k 2 ADL k 1 k 1 +k 2 log 2 k 1 + k 2 k 1 +k 2 log 2 k 2 + 1 min when k 1 = k 2 = k/2 ADL is 0.5 log k/2 + 0.5 log k/2 + 1 = log 2 k A contradiction. Note that (0.5+ǫ) log(k/2+ǫ)+(0.5 ǫ) log(k/2 ǫ)+1 is greater simply because log(k/2 + ǫ) log k/2 > log k/2 log(k/2 ǫ)

Sorting22 Summary Algorithm WC AC Stable? Space Binsort O(n) [0, n 1] RadixSort O(kn) [0, n k 1] HeapSort O(n log n) 0(n log n) not stable O(c) QuickSort O(n 2 ) O(n log n) not stable O(log n) Mergesort O(n log n) O(n log n) stable O(n) Insertsort* O(n 2 ) O(n 2 ) stable Insertsort*: Best when sorting 15 elements, used in hybrid algorithms

Sorting23 Sorting of Integers O(n log log n) time: S. Nilsson, The fastest sorting algorithm, Dr. Dobbs Jr. Apr. 2000. Lots of extra space O(n log log n) time and O(n) space, Not practical Y. Han, Determinist Sorting in O(n log log n) time + Linear Space, STOC 02, pp. 602-608.