Active Learning: Sorting

Similar documents
L14 Quicksort and Performance Optimization

COMP Data Structures

Data Structures and Algorithms

Faster Sorting Methods

CSC Design and Analysis of Algorithms

Sorting. Order in the court! sorting 1

We can use a max-heap to sort data.

Sorting. Order in the court! sorting 1

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

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

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

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

Sorting and Searching

CSCI 204 Introduction to Computer Science II

Sorting Algorithms. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

II (Sorting and) Order Statistics

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

Programming II (CS300)

SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY

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

Unit-2 Divide and conquer 2016

QuickSort

CSC 273 Data Structures

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

26 How Many Times Must a White Dove Sail...

Lecture Notes on Quicksort

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

Sorting: Given a list A with n elements possessing a total order, return a list with the same elements in non-decreasing order.

Sorting. CSE 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation. CSE143 Au

Lecture 7 Quicksort : Principles of Imperative Computation (Spring 2018) Frank Pfenning

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

Sorting and Searching Algorithms

Sorting. CSC 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation CSC Picture

Lecture Notes on Quicksort

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

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.

Sorting is a problem for which we can prove a non-trivial lower bound.

Lecture 19 Sorting Goodrich, Tamassia

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

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Selection (deterministic & randomized): finding the median in linear time

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

Divide-and-Conquer. The most-well known algorithm design strategy: smaller instances. combining these solutions

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

Plan of the lecture. Quick-Sort. Partition of lists (or using extra workspace) Quick-Sort ( 10.2) Quick-Sort Tree. Partitioning arrays

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

Quick Sort. CSE Data Structures May 15, 2002

"Organizing is what you do before you do something, so that when you do it, it is not all mixed up." ~ A. A. Milne SORTING

Divide and Conquer. Algorithm Fall Semester

Cpt S 122 Data Structures. Sorting

"Organizing is what you do before you do something, so that when you do it, it is not all mixed up." ~ A. A. Milne SORTING

Programming II (CS300)

Programming II (CS300)

Comparison Sorts. Chapter 9.4, 12.1, 12.2

9/10/12. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

Algorithms. MIS, KUAS, 2007 Ding

having any value between and. For array element, the plot will have a dot at the intersection of and, subject to scaling constraints.

Quick Sort. Biostatistics 615 Lecture 9

Component 02. Algorithms and programming. Sorting Algorithms and Searching Algorithms. Matthew Robinson

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

SORTING. Comparison of Quadratic Sorts

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

Outline. Computer Science 331. Three Classical Algorithms. The Sorting Problem. Classical Sorting Algorithms. Mike Jacobson. Description Analysis

Lecture 11: In-Place Sorting and Loop Invariants 10:00 AM, Feb 16, 2018

Sorting Algorithms. + Analysis of the Sorting Algorithms

Prof. Sushant S Sundikar 1

Recursion: The Beginning

Question 7.11 Show how heapsort processes the input:

CS 171: Introduction to Computer Science II. Quicksort

What is sorting? Lecture 36: How can computation sort data in order for you? Why is sorting important? What is sorting? 11/30/10

Programming II (CS300)

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

DATA STRUCTURE AND ALGORITHM USING PYTHON

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

InserDonSort. InserDonSort. SelecDonSort. MergeSort. Divide & Conquer? 9/27/12

Overview of Sorting Algorithms

"Organizing is what you do before you do something, so that when you do it, it is not all mixed up." ~ A. A. Milne SORTING

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

4. Sorting and Order-Statistics

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

Scan and Quicksort. 1 Scan. 1.1 Contraction CSE341T 09/20/2017. Lecture 7

Lecture 5: Sorting Part A

S O R T I N G Sorting a list of elements implemented as an array. In all algorithms of this handout the sorting of elements is in ascending order

9. The Disorganized Handyman

1.00 Lecture 34. Sorting 2. Reading for next time: Big Java Counting sort

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

Scribe: Sam Keller (2015), Seth Hildick-Smith (2016), G. Valiant (2017) Date: January 25, 2017

8 Algorithms 8.1. Foundations of Computer Science Cengage Learning

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems.

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

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

Topics Recursive Sorting Algorithms Divide and Conquer technique An O(NlogN) Sorting Alg. using a Heap making use of the heap properties STL Sorting F

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

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.

Elementary maths for GMT. Algorithm analysis Part II

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

UNIT 5C Merge Sort. Course Announcements

CmpSci 187: Programming with Data Structures Spring 2015

