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

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

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

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

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

Better sorting algorithms (Weiss chapter )

CSE 373: Data Structures and Algorithms

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

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

Unit-2 Divide and conquer 2016

DATA STRUCTURES AND ALGORITHMS

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

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

Lecture Notes on Quicksort

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

CSC Design and Analysis of Algorithms

Outline. Quadratic-Time Sorting. Linearithmic-Time Sorting. Conclusion. Bubble/Shaker Sort Insertion Sort Odd-Even Sort

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

CSE 373: Data Structures and Algorithms

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

Chapter 7 Sorting. Terminology. Selection Sort

Data structures. More sorting. Dr. Alex Gerdes DIT961 - VT 2018

Faster Sorting Methods

Sorting. Order in the court! sorting 1

CS1 Lecture 30 Apr. 2, 2018

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

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

Comparison Sorts. Chapter 9.4, 12.1, 12.2

CS 171: Introduction to Computer Science II. Quicksort

Computer Science 4U Unit 1. Programming Concepts and Skills Algorithms

Mergesort again. 1. Split the list into two equal parts

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.

Quicksort (Weiss chapter 8.6)

Sorting Algorithms. + Analysis of the Sorting Algorithms

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

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

Sorting Algorithms Day 2 4/5/17

QuickSort

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

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

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

Mergesort again. 1. Split the list into two equal parts

Lecture Notes on Quicksort

CIS 121 Data Structures and Algorithms with Java Spring Code Snippets and Recurrences Monday, January 29/Tuesday, January 30

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

Sorting. Order in the court! sorting 1

Quick Sort. CSE Data Structures May 15, 2002

SORTING. Comparison of Quadratic Sorts

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

CS102 Sorting - Part 2

Divide and Conquer. Algorithm Fall Semester

Divide and Conquer Algorithms: Advanced Sorting

Chapter 4. Divide-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

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

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

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

Parallel Sorting Algorithms

Divide and Conquer Sorting Algorithms and Noncomparison-based

CSE 373: Data Structures and Algorithms

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

Sorting. Weiss chapter , 8.6

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

Overview of Sorting Algorithms

Randomized Algorithms: Selection

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

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

We can use a max-heap to sort data.

UNIT 7. SEARCH, SORT AND MERGE

BM267 - Introduction to Data Structures

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

Divide-and-Conquer. Dr. Yingwu Zhu

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

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

algorithm evaluation Performance

SORTING AND SELECTION

Lecture 7: Searching and Sorting Algorithms

About this exam review

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

ITEC2620 Introduction to Data Structures

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

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

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

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

Sorting. CPSC 259: Data Structures and Algorithms for Electrical Engineers. Hassan Khosravi

Lecture 8: Mergesort / Quicksort Steven Skiena

SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY

Lecture 19 Sorting Goodrich, Tamassia

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

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

Sorting and Searching

SORTING, SETS, AND SELECTION

CS 171: Introduction to Computer Science II. Quicksort

University of the Western Cape Department of Computer Science

Lecture 2: Getting Started

Lecture 2: Divide&Conquer Paradigm, Merge sort and Quicksort

CS221: Algorithms and Data Structures. Sorting Takes Priority. Steve Wolfman (minor tweaks by Alan Hu)

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

COMP 352 FALL Tutorial 10

MPATE-GE 2618: C Programming for Music Technology. Unit 4.2

Introduction to Algorithms February 24, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Handout 8

Transcription:

CSE 43 Java Sorting Reading: Ch. 3 & Sec. 7.3 Sorting Binary search is a huge speedup over sequential search But requires the list be sorted Slight Problem: How do we get a sorted list? Maintain the list in sorted order as each word is added Sort the entire list when needed Many, many algorithms for sorting have been invented and analyzed Our algorithms mostly assume the data is already in an array Other starting points and assumptions are possible /20/2003 (c) 200-3, University of Washington 23- /20/2003 (c) 200-3, University of Washington 23-2 Insert for a Sorted List One possibility: ensure the list is always sorted as it is created Exercise: Assume that words[0..size-] is sorted. Place new word in correct location so modified list remains sorted Assume that there is spare capacity for the new word Before coding: Draw pictures of an example situation, before and after Write down the postconditions for the operation // given existing list words[0..size-], insert word in correct place and increase size void insertword(string word) { Draw your picture here Picture size++; /20/2003 (c) 200-3, University of Washington 23-3 /20/2003 (c) 200-3, University of Washington 23-4 Insertion Sort Once we have insertword working... We can sort a list in place by repeating the insertion operation void insertionsort( ) { int finalsize = size; size = ; for (int k = ; k < finalsize; k++) { insertword(words[k]); Insertion Sort As A Card Game Operation A bit like sorting a hand full of cards dealt one by one: Pick up st card it's sorted, the hand is sorted Pick up 2 nd card; insert it after or before st both sorted Pick up 3 rd card; insert it after, between, or before st two Each time: Determine where new card goes Make room for the newly inserted card and place it there /20/2003 (c) 200-3, University of Washington 23-5 /20/2003 (c) 200-3, University of Washington 23-6 CSE43 Au03 23-

