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.

Slides:



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

Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 6.
CSE 3101: Introduction to the Design and Analysis of Algorithms
ADA: 5. Quicksort1 Objective o describe the quicksort algorithm, it's partition function, and analyse its running time under different data conditions.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Spring 2015 Lecture 5: QuickSort & Selection
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
Analysis of Algorithms CS 477/677 Randomizing Quicksort Instructor: George Bebis (Appendix C.2, Appendix C.3) (Chapter 5, Chapter 7)
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
CMPS1371 Introduction to Computing for Engineers SORTING.
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.
Ch. 7 - QuickSort Quick but not Guaranteed. Ch.7 - QuickSort Another Divide-and-Conquer sorting algorithm… As it turns out, MERGESORT and HEAPSORT, although.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
TTIT33 Algorithms and Optimization – Dalg Lecture 2 HT TTIT33 Algorithms and optimization Lecture 2 Algorithms Sorting [GT] 3.1.2, 11 [LD] ,
Quicksort CIS 606 Spring Quicksort Worst-case running time: Θ(n 2 ). Expected running time: Θ(n lg n). Constants hidden in Θ(n lg n) are small.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Analysis of Algorithms CS 477/677
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
David Luebke 1 8/17/2015 CS 332: Algorithms Linear-Time Sorting Continued Medians and Order Statistics.
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
Order Statistics The ith order statistic in a set of n elements is the ith smallest element The minimum is thus the 1st order statistic The maximum is.
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.
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.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
Analysis of Algorithms CS 477/677
Introduction to Algorithms Jiafen Liu Sept
David Luebke 1 6/3/2016 CS 332: Algorithms Analyzing Quicksort: Average Case.
Chapter 9: Selection Order Statistics What are an order statistic? min, max median, i th smallest, etc. Selection means finding a particular order statistic.
Sorting CS 110: Data Structures and Algorithms First Semester,
Algorithms and their Applications CS2004 ( ) Professor Jasna Kuljis (adapted from Dr Steve Swift) 6.1 Classic Algorithms - Sorting.
1 Sorting (Bubble Sort, Insertion Sort, Selection Sort)
Order Statistics(Selection Problem)
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
QuickSort (Ch. 7) Like Merge-Sort, based on the three-step process of divide- and-conquer. Input: An array A[1…n] of comparable elements, the starting.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director
COSC 3101A - Design and Analysis of Algorithms 6 Lower Bounds for Sorting Counting / Radix / Bucket Sort Many of these slides are taken from Monica Nicolescu,
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
Today’s Material Sorting: Definitions Basic Sorting Algorithms
QuickSort. Yet another sorting algorithm! Usually faster than other algorithms on average, although worst-case is O(n 2 ) Divide-and-conquer: –Divide:
Sorting & Lower Bounds Jeff Edmonds York University COSC 3101 Lecture 5.
INTRO2CS Tirgul 8 1. Searching and Sorting  Tips for debugging  Binary search  Sorting algorithms:  Bogo sort  Bubble sort  Quick sort and maybe.
David Luebke 1 6/26/2016 CS 332: Algorithms Linear-Time Sorting Continued Medians and Order Statistics.
David Luebke 1 7/2/2016 CS 332: Algorithms Linear-Time Sorting: Review + Bucket Sort Medians and Order Statistics.
Chapter 9: Selection of Order Statistics What are an order statistic? min, max median, i th smallest, etc. Selection means finding a particular order statistic.
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Analysis of Algorithms CS 477/677
Linear-Time Sorting Continued Medians and Order Statistics
Randomized Algorithms
Chapter 7 Sorting Spring 14
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Algorithm design and Analysis
Quick Sort (11.2) CSE 2011 Winter November 2018.
Randomized Algorithms
Data Structures Review Session
CS 101 – Oct. 21 Sorting Much-studied problem in CS – many ways to do it Given a list of data, need to arrange it “in order” Some methods do better based.
CS 3343: Analysis of Algorithms
CS 332: Algorithms Quicksort David Luebke /9/2019.
CSE 373 Data Structures and Algorithms
Algorithms: Design and Analysis
The Selection Problem.
Quicksort and Randomized Algs
Presentation transcript:

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 element without sorting.