Sorting/Searching and File I/O. Sorting Searching Reading for this lecture: L&L

Transcription:

Lecture 32 Active Learning: Sorting Why Is Sorting Interesting? Sorting is an operation that occurs as part of many larger programs. There are many ways to sort, and computer scientists have devoted much research to devising efficient sorting algorithms. Sorting algorithms illustrate a number of important principles of algorithm design, some of them counterintuitive. We will examine a series of sorting algorithms in this session and try and discover the tradeoffs between them. 2 1

Sorting Order Sorting implies that the items to be sorted are ordered. We shall use the same approach that we employed for binary search trees. That is, we will sort Objects, and whenever we invoke a sort, we shall either supply a Comparator to order the elements to be sorted or the sort routine can assume that the objects to be sorted belong to a class like Integer that implements Comparable and possesses a native ordering. 3 The Sort Interface There are times when one wants to sort only part of a collection. We define an interface for sorts with this in mind. A Sort must supply two methods, one to sort an entire array of Objects and one to sort a portion of an array specified by start and end elements (a true closed interval, not half open): public interface Sort { public void sort(object[] d,int start,int end); public void sort(object[] d); } 4 2

Using the Sort Interface This interface makes sorting algorithms become classes. We must create an instance of the class before we can use it to perform a sort. Each sort class will have two constructors. The default constructor will sort elements that have a native order. The other constructor will take a Comparator as the only argument. Here is an example code fragment that creates an instance of InsertionSort to sort an array of Integers: Integer [] iarray = new Integer[ 100 ];... // initialize iarray Sort isort = new InsertionSort(); isort.sort( iarray ); 5 Insertion Sort Insertion sorting is a formalized version of the way most of us sort cards. In an insertion sort, items are selected one at a time from an unsorted list and inserted into a sorted list. It is a good example of an incremental or iterative algorithm. It is simple and intuitive, but we can still do a little to optimize it. To save memory and unnecessary copying we sort the array in place. We do this by using the low end of the array to grow the sorted list against the unsorted upper end of the array. 6 3

Insertion Sort Diagram Sorted Unsorted Initially 7 4 3 6 3 Sorted Unsorted After 1 insertion 4 7 3 6 3... Sorted Unsorted After 3 insertions 3 4 6 7 3 7 Download Simulations Go to the Lecture page on the class web site and download Sorting.zip from the Supporting Files directory using Internet Explorer, not Netscape. Double click the downloaded file and click the Extract button. Use the file dialog box to choose a place to unpack the simulations we will be using today. 8 4

Run the InsertionSort Simulation Navigate to where you unpacked the zip file and double click the InsertionSort.jar file in Windows Explorer, not Forte. It should bring up the simulation that you will use to examine the algorithm. Type in a series of numbers each followed by a return. These are the numbers to sort. Click start and then stepinto to single step through the code. reset restarts the current sort. new allows you to specify a new sort. 9 InsertionSort Questions Use the simulator to explore the following questions and let us know when you think you know the answers: How many elements have to be moved in the inner for loop if you run InsertionSort on an already sorted list? Does it run in O(1), O(n), or O(n 2 ) time? What order of elements will produce the worst performance? Does this case run in O(1), O(n), or O(n 2 ) time? Why? Does the average case have more in common with the best case or the worst case? 10 5

QuickSort Quicksort is a classic and subtle algorithm originally invented by C. A. R. Hoare in 1962. There are now endless variations on the basic algorithm, and it is considered the best overall sorting algorithm for industrial-strength applications. 11 QuickSort Strategy QuickSort is a divide-and-conquer algorithm. It sorts recursively by performing an operation called partitioning on smaller and smaller portions of the array. The essential idea of quicksort is to choose an element of the portion of the array to be sorted called the pivot. Then the algorithm partitions the array with respect to the pivot. Partitioning means separating the array into two subarrays, the left one containing elements that are less than or equal to the pivot and the right one containing elements greater than or equal to the pivot. The algorithm swaps elements to achieve this condition. 12 6

QuickSort Strategy, 2 Note that these subarrays are not sorted! In the version we are using today (but not all QuickSort implementations), we move the pivot between the two partitions. Quicksort is then recursively applied to the subarrays. The algorithm terminates because subarrays of one element are trivially sorted. 13 QuickSort Single Partition 1. Select the last element of the current array segment to be the pivot: pivot... 7 2 5 2 9 3 4... 2. Swap elements to fulfill the partition condition:... 7 2 5 2 9 3 4... 3. Swap pivot with first element of upper half or partition:... 3 2 2 5 9 7 4...... 3 2 2 4 9 7 5... pivot 14 7