Insertion Sort As Invariant Progression Insertion Sort sorted unsorted sorted unsorted // instance variable int[ ] list; // list[0..size-] is the list to be sorted int size; // Sort list[0..size-] public void sort { for (int j= ; j < size; j++) { // pre: <= j && j < size && list[0... j-] is in sorted order int temp = list[ j ]; for (int i = j - ; i >= 0 && list[i] > temp ; i-- ) { list[i+] = list[i] ; list[i+] = temp ; // post: <= j && j < size && list[0... j] in sorted order sorted unsorted /20/2003 (c) 200-3, University of Washington 23-7 /20/2003 (c) 200-3, University of Washington 23-8 Initial array contents 0 pear orange 2 apple 3 rutabaga 4 aardvark 5 cherry 6 banana 7 kumquat Insertion Sort Trace Insertion Sort Performance Cost of each insertword operation: Number of times insertword is executed: Total cost: Can we do better? /20/2003 (c) 200-3, University of Washington 23-9 /20/2003 (c) 200-3, University of Washington 23-0 Analysis Why was binary search so much more effective than sequential search? Answer: binary search divided the search space in half each time; sequential search only reduced the search space by item per iteration Why is insertion sort O(n 2 )? Each insert operation only gets more item in place at cost O(n) O(n) insert operations Can we do something similar for sorting? Where are we on the chart? N log 2 N 5N N log 2 N N 2 2 N =============================================================== 8 3 40 24 64 256 6 4 80 64 256 65536 32 5 60 624 ~0 9 64 6 320 384 4096 ~0 9 28 7 640 896 6384 ~0 38 256 8 280 2048 65536 ~0 76 0000 3 5000 5 0 8 ~0 300 /20/2003 (c) 200-3, University of Washington 23- /20/2003 (c) 200-3, University of Washington 23-2 CSE43 Au03 23-2

Divide and Conquer Sorting Idea: emulate binary search in some ways. divide the sorting problem into two subproblems; 2. recursively sort each subproblem; 3. combine results Want division and combination at the end to be fast Want to be able to sort two halves independently This algorithm strategy is called divide and conquer Quicksort Invented by C. A. R. Hoare (962) Idea Pick an element of the list: the pivot Place all elements of the list smaller than the pivot in the half of the list to its left; place larger elements to the right Recursively sort each of the halves Before looking at any code, see if you can draw pictures based just on the first two steps of the description /20/2003 (c) 200-3, University of Washington 23-3 /20/2003 (c) 200-3, University of Washington 23-4 // Sort words[0..size-] void quicksort( ) { qsort(0, size-); Code for QuickSort // Sort words[lo..hi] void qsort(int lo, int hi) { // quit if empty partition if (lo > hi) { return; int pivotlocation = partition(lo, hi); qsort(lo, pivotlocation-); qsort(pivotlocation+, hi); // partition array and return pivot loc Recursion Analysis Base case? Yes. // quit if empty partition if (lo > hi) { return; Recursive cases? Yes qsort(lo, pivotlocation-); qsort(pivotlocation+, hi); Each recursive cases work on a smaller subproblem, so algorithm will terminate /20/2003 (c) 200-3, University of Washington 23-5 /20/2003 (c) 200-3, University of Washington 23-6 A Small Matter of Programming Partition algorithm Pick pivot Rearrange array so all smaller element are to the left, all larger to the right, with pivot in the middle Partition is not recursive Fact of life: partition can be tricky to get right Pictures and invariants are your friends here How do we pick the pivot? For now, keep it simple use the first item in the interval Better strategies exist Partition design We need to partition words[lo..hi] Pick words[lo] as the pivot Picture: /20/2003 (c) 200-3, University of Washington 23-7 /20/2003 (c) 200-3, University of Washington 23-8 CSE43 Au03 23-3