The problem Arrange items in a sequence so that their keys are in ascending or descending order. Long history of research. Many new algorithms are tweaks of famous sorting algorithms. Some methods do better based on how values distributed, size of data, nature of underlying hardware (parallel processing, memory hierarchy), etc. Some implementations given on the class Web site and also:

Some methods Selection sort: Find the largest value and swap it into first position, find 2 nd largest value and put it 2 nd, etc. Bubble sort: Scan the list and see which consecutive values are out of order, and swap them. Multiple passes are required. Insertion sort: Place the next element in the correct place by shifting other ones over to make room. We maintain a boundary between the sorted and unsorted parts of the list. Merge sort: Split list in half until just 1-2 elements. Merge adjacent lists by collating them.

Analysis What is the best case (  ) time for any sorting algorithm? Selection, bubble, and insertion sort all run in O(n 2 ) time. –Why? –Among the three: which is the best, which is the worst? Merge sort runs in O(n log 2 n) time. –If we imagine the tree of recursive calls, the nested calls go about log 2 n deep. At each level, we must do O(1) work at each of the n values. –Later, we will use a more systematic approach to compute the complexity of recursive algorithms.

Quick sort Like merge sort, it’s a recursive algorithm based on divide and conquer. Call quickSort initially with parameters (array, 0, n – 1). quickSort(a, p, r): if p < r: q = partition(a, p, r) quickSort(a, p, q) quickSort(a, q+1, r) What makes quick sort distinctive is its partitioning. See handout.

QS partitioning Given a sub-array spanning indices p..r Let x = value of first element here, i.e. a[p] We want to put smaller values on left side and larger values on right side of this array slice. We return the location of the boundary between the low and high regions. Practice with handout!

Counting sort Designed to run in linear time Works well when range of values is not large Find how many values are less than x = a[i]. That will tell you where x belongs in output sorted array. for i = 1 to k: C[i] = 0 for i = 1 to n:// Let C[x] = #elements == x ++ C[A[i]] for i = 2 to k:// Let C[x] = #elements <= x C[i] += C[i – 1] for i = n downto 1:// Put sorted values into B. B[C[A[i]]] = A[i] -- C[A[i]]// try: 3,6,4,1,3,4,1,4

Bucket sort Assume array’s values are (more or less) evenly distributed over some range. Create n buckets, each covering 1/n of the range. Insert each a[i] into the appropriate bucket. If a bucket winds up with 2+ values, use any method to sort them. Ex. { 63, 42, 87, 37, 60, 58, 95, 75, 97, 3 } –We can define buckets by tens

Radix sort Old and simple method. Sort values based on their ones’ digit. –In other words, write down all numbers ending with 0, followed by all numbers ending with 1, etc. Continue: Sort by the tens’ digit. Then by the hundreds’ digit, etc. Can easily be modified to alphabetize words. Technique also useful for sorting records by several fields.

Stooge sort Designed to show that divide & conquer does not automatically mean a faster algorithm. Soon we will learn how to mathematically determine the exact O(g(n)) runtime. stoogeSort(A, i, j): if A[i] > A[j]: swap A[i] and A[j] if i+1 >= j: return k = (j – i + 1) / 3 stoogeSort(A, i, j – k) // how much of A? stoogeSort(A, i + k, j) stoogeSort(A, i, j – k)

Selection The “selection problem” is: given a sequence of values, return the k-th smallest value, for some k. –If k = 1 or n, problem is simple. –It would be easy to write a O(n log n) algorithm by sorting all values first. But this does unnecessary work. A randomized method with expected runtime of O(n). –Based on randomized quick sort: choose any value to be the pivot –So it’s called “randomized quick select” –Algorithm takes as input S and k, where 1  k  n. (Indices count from 1)

Pseudocode quickSelect(S,k): if n = 1 return S[1] x = random element from S L = [ all elements < x ] E = [ all elements == x ] G = [ all elements > x ] if k <= |L| return quickSelect(L, k) else if k <= |L| + |E| return x else return quickSelect(G, k – |L| - |E|) // e.g. 12 th out of 20 = 2 nd out of 10

Analysis To find O(g(n)), we are going to find an upper bound on the “expected” execution time. Expected, as in expected value – the long term average if you repeated the random experiment many times. Book has some preliminary notes… –You can add expected values, but not probabilities. –Consider rolling 1 vs. 2 dice. P 1 (rolling 4) = 1/6 but P 2 (rolling 8) = 5/36 So the probabilities don’t add! You can only add probability if it’s 2 alternatives of the same experiment, e.g. rolling a 4 or a 5 on one die: 1/6+1/6. –Exp(1 die) = 3.5Exp(2 dice) = 7 These values can add.

Selection proof We want to show that the selection algorithm is O(n). The algorithm is based on partitioning S. –Define a “good” partition = where x is in the middle half of the distribution of values (not in the middle half of locations). –Probability = ½, and sizes of L and of G used for the next recursive call have sizes .75n. How many recursive calls until we have a “good” partition? Same as asking how many times a coin flips until we get heads: we would expect 2. Overhead in doing one function invocation. –We need a loop, so this is O(n). Say, bn for some constant b. T(n) = expected time of algorithm T(n)  T(.75n) + 2bn

Work out recurrence T(n)  T(.75n) + 2bn Let’s expand T(.75n)  T(.75 2 n) + 2b(.75n) Substitute: T(n)  T(.75 2 n) + 2b(.75n) + 2bn Expand: T(.75 2 n)  T(.75 3 n) + 2b(.75 2 n) Substitute: T(n)  T(.75 3 n) + 2b(.75 2 n) + 2b(.75n) + 2bn We can keep going and eventually the argument of T on the right side becomes at most 1. (The O(1) base case.) When does that occur? Solve for k: (.75 k n)  1 (3/4) k  1/n  (4/3) k  n  k  log 4/3 n  k = ceil(log 4/3 n) So, T(n)  T(1) + k terms of 2bn multiplied by.75 i (for i = 0 to k) (This sum is at most 4.) T(n)  O(1) + 2bn (4) = O(n)

Merge sort We can use the same technique to analyze merge sort. (p. 248). Let’s look at cost of recursive case: T(n) = 2 T(n/2) + cn Expand recursive case: T(n/2) = 2 T(n/4) + c(n/2) Substitute: T(n) = 2 T(n/2) + cn = 2 [ 2 T(n/4) + c(n/2) ] + cn = 4 T(n/4) + 2cn Expand: T(n/4) = 2 T(n/8) + c(n/4) Substitute: T(n) = 4 T(n/4) + cn = 4 [ 2 T(n/8) + c(n/4) ] + 2cn = 8 T(n/8) + 3cn. See a pattern? T(n) = 2 k T(n/2 k ) + kcn At some point, n/2 k = 1  n = 2 k  k = log 2 n. T(n) = 2 log 2 n T(1) + (log 2 n) cn = O(n log 2 n).

Stooge sort Yes, even Stooge sort can be analyzed a similar way! T(n) = 3 T((2/3)n) + cn Expand: T((2/3)n) = 3 T((4/9) n) + c((2/3)n) Substitute: T(n) = 3 T((2/3)n) + cn = 3 [ 3 T((4/9) n) + c((2/3)n) ] + cn = 9 T((4/9) n) + 3cn Expand: T((4/9) n) = 3 T((8/27) n) + c((4/9)n) Substitute: T(n) = 9 T((4/9) n) + 3cn = 9 [3 T((8/27) n) + c((4/9)n) ] + 3cn = 27 T((8/27) n) + 7cn Continuing, we observe: T(n) = 3 k T((2/3) k n) + (2 k – 1)cn

Stooge sort (2) At some point, the recursive argument reaches (or goes below) 1. ((2/3) k n) = 1  (2/3) k = 1/n  (3/2) k = n  k = log 3/2 n So T(n) = 3 log 3/2 n T(1) + (2 log 3/2 n ) – 1) cn = O(3 log 3/2 n ) + O(n 2 log 3/2 n ) Is this exponential complexity? No – let’s simplify: 3 log 3/2 n = ((3/2) log 3/2 3 ) log 3/2 n = ((3/2) log 3/2 n ) log 3/2 3 = n log 3/2 3 The other term can be simplified similarly and we have n 1 + log 3/2 2 which turns out to be the same order. T(n) = O(n log 3/2 3 ).