Partitioning Partitioning is the key step in quicksort. In our first version of quicksort, the pivot is chosen to be the last element of the (sub)array to be sorted. We scan the (sub)array from the left end using index low looking for an element >= the pivot. When we find one we scan from the left end using index high looking for an element <= pivot. If low <= high, we swap them and start scanning for another pair of swappable elements. If low > high, we are done and we swap low with the pivot, which now stands between the two partitions. 15 Recursive Partitioning 8 4 3 6 2 6 9 7 5 2 4 3 5 8 6 9 7 6 2 4 3 5 6 6 9 7 8 2 4 3 5 6 6 7 8 9 partition swap 5 current pivot pivot swap 5 old pivot 16 8

Quicksort Simulation Double click the QuickSort.jar file. It works the same way as the InsertionSort simulation. 17 QuickSort Questions Use the simulator to explore the following questions and let us know when you think you know the answers: Why do the low and high indices stay within the subarray? How can we be sure that when the procedure terminates, the subarray is legally partitioned? Can low stop at an out of place element without a swap occurring? What if low has passed high? Why won t low be out of place? 18 9

QuickSort Questions, 2 What happens when the pivot is the largest or smallest element in the subarray? Is quicksort more or less efficient if the pivot is consistently the smallest or the largest element of the (sub)array? What input data could make this happen? Why swap high and low when they are both equal to the pivot? Isn t this unnecessary? What will happen if you try to sort an array where all the elements are equal? 19 Quicksort Performance In general, quicksort gives O(n log n ) performance, but in the cases outlined above it will perform as poorly as insertion sort (O( n 2 )) or worse because insertion sort can sort a presorted set in O( n ). 20 10

A Better Quicksort The choice of pivot is crucial to quicksort's performance. The ideal pivot is the median of the subarray, that is, the middle member of the sorted array. But we can't find the median without sorting first. It turns out that the median of the first, middle and last element of each subarray is a good substitute for the median. It guarantees that each part of the partition will have at least two elements, provided that the array has at least four, but its performance is usually much better. And there are no natural cases that will produce worst case behavior. Try running MedianQuickSort from MedianQuickSort.jar. 21 CountingSort InsertionSort and Quicksort both sort by comparing elements. Is there any other way to sort? Assume that you are sorting data with a limited set of integer keys that range from 0 to range-1. CountingSort sorts the data into a temporary array by taking a census of the keys and laying out a directory of where each key must go. 22 11

Steps in the CountingSort Algorithm 1. Copy the numbers to be sorted to a temporary array. 2. Initialize an array indexed by key values (a histogram of keys) to 0. 3. Iterate over the array to be sorted counting the frequency of each key. 4. Now calculate the cumulative histogram for each key value, k. The first element, k=0, is the same as the frequency of key k. The second, k=1, is the sum of the frequency for k=0 and k=1. The third is the sum of the second plus the frequency for k=2. And so on. 23 Steps in the CountingSort Algorithm, 2 5. The first element of the cumulative histogram contains the number of elements of the original array with values <= 0. the second, those <= 1. They lay out blocks of values in the sorted array. 6. Starting with the last element in the original array and working back to the first, look up its key in the cumulative histogram to find its destination in the sorted array. It will be the histogram value 1. 7. Decrement the entry in the cumulative histogram so the next key is not stored on top of the first. 24 12

CountingSort range = 10 4 3 7 6 4 8 3 5 8 2 frequencies 0 1 2 3 4 5 6 7 8 9 0 0 1 2 2 1 1 1 2 0 cumulative histogram 0 1 2 3 4 5 6 7 8 9 0 0 1 3 5 6 7 8 10 10 There are no keys 0, 1, or 9 Key 2 occurs 1 time and should occupy Position 0. Key 3 occurs 2 times and should occupy positions 1 and 2. Key 4 occurs 2 times and should occupy positions 3 and 4. Etc. 25 CountingSort Questions Double click the CountingSort.jar file. Note that since CountingSort requires the specification of a range (or two passes over the data), it can t be implemented using our Sort interface. Experiment with the simulation. Type the numbers to be sorted first. Then enter the range in the range field and press the start button. Use stepinto to trace the code. Does counting sort have a best case or worst case? Does it sort in O(1), O(n), or O(n log n) time? Why? 26 13