A Partition Implementation Use first element of array section as the pivot Invariant: lo L R hi words x <=x unprocessed >x Partition Algorithm: PseudoCode The two-fingered method // Partition words[lo..hi]; return location of pivot in range lo..hi int partition(int lo, int hi) { pivot /20/2003 (c) 200-3, University of Washington 23-9 /20/2003 (c) 200-3, University of Washington 23-20 Check: partition(0,7) 0 orange pear 2 apple 3 rutabaga 4 aardvark 5 cherry 6 banana 7 kumquat Partition Test Complexity of QuickSort Each call to Quicksort (ignoring recursive calls): Each call of partition( ) is O(n) where n is size of the part of array being sorted Note: This n is smaller than the N of the original problem Some O() work Total = O(n) (n is the size of array part being sorted) Including recursive calls: Two recursive calls at each level of recursion, each partitions half the array at a cost of O(n/2) How many levels of recursion? /20/2003 (c) 200-3, University of Washington 23-2 /20/2003 (c) 200-3, University of Washington 23-22 All boxes are executed (except some of the 0 cases) Total work at each level is O(N) QuickSort (Ideally) N N/2 N/2 N/4 N/4 N/4 N/4......... /20/2003 (c) 200-3, University of Washington 23-23 QuickSort Performance (Ideal Case) Each partition divides the list parts in half Sublist sizes on recursive calls: n, n/2, n/4, n/8. Total depth of recursion: Total work at each level: O(n) Total cost of quicksort:! For a list of 0,000 items Insertion sort: O(n 2 ): 00,000,000 Quicksort: O(n log n): 0,000 log 2 0,000 = 32,877 /20/2003 (c) 200-3, University of Washington 23-24 CSE43 Au03 23-4

Worst Case for QuickSort If we re very unlucky, then each pass through partition removes only a single element. 2 3 4 2 3 4 2 3 4 QuickSort Performance (Worst Case) Each partition manages to pick the largest or smallest item in the list as a pivot Sublist sizes on recursive calls: Total depth of recursion: Total work at each level: O(n) Total cost of quicksort:! 3 4 2 3 4 In this case, we have N levels of recursion rather than log 2 N. What s the total complexity? /20/2003 (c) 200-3, University of Washington 23-25 /20/2003 (c) 200-3, University of Washington 23-26 Worst Case vs Average Case QuickSort as an Instance of Divide and Conquer QuickSort has been shown to work well in the average case (mathematically speaking) In practice, Quicksort works well, provided the pivot is picked with some care Some strategies for choosing the pivot: Compare a small number of list items (3-5) and pick the median for the pivot Pick a pivot element randomly (!) in the range lo..hi Generic Divide and Conquer. Divide 2. Solve subproblems separately (and recursively) 3. Combine subsolutions to get overall solution QuickSort Pick an element of the list: the pivot Place all elements of the list smaller than the pivot in the half of the list to its left; place larger elements to the right Recursively sort each of the halves Surprise! Nothing to do /20/2003 (c) 200-3, University of Washington 23-27 /20/2003 (c) 200-3, University of Washington 23-28 Another Divide-and-Conquer Sort: Mergesort. Split array in half just take the first half and the second half of the array, without rearranging 2. Sort the halves separately 3. Combining the sorted halves ( merge ) repeatedly pick the least element from each array compare, and put the smaller in the resulting array example: if the two arrays are 2 5 20 5 6 3 2 30 The "merged" array is 5 6 2 3 5 20 2 30 note: we will need a second array to hold the result Quicksort vs MergeSort Mergesort always has subproblems of size n/2 Which means guaranteed O(n log n) But mergesort requires an extra array for the result No problem if you re sorting disk or tape files Can be a problem if you re trying to sort large lists in main memory In practice, quicksort is the most commonly used general-purpose sort Pretty easy to pick pivots well, so expected time is O(n log n) Doesn t require extra space for a copy of the data /20/2003 (c) 200-3, University of Washington 23-29 /20/2003 (c) 200-3, University of Washington 23-30 CSE43 Au03 23-5

Summary Divide and Conquer Algorithm design strategy that exploits recursion Divide original problem into subproblems Solve each subproblem recursively Can sometimes yield dramatic performance improvements Sorting Quicksort, mergesort: classic divide and conquer algorithms /20/2003 (c) 200-3, University of Washington 23-3 CSE43 Au03 23-6