Sorting DS 2017/2018

Similar documents
Sorting algorithms. Sorting algorithms

Fundamental Algorithms

Fundamental Algorithms

Sorting. Chapter 11. CSE 2011 Prof. J. Elder Last Updated: :11 AM

Review Of Topics. Review: Induction

Sorting Algorithms. We have already seen: Selection-sort Insertion-sort Heap-sort. We will see: Bubble-sort Merge-sort Quick-sort

Lecture 4. Quicksort

Algorithms And Programming I. Lecture 5 Quicksort

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

Fast Sorting and Selection. A Lower Bound for Worst Case

Insertion Sort. We take the first element: 34 is sorted

Outline. 1 Introduction. 3 Quicksort. 4 Analysis. 5 References. Idea. 1 Choose an element x and reorder the array as follows:

Algorithms. Quicksort. Slide credit: David Luebke (Virginia)

Divide-and-conquer: Order Statistics. Curs: Fall 2017

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Algorithms, CSE, OSU Quicksort. Instructor: Anastasios Sidiropoulos

Week 5: Quicksort, Lower bound, Greedy

Data Structures and Algorithms CSE 465

ITEC2620 Introduction to Data Structures

1. Basic Algorithms: Bubble Sort

CS 361, Lecture 14. Outline

Central Algorithmic Techniques. Iterative Algorithms

Divide and Conquer Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 14

Bucket-Sort. Have seen lower bound of Ω(nlog n) for comparisonbased. Some cheating algorithms achieve O(n), given certain assumptions re input

Quicksort. Where Average and Worst Case Differ. S.V. N. (vishy) Vishwanathan. University of California, Santa Cruz

1. Basic Algorithms: Bubble Sort

Divide-and-conquer. Curs 2015

Solutions. Problem 1: Suppose a polynomial in n of degree d has the form

Data Structures and Algorithms

Randomized Sorting Algorithms Quick sort can be converted to a randomized algorithm by picking the pivot element randomly. In this case we can show th

Lecture 14: Nov. 11 & 13

Advanced Analysis of Algorithms - Midterm (Solutions)

CMPT 307 : Divide-and-Conqer (Study Guide) Should be read in conjunction with the text June 2, 2015

Data selection. Lower complexity bound for sorting

CS173 Running Time and Big-O. Tandy Warnow

Algorithm efficiency analysis

Recommended readings: Description of Quicksort in my notes, Ch7 of your CLRS text.

data structures and algorithms lecture 2

5. DIVIDE AND CONQUER I

Quicksort (CLRS 7) We previously saw how the divide-and-conquer technique can be used to design sorting algorithm Merge-sort

1 Quick Sort LECTURE 7. OHSU/OGI (Winter 2009) ANALYSIS AND DESIGN OF ALGORITHMS

Data Structures in Java

Analysis of Algorithms. Randomizing Quicksort

CS 332: Algorithms. Linear-Time Sorting. Order statistics. Slide credit: David Luebke (Virginia)

Divide & Conquer. Jordi Cortadella and Jordi Petit Department of Computer Science

Data Structures and Algorithm Analysis (CSC317) Randomized Algorithms (part 3)

COMP 250: Quicksort. Carlos G. Oliver. February 13, Carlos G. Oliver COMP 250: Quicksort February 13, / 21

Algorithms, Design and Analysis. Order of growth. Table 2.1. Big-oh. Asymptotic growth rate. Types of formulas for basic operation count

Divide & Conquer. Jordi Cortadella and Jordi Petit Department of Computer Science

Partition and Select

Divide and Conquer Strategy

Problem Set 2 Solutions

CS325: Analysis of Algorithms, Fall Midterm

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: Oct 26. Homework #2. ( Due: Nov 8 )

AC64/AT64/ AC115/AT115 DESIGN & ANALYSIS OF ALGORITHMS

Algorithms Test 1. Question 1. (10 points) for (i = 1; i <= n; i++) { j = 1; while (j < n) {

Analysis of Algorithms CMPSC 565

CSCE 750, Spring 2001 Notes 2 Page 1 4 Chapter 4 Sorting ffl Reasons for studying sorting is a big deal pedagogically useful Λ the application itself

V. Adamchik 1. Recurrences. Victor Adamchik Fall of 2005

Longest Common Prefixes

Divide and conquer. Philip II of Macedon

5. DIVIDE AND CONQUER I

Linear Selection and Linear Sorting

CS 161 Summer 2009 Homework #2 Sample Solutions

Data Structures and Algorithm Analysis (CSC317) Randomized algorithms

Asymptotic Running Time of Algorithms

Recap: Prefix Sums. Given A: set of n integers Find B: prefix sums 1 / 86

Algorithm Design and Analysis

On Partial Sorting. Conrado Martínez. Univ. Politècnica de Catalunya, Spain

Divide&Conquer: MergeSort. Algorithmic Thinking Luay Nakhleh Department of Computer Science Rice University Spring 2014

Design and Analysis of Algorithms

Design and Analysis of Algorithms

1 Divide and Conquer (September 3)

Divide and Conquer Algorithms

CPS 616 DIVIDE-AND-CONQUER 6-1

CSCI 3110 Assignment 6 Solutions

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures

5. DIVIDE AND CONQUER I

COL 730: Parallel Programming

Problem 5. Use mathematical induction to show that when n is an exact power of two, the solution of the recurrence

MIDTERM I CMPS Winter 2013 Warmuth

Outline. 1 Introduction. Merging and MergeSort. 3 Analysis. 4 Reference

About complexity. Number theoretical algorithms

Quick Sort Notes , Spring 2010

CS483 Design and Analysis of Algorithms

Searching. Sorting. Lambdas

University of New Mexico Department of Computer Science. Midterm Examination. CS 361 Data Structures and Algorithms Spring, 2003

CSE 312, Winter 2011, W.L.Ruzzo. 8. Average-Case Analysis of Algorithms + Randomized Algorithms

COMP Analysis of Algorithms & Data Structures

CSE 613: Parallel Programming. Lecture 9 ( Divide-and-Conquer: Partitioning for Selection and Sorting )

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

Sorting and Searching. Tony Wong

Linear Time Selection

CSE 421, Spring 2017, W.L.Ruzzo. 8. Average-Case Analysis of Algorithms + Randomized Algorithms

Outline. 1 Merging. 2 Merge Sort. 3 Complexity of Sorting. 4 Merge Sort and Other Sorts 2 / 10

CS361 Homework #3 Solutions

Discrete Mathematics: Logic. Discrete Mathematics: Lecture 17. Recurrence

Divide and Conquer Algorithms

Chapter 4 Divide-and-Conquer

Transcription:

Sorting DS 2017/2018

Content Sorting based on comparisons Bubble sort Insertion sort Selection sort Merge sort Quick sort Counting sort Distribution sort FII, UAIC Lecture 8 DS 2017/2018 2 / 44

The sorting problem Case 1: Input: n, (v 0,..., v n 1 ) Output: (w0,..., w n 1 ) such that (w 0,..., w n 1 ) is a permutation of (v 0,..., v n 1 ) and w 0... w n 1 Case 2: Input: n, (R0,..., R n 1 ) with the keys k 0,..., k n 1 Output: (R 0,..., R n 1 ) such that (R 0,..., R n 1 ) is a permutation of (R 0,..., R n 1 ) and R 0.k 0... R n 1.k n 1 Data structure Array a[0..n 1] a[0] = v 0,..., a[n 1] = v n 1 FII, UAIC Lecture 8 DS 2017/2018 3 / 44

Content Sorting based on comparisons Bubble sort Insertion sort Selection sort Merge sort Quick sort Counting sort Distribution sort FII, UAIC Lecture 8 DS 2017/2018 4 / 44

Bubble-sort Basic principle: (i, j) with i < j is an inversion if a[i] > a[j] While there is an inversion (i, i + 1) interchange a[i] with a[i + 1] Algorithm: Procedure bubblesort(a, n) begin last n 1 while (last > 0) do n1 last 1; last 0 for i 0 to n1 do if (a[i] > a[i + 1]) then swap(a[i], a[i + 1]) last i end FII, UAIC Lecture 8 DS 2017/2018 5 / 44

Bubble sort - example 3 2 1 4 7 (n1 = 2) 3 7 2 1 4 (n1 = 3) 2 3 1 4 7 3 7 2 1 4 2 3 1 4 7 3 2 7 1 4 2 1 3 4 7 3 2 7 1 4 2 1 3 4 7 3 2 1 7 4 2 1 3 4 7 3 2 1 7 4 3 2 1 4 7 2 1 3 4 7 (n1 = 0) 3 2 1 4 7 1 2 3 4 7 1 2 3 4 7 FII, UAIC Lecture 8 DS 2017/2018 6 / 44

Bubble sort Analysis Worst-case performance a[0] > a[1] >... > a[n 1] Time for searching: O(n 1 + n 2 +... + 1) = O(n 2 ) T bubblesort (n) = O(n 2 ) Best-case performance: O(n) FII, UAIC Lecture 8 DS 2017/2018 7 / 44

Content Sorting based on comparisons Bubble sort Insertion sort Selection sort Merge sort Quick sort Counting sort Distribution sort FII, UAIC Lecture 8 DS 2017/2018 8 / 44

Insertion sort Basic principle: suppose a[0..i 1] sorted insert a[i] such that a[0..i] becomes sorted Algorithm (search sequentially the position of a[i]): Procedure insertsort(a, n) begin for i 1 to n 1 do j i 1 // a[0..i 1] sorted temp a[i] // search the place of temp while ((j 0) and (a[j] > temp)) do a[j + 1] a[j] j j 1 if (a[j + 1]! = temp) then a[j + 1] temp end FII, UAIC Lecture 8 DS 2017/2018 9 / 44

Insertion sort Example 3 7 2 1 3 7 2 1 2 3 7 1 1 2 3 7 Analysis searching the position i in a[0..j 1] needs O(j 1) steps worst-case scenario a[0] > a[1] >... > a[n 1] Search time: O(1 + 2 +... + n 1) = O(n 2 ) T insertsort (n) = O(n 2 ) best-case scenario: O(n) FII, UAIC Lecture 8 DS 2017/2018 10 / 44

Content Sorting based on comparisons Bubble sort Insertion sort Selection sort Merge sort Quick sort Counting sort Distribution sort FII, UAIC Lecture 8 DS 2017/2018 11 / 44

Selection sort Apply the following scheme: the current step: select an element and bring it on the final position in the sorted table; repeat the current step until all elements reach the final positions. After the type of the selection of an element: Naive selection: choose elements in the order they are initially (from n 1 to 0 or from 0 to n 1) Systematic selection: use max-heap FII, UAIC Lecture 8 DS 2017/2018 12 / 44

Selection sort (naive selection) In the order n 1, n 2,..., 1, 0, namely: ( i)0 i < n = a[i] = max{a[0],..., a[i]} Procedure naivsort(a, n) begin for i n 1 downto 1 do imax i for j i 1 downto 0 do if (a[j] > a[imax]) then imax j if (i! = imax) then swap(a[i], a[imax]) end The time complexity for all cases is O(n 2 ) FII, UAIC Lecture 8 DS 2017/2018 13 / 44

Heap sort (sorting by systematic selection) Phase I organize the table like a max-heap: ( k)1 k n 1 = a[k] a[(k 1)/2]; initially the table satisfies the max-heap property starting with the position n/2; insert in max-heap the elements from the positions n/2 1, n/2 2,, 1, 0. 3 9 8 7 1 12 4 5 2 0 1 2 3 4 5 6 7 8 FII, UAIC Lecture 8 DS 2017/2018 14 / 44

Heap sort (sorting by systematic selection) Phase II select the maximum element and bring it on his place by interchanging it with the last element; decrease n by 1 and then redo the max-heap; repeat the above steps until all elements reach their place. FII, UAIC Lecture 8 DS 2017/2018 15 / 44

Heap sort (sorting by systematic selection) 9 7 8 5 1 3 4 2 12 0 1 2 3 4 5 6 7 8 FII, UAIC Lecture 8 DS 2017/2018 16 / 44

The operation of insertion in heap Procedure inserttth(a, n, t) begin j t heap false while ((2 j + 1 < n) and not heap) do k 2 j + 1 if ((k < n 1) and (a[k] < a[k + 1])) then k k + 1 if (a[j] < a[k]) then swap(a[j], a[k]) j k else heap true end FII, UAIC Lecture 8 DS 2017/2018 17 / 44

Heap sort Procedure heapsort(a, n) begin // build the max-heap for t (n 1)/2 downto 0 do inserttth(a, n, t) // remove r n 1 while (r > 0) do swap(a[0], a[r]) inserttth(a, r, 0) r r 1 end FII, UAIC Lecture 8 DS 2017/2018 18 / 44

Heap sort - Example 10 17 5 23 7 (n = 5) 10 17 5 23 7 10 23 5 17 7 23 10 5 17 7 23 17 5 10 7 (max-heap n) FII, UAIC Lecture 8 DS 2017/2018 19 / 44

Heap sort - Example 23 17 5 10 7 (max-heap n) 7 17 5 10 23 17 10 5 7 23 (max-heap n-1) 7 10 5 17 23 10 7 5 17 23 (max-heap n-2) 5 7 10 17 23 7 5 10 17 23 (max-heap n-3) 5 7 10 17 23 5 7 10 17 23 (max-heap n-4) 5 7 10 17 23 FII, UAIC Lecture 8 DS 2017/2018 20 / 44

Heap sort - complexity building the heap (suppose n = 2 k 1) k 1 i=0 2(k i 1)2i = 2 k+1 2(k + 1) remove from heap and redo the heap k 1 i=0 2i2i = (k 2)2 k+1 + 4 complexity of the sorting algorithm T heapsort (n) = 2nlogn 2n = O(nlogn) FII, UAIC Lecture 8 DS 2017/2018 21 / 44

Content Sorting based on comparisons Bubble sort Insertion sort Selection sort Merge sort Quick sort Counting sort Distribution sort FII, UAIC Lecture 8 DS 2017/2018 22 / 44

Divide-et-impera paradigm P(n): problem of dimension n base: if n n0 then solve P by elementary methods divide-et-impera: divide P in a problems P1 (n 1 ),..., P a (n a ) cu n i n/b, b > 1 solve P1 (n 1 ),..., P a (n a ) in the same way and obtain the solutions S 1,..., S a assemble S 1,..., S a to obtain the solution S to the problem P FII, UAIC Lecture 8 DS 2017/2018 23 / 44

Paradigma divide-et-impera: algorithm Procedure DivideEtImpera(P, n, S) begin if (n n 0 ) then find S by elementary methods else Divide P in P 1,..., P a DivideEtImpera(P 1, n 1, S 1 )... DivideEtImpera(P a, n a, S a ) Assemble(S 1,..., S a, S) end FII, UAIC Lecture 8 DS 2017/2018 24 / 44

Merge sort generalization: a[p..q] base: p q divide-et-impera divide: m = [(p + q)/2] subproblems: a[p..m], a[m + 1..q] assembling: interclass the sorted subsequences a[p..m] şi a[m + 1..q] initially store the result of interclassing in temp copy from temp[0..q p + 1] in a[p..q] complexity: time: T (n) = O(n log n) additional space: O(n) FII, UAIC Lecture 8 DS 2017/2018 25 / 44

Interclassing two sorted sequences the problem: given a[0] a[1] a[m 1], b[0] b[1] b[n 1], construct c[0] c[1] c[m + n 1] such that ( k)(( i)c[k] = a[i]) ( j)c[k] = b[j]) and for k! = p, c[k] and c[p] come from different elements the solution initially: i 0, j 0, k 0 the current step: if a[i] b[j] then c[k] a[i], i i + 1 if a[i] > b[j] then c[k] b[j], j j + 1 k k + 1 termination criterion: i > m 1 or j > n 1 if it is the case, copy in c the elements from the unfinished table FII, UAIC Lecture 8 DS 2017/2018 26 / 44

Content Sorting based on comparisons Bubble sort Insertion sort Selection sort Merge sort Quick sort Counting sort Distribution sort FII, UAIC Lecture 8 DS 2017/2018 27 / 44

Quick sort generalization: a[p..q] base: p q divide-et-impera divide: find k between p and q by interchanges such that after finding k we have: p i k = a[i] a[k] k < j q = a[k] a[j] subproblems: a[p..k 1], a[k + 1..q] assembling: none FII, UAIC Lecture 8 DS 2017/2018 28 / 44

Quick sort: partitioning initially: x a[p] (may choose x arbitrarily from a[p..q]) i p + 1; j q the current step: if a[i] x then i i + 1 if a[j] x then j j 1 if a[i] > x > a[j] si i < j then swap(a[i], a[j]) i i + 1 j j 1 termination: the condition i > j operations k i 1 swap(a[p], a[k]) FII, UAIC Lecture 8 DS 2017/2018 29 / 44

Quick sort: partitioning - example Procedure partitioning(a, p, q, k) begin x a[p] i p + 1 j q while (i <= j) do if (a[i] <= x) then i i + 1 if (a[j] >= x) then j j 1 if (i < j) and (a[i] > x) and (x > a[j]) then swap(a[i], a[j]) i i + 1 j j 1 k i 1 a[p] a[k] a[k] x end FII, UAIC Lecture 8 DS 2017/2018 30 / 44

Quick sort: recursion - example Procedure quicksort(a, p, q) begin while (p < q) do partitioning(a, p, q, k) quicksort(a, p, k 1) quicksort(a, k + 1, q) end FII, UAIC Lecture 8 DS 2017/2018 31 / 44

Quick sort: the recursion tree FII, UAIC Lecture 8 DS 2017/2018 32 / 44

Quick sort - complexity The choice of the pivot influences the efficiency of the algorithm Worst-case scenario: the pivot is the smallest (the biggest) value. The time is proportional with n + n 1 +... + 1. T quicksort (n) = O(n 2 ) The recursion tree: FII, UAIC Lecture 8 DS 2017/2018 33 / 44

Quick sort - complexity A good pivot divides the table in two subtables of comparable dimensions The height of the recursion tree is O(log n) The average complexity is O(n log n) FII, UAIC Lecture 8 DS 2017/2018 34 / 44

Content Sorting based on comparisons Bubble sort Insertion sort Selection sort Merge sort Quick sort Counting sort Distribution sort FII, UAIC Lecture 8 DS 2017/2018 35 / 44

Counting sort Assumption: a[i] {1, 2,..., k} Find the position of each element in the sorted table by counting how many elements are lower than him 1 Procedure countingsort(a, b, n, k) 2 begin 3 for i 1 to k do 4 c[i] 0 5 for j 0 to n 1 do 6 c[a[j]] c[a[j]] + 1 7 for i 2 to k do 8 c[i] c[i] + c[i 1] 9 for j n 1 downto 0 do 10 b[c[a[j]] 1] a[j] 11 c[a[j]] c[a[j]] 1 12 end Complexity: O(k + n) FII, UAIC Lecture 8 DS 2017/2018 36 / 44

Counting sort example (k = 6) FII, UAIC Lecture 8 DS 2017/2018 37 / 44

Content Sorting based on comparisons Bubble sort Insertion sort Selection sort Merge sort Quick sort Counting sort Distribution sort FII, UAIC Lecture 8 DS 2017/2018 38 / 44

Distribution sort Assumption: the elements a[i] are uniformly distributed over the interval [0, 1) Principle: divide the interval [0, 1) in n subintervals of equal sizes, indexed from 0 to n 1; distribute the elements a[i] in the corresponding interval: n a[i] ; sort each package using another method; combine the n packages in a sorted list. FII, UAIC Lecture 8 DS 2017/2018 39 / 44

Distribution sort Algorithm: Procedure bucketsort(a, n) begin for i 0 to n 1 do insert(b[ n a[i] ], a[i]) for i 0 to n 1 do sort the list B[i] concatenate in order the lists B[0], B[1],, B[n 1] end The average complexity: O(n) FII, UAIC Lecture 8 DS 2017/2018 40 / 44

Distribution sort example (Cormen T.H. et al., Introducere în algoritmi) FII, UAIC Lecture 8 DS 2017/2018 41 / 44

Sorting - complexity Algorithm Case best-case average worst-case bubblesort n n 2 n 2 insertsort n n 2 n 2 naivsort n 2 n 2 n 2 heapsort n log n n log n n log n mergesort n log n n log n n log n quicksort n log n n log n n 2 countingsort n + k n + k bucketsort n FII, UAIC Lecture 8 DS 2017/2018 42 / 44

When a sorting algorithm is favourite? A sorting method is stable if it keeps unchanged the relative order of elements with identical keys Recommendations Quick sort: when you don t need a stable method and the average performance is more important than the worst one; O(n log n) - the average time complexity, O(log n) auxiliary space Merge sort: when a stable method is required; time complexity O(n log n); drawback: O(n) auxiliary space, a larger constant than QuickSort Heap sort: when you don t need a stable method and you are interested more in the worst case performance than the average one; time O(n log n), space O(1) Insert sort: when n is small FII, UAIC Lecture 8 DS 2017/2018 43 / 44

When a sorting algorithm is favourite? Under some limited conditions, it is possible a sorting in O(n) Counting sort: the values are in an interval Bucket sort: the values are approximately uniformly distributed FII, UAIC Lecture 8 DS 2017/2018 44 / 44