ECE 242 Data Structures and Algorithms. Advanced Sorting I. Lecture 16. Prof.

Similar documents
ECE 242 Data Structures and Algorithms. Advanced Sorting II. Lecture 17. Prof.

DATA STRUCTURES AND ALGORITHMS

Sorting Algorithms. + Analysis of the Sorting Algorithms

CS 171: Introduction to Computer Science II. Quicksort

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

ITEC2620 Introduction to Data Structures

Sorting. Task Description. Selection Sort. Should we worry about speed?

Sort: Divide & Conquer. Data Structures and Algorithms Emory University Jinho D. Choi

About this exam review

Algorithm for siftdown(int currentposition) while true (infinite loop) do if the currentposition has NO children then return

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

Quick Sort. CSE Data Structures May 15, 2002

Quicksort. The divide-and-conquer strategy is used in quicksort. Below the recursion step is described:

Topic 17 Fast Sorting

Chapter 7 Sorting. Terminology. Selection Sort

DATA STRUCTURES AND ALGORITHMS

ECE 2574: Data Structures and Algorithms - Basic Sorting Algorithms. C. L. Wyatt

Sorting and Selection

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

Sorting. Quicksort analysis Bubble sort. November 20, 2017 Hassan Khosravi / Geoffrey Tien 1

Divide and Conquer Sorting Algorithms and Noncomparison-based

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

Sorting. Dr. Baldassano Yu s Elite Education

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

Sorting. Weiss chapter , 8.6

MERGESORT & QUICKSORT cs2420 Introduction to Algorithms and Data Structures Spring 2015

ECE 242 Data Structures and Algorithms. Simple Sorting I. Lecture 4. Prof.

Sorting (I) Hwansoo Han

COMP1511 focuses on writing programs. Effciency is also important. Often need to consider:

Divide and Conquer Algorithms: Advanced Sorting. Prichard Ch. 10.2: Advanced Sorting Algorithms

CS 171: Introduction to Computer Science II. Quicksort

Sorting. Order in the court! sorting 1

COMP Data Structures

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

Sorting and Searching

Lecture 7: Searching and Sorting Algorithms

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm

CSE 143. Two important problems. Searching and Sorting. Review: Linear Search. Review: Binary Search. Example. How Efficient Is Linear Search?

Cosc 241 Programming and Problem Solving Lecture 17 (30/4/18) Quicksort

Sorting. Order in the court! sorting 1

12/1/2016. Sorting. Savitch Chapter 7.4. Why sort. Easier to search (binary search) Sorting used as a step in many algorithms

Key question: how do we pick a good pivot (and what makes a good pivot in the first place)?

Final Examination Semester 1 / Year 2011

Lecture #2. 1 Overview. 2 Worst-Case Analysis vs. Average Case Analysis. 3 Divide-and-Conquer Design Paradigm. 4 Quicksort. 4.

ECE 242 Data Structures and Algorithms. Hash Tables I. Lecture 24. Prof.

CS102 Sorting - Part 2

Binary Trees. Quicksort. SorKng process with quicksort. University of Massachuse5s Amherst ECE 242 Data Structures and Algorithms Lecture 18

QUICKSORT TABLE OF CONTENTS

// walk through array stepping by step amount, moving elements down for (i = unsorted; i >= step && item < a[i-step]; i-=step) { a[i] = a[i-step];

Divide and Conquer Algorithms: Advanced Sorting

Sorting. Lecture10: Sorting II. Sorting Algorithms. Performance of Sorting Algorithms

Cpt S 122 Data Structures. Sorting

Measuring algorithm efficiency

9. The Disorganized Handyman

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

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

Overview of Sorting Algorithms

Sorting algorithms Properties of sorting algorithm 1) Adaptive: speeds up to O(n) when data is nearly sorted 2) Stable: does not change the relative

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych

ECE 242 Data Structures and Algorithms. Heaps I. Lecture 22. Prof. Eric Polizzi

Algorithms and Applications

Lecture Notes 14 More sorting CSS Data Structures and Object-Oriented Programming Professor Clark F. Olson

CSCI-1200 Data Structures Spring 2017 Lecture 15 Problem Solving Techniques, Continued

Elementary Sorting Algorithms

Objectives. Chapter 23 Sorting. Why study sorting? What data to sort? Insertion Sort. CS1: Java Programming Colorado State University

I. Algorithms. examples of cubic, quadratic, NlogN, linear, logarithmic, and constant

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

Hiroki Yasuga, Elisabeth Kolp, Andreas Lang. 25th September 2014, Scientific Programming

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

LINKED LISTS cs2420 Introduction to Algorithms and Data Structures Spring 2015

Sorting. There exist sorting algorithms which have shown to be more efficient in practice.

Lecture 19 Sorting Goodrich, Tamassia

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

Faster Sorting Methods

Searching and Sorting

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Better sorting algorithms (Weiss chapter )

Dual Sorting Algorithm Based on Quick Sort

Real-world sorting (not on exam)

Unit-2 Divide and conquer 2016

Giri Narasimhan. COT 5993: Introduction to Algorithms. ECS 389; Phone: x3748

Programming II (CS300)

QuickSort

Introduction. e.g., the item could be an entire block of information about a student, while the search key might be only the student's name

CS125 : Introduction to Computer Science. Lecture Notes #40 Advanced Sorting Analysis. c 2005, 2004 Jason Zych

Sorting and Searching Algorithms

Lecture 5: Sorting Part A

Shell Sort. Biostatistics 615/815

CSE373: Data Structure & Algorithms Lecture 18: Comparison Sorting. Dan Grossman Fall 2013

protected BinaryNode root; } 02/17/04 Lecture 11 1

ECE 242 Data Structures and Algorithms. Trees IV. Lecture 21. Prof.

Searching & Sorting in Java Shell Sort

CSE 373 NOVEMBER 8 TH COMPARISON SORTS

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

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

Sorting. Riley Porter. CSE373: Data Structures & Algorithms 1

CSC Design and Analysis of Algorithms

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Divide and Conquer

Quicksort (Weiss chapter 8.6)

Transcription:

ECE 242 Data Structures and Algorithms http://www.ecs.umass.edu/~polizzi/teaching/ece242/ Advanced Sorting I Lecture 16 Prof. Eric Polizzi

Sorting Algorithms... so far Bubble Sort Selection Sort Insertion Sort Enhanced Insertion Sort List Insertion Sort MergeSort ShellSort O(N 2 ) O(N 2 ) comp.+ O(N 2 ) swaps O(N 2 ) O(N 2 ) comp.+ O(N) swaps O(N 2 ) O(N 2 ) comp.+ O(N 2 ) copies O(N 2 ) O(NlogN) comp.+ O(N 2 ) copies O(N 2 ) O(N 2 ) comp.+ O(N) copies O(NlogN) O(N) copies by O(logN) levels Slow, slow, slow Intuitive but still slow half #comp. than Bubble O(N) total if data almost sorted Use binary search rather than linear search Only 2N copies Does not work 'in place' Divide & Conquer + Recursive Does not work 'in place' QuickSort 2

ShellSort Intro Named after Donald L. Shell (discovered in 1959) It can be seen as a drastic improvement of the insertion sort algorithm It is much faster than simple sorting but not as fast as quicksort However, the worst case performance is not significantly worse than the average performance (this may not be the case for quicksort) It is sometimes recommended to experiment first with ShellSort before QuickSort 3

ShellSort Intro What is the main problem with Insertion sort? Suppose a key small items is on the far right: example worst case Needs to be inserted... 2 3 5 7 8 35 37 39 40 1 Already sorted Too many copies/shifts (using an array) Too many comparisons (using an array with linear search, or linkedlist) For the example above, just for a single item you need N comparisons with all the other items on the left and N copies for insertion How can we improve insertion sort?. using less copies/comparisons for a single item 4

ShellSort N Sorting Use widely space items (higher increment than 1) Example with 4 Once sorted use less widely space items and so on At each steps the list becomes more sorted and we minimize the amount of work used by insertion sort The last stage uses an increment of 1, which is just insertion sort 5

ShellSort Examples Increment 5 3 0 1 8 7 2 5 4 9 6 Increment 3 2 0 1 8 6 3 5 4 9 7 2 0 1 8 6 3 5 4 9 7 2 0 1 5 4 3 7 6 9 8 Increment 1 (insertion sort) 2 0 1 5 4 3 7 6 9 8 0 1 2 3 4 5 6 7 8 9 6

ShellSort Examples For fun: https://www.youtube.com/watch?v=cmpa7ze8mx0 To do: Test Java applet ShellSort.html 7

ShellSort Why is it faster than insertion sort? When increment is large The number of items to consider per sub list is small and they can move very long distance... this is quite effective When increments grow smaller The number of items to consider per sub list increases, but the items are already closer to their final sorted position Challenge: picking the right increment sequence Shell originally suggested N/2 to start with and then divide in 2 for each pass. Example for N=100, increments of 50,25,12,6,3,1 However, it is very likely with this choice to run into worst case behavior and end up with O(N 2 ) The choice to divide by 2.2 actually improves the behavior (45,20,9,4,1) provide a better mix with the items previously sorted Most popular sequence was proposed by Knuth 40,13,4,1 starting from 1 it follows: h=3*h+1 8

ShellSort Code public void shellsort(double[] array) { int n=array.length; int inner, outer; double temp; // find initial value of increment h (overhead for shellsort) int h=1; while (h<=n/3) h=h*3+1; // sequence (1,4,13,40,121,364,1093) while (h>0) { // decreasing h until 1 - use inverse formula (h-1)/3 for (outer=h;outer<n;outer++){ temp=array[outer]; inner=outer; while (inner>h-1 && array[inner-h]>=temp){ array[inner]=array[inner-h]; inner=inner-h; array[inner]=temp; h=(h-1)/3; // decrease h Short code but not simple to follow 9

ShellSort Efficiency Theoretical analysis is difficult except in special cases Based on experiments there are various estimates Average is O(N(logN) 2 ) 10

Notion of Partitioning Action of dividing into groups depending on key value (a 'custom' divide) Example: You may want to divide employee records into two groups: Employees who live within 15 miles of the office Those who live further away After being partitionned the data is not sorted yet; however it is more sorted that it was before Partitioning is the underlying mechanism of quicksort Note: Partionning is not stable. Each group is not in the same order as it was originally 11

Notion of Partitioning To do: Test Java applet Partition.html Remark: The partitioning process does not necessarily divide the list in half, that depends on the pivot/key value. There may be more items in one group than in the other. 12

Notion of Partitioning Algorithm It uses two probes/pointers at the opposite ends of the array The probe from the left moves towards the right The probe from the right moves toward the left They are both incremented/decremented Left probe stops when it encounters an item larger than pivot Right probe stops when it encounters an item smaller than pivot Both items are swapped The process stops when the probes cross Extra tests necessary to check special case (if all data are below or above the pivot, one of the probe would go all the way beyond the limit of the array) The partitioning algorithm is O(N) (but fewer swaps than comparisons) 13

Notion of Partitioning Code private static void partition(double[] array, double pivot) { int left = -1; int right = array.length; while (true) { while (left<array.length && array[++left]<pivot) { ; // search from left while (right>0 && array[--right]>pivot) { ; // search from right if (left >= right) { // cross-over indicates end of partitioning process break; else { // found misplaced items; swap double temp = array[left]; array[left] = array[right]; array[right] = temp; Note: array[++i] means i=i+1 and then array[i] This order is reversed for array[i++] 14

Logistic Project 1- average is 64.5 Change of rules for projects- New deadline for preliminary version (bonus point) 15