Sorting. 4.2 Sorting and Searching. Sorting. Sorting. Insertion Sort. Sorting. Sorting problem. Rearrange N items in ascending order.

Similar documents
4.2 Sorting and Searching. Section 4.2

4.1, 4.2 Performance, with Sorting

4.2 Sorting and Searching. Section 4.2

4.1 Performance. Running Time. The Challenge. Scientific Method. Reasons to Analyze Algorithms. Algorithmic Successes

Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS

input sort left half sort right half merge results Mergesort overview

Lecture T5: Analysis of Algorithm

4.1 Performance. Running Time. The Challenge. Scientific Method

Running Time. Analytic Engine. Charles Babbage (1864) how many times do you have to turn the crank?

CS/COE 1501

13. Sorting and Searching

CS 112 Introduction to Programming

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

Analysis of Algorithms

4.1 Performance. Running Time. Scientific Method. Algorithmic Successes

Algorithms. Algorithms 2.2 MERGESORT. mergesort bottom-up mergesort sorting complexity comparators stability ROBERT SEDGEWICK KEVIN WAYNE

Algorithms. Algorithms 2.2 MERGESORT. mergesort bottom-up mergesort sorting complexity comparators stability ROBERT SEDGEWICK KEVIN WAYNE

1.4 Analysis of Algorithms

Running Time. Analytic Engine. Charles Babbage (1864) how many times do you have to turn the crank?

CS/COE 1501 cs.pitt.edu/~bill/1501/ Introduction

CS/COE 1501


Algorithms 2.2 MERGESORT. mergesort bottom-up mergesort sorting complexity comparators stability

Algorithms 2.2 MERGESORT. mergesort bottom-up mergesort sorting complexity comparators stability

1.4 Analysis of Algorithms

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

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

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

Algorithms 1.4 ANALYSIS OF ALGORITHMS. observations mathematical models order-of-growth classifications dependencies on inputs memory

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

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

Merge Sort Algorithm

Mergesort. ! mergesort! sorting complexity! comparators. ! bottom-up mergesort. Two classic sorting algorithms

ANALYSIS OF ALGORITHMS

Algorithms. Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS. introduction. introduction observations. observations

Algorithms. Algorithms. Algorithms 2.2 M ERGESORT. mergesort. bottom-up mergesort. sorting complexity comparators stability

Advanced Algorithms and Data Structures

Searching in General

Divide and Conquer. Algorithm Fall Semester

Lecture 1. Introduction / Insertion Sort / Merge Sort

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

Algorithms. Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS

CS.7.A.Performance.Challenge

Unit-2 Divide and conquer 2016

Data Structures 1.4 ANALYSIS OF ALGORITHMS. introduction observations mathematical models order-of-growth classifications theory of algorithms memory

Algorithms. Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS. introduction. introduction observations. observations

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

Elementary Sorts. rules of the game selection sort insertion sort sorting challenges shellsort. Sorting problem. Ex. Student record in a University.

Cpt S 122 Data Structures. Sorting

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

Algorithms and Data Structures, Fall Introduction. Rasmus Pagh. Based on slides by Kevin Wayne, Princeton. Monday, August 29, 11

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

Divide and Conquer 4-0

CSC 273 Data Structures

Better sorting algorithms (Weiss chapter )

Merge Sort Roberto Hibbler Dept. of Computer Science Florida Institute of Technology Melbourne, FL

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

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

MERGESORT. Textbook (pages )

Algorithms. Algorithms 1.5 UNION-FIND. dynamic connectivity quick find quick union improvements applications ROBERT SEDGEWICK KEVIN WAYNE

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

Performance analysis.

4.1 Performance Analysis

Lecture 19 Sorting Goodrich, Tamassia

Lecture 2: Getting Started

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

CS-A1140 Data structures and algorithms

2.5 Arrays. Arrays. Choosing a Random Student. Arrays in Java

Computer Programming

CS 171: Introduction to Computer Science II. Quicksort

Quicksort (Weiss chapter 8.6)

Principles of Algorithm Analysis. Biostatistics 615/815

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

Sorting. Weiss chapter , 8.6

Algorithms 1.5 UNION FIND. dynamic connectivity quick find quick union improvements applications

Algorithms 1.5 UNION FIND. dynamic connectivity quick find quick union improvements applications

2.5 Arrays. Arrays. Choosing a Random Student. Arrays in Java

For searching and sorting algorithms, this is particularly dependent on the number of data elements.

COSC242 Lecture 7 Mergesort and Quicksort

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

Sorting Algorithms. + Analysis of the Sorting Algorithms

Performance. CSCI 135: Fundamentals of Computer Science Keith Vertanen. h"p://

Lecture 6 Sorting and Searching

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

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

COMP Data Structures

Chapter Contents. An Introduction to Sorting. Selection Sort. Selection Sort. Selection Sort. Iterative Selection Sort. Chapter 9

Sorting and Searching Algorithms

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

! 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.

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

Introduction to Computers and Programming. Today

Algorithmic Analysis and Sorting, Part Two. CS106B Winter

1.1 Your First Program

Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved

Algorithms. Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS

MERGE SORT SYSTEM IJIRT Volume 1 Issue 7 ISSN:

Elementary Sorts. ! rules of the game! selection sort! insertion sort! sorting challenges! shellsort. Sorting problem

COS 226 Algorithms and Data Structures Fall Midterm Solutions

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.

Transcription:

4.2 and Searching pentrust.org Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright 2002 2010 23/2/2012 15:04:54 pentrust.org pentrust.org shanghaiscrap.org shanghaiscrap.org De Beers problem. Rearrange items in ascending order. Applications. Statistics, databases, data compression, bioinformatics, computer graphics, scientific computing, (too numerous to list),... Insertion Sort Hauser Hong Hsu Hayes Haskell Hanley Hornet Hanley Haskell Hauser Hayes Hong Hornet Hsu The Bridge Table 14 15 1

Comparsions (millions) Insertion Sort Insertion Sort Insertion sort. Brute-force sorting solution. Move left-to-right through array. Exchange next element with larger elements to its left, one-by-one. Insertion sort. Brute-force sorting solution. Move left-to-right through array. Exchange next element with larger elements to its left, one-by-one. 16 17 Insertion Sort: Java Implementation Insertion Sort: Empirical Analysis public class Insertion { public static void sort(string[] a) { int = a.length; for (int i = 1; i < ; i++) for (int j = i; j > 0; j--) if (a[j-1].compareto(a[j]) > 0) exch(a, j-1, j); else break; private static void exch(string[] a, int i, int j) { String swap = a[i]; a[i] = a[j]; a[j] = swap; Observation. umber of compares depends on input family. Descending: ~ 2 / 2. Random: ~ 2 / 4. Ascending: ~. 1 0000000 Descendng Random 1 00000 Ascendi ng 1 000 10 0.1 0.001 1 000 1 0000 1 00000 1 000000 Input Size 18 19 Insertion Sort: Mathematical Analysis Challenge 1 Worst case. [descending] Iteration i requires i comparisons. Total = (0 + 1 + 2 +... + -1) ~ 2 / 2 compares. E F G H I J D C B A i Average case. [random] Iteration i requires i / 2 comparisons on average. Total = (0 + 1 + 2 +... + -1) / 2 ~ 2 / 4 compares Q. A credit card company sorts 10 million customer account numbers, for use with binary search. Using insertion sort, what kind of computer is needed? A. Toaster B. Cell phone C. Your laptop D. Supercomputer E. Google server farm A C D F H J E B I G i 20 21 2

Insertion Sort: Lesson Moore's Law Lesson. Supercomputer can't rescue a bad algorithm. Moore's law. Transistor density on a chip doubles every 2 years. Variants. Memory, disk space, bandwidth, computing power per $. Computer Per Second Thousand Million Billion laptop 10 7 instant 1 day 3 centuries super 10 12 instant 1 second 2 weeks http://en.wikipedia.org/wiki/moore's_law 22 23 Moore's Law and Algorithms Quadratic algorithms do not scale with technology. ew computer may be 10x as fast. But, has 10x as much memory so problem may be 10x bigger. With quadratic algorithm, takes 10x as long! Software inefficiency can always outpace Moore's Law. Moore's Law isn't a match for our bad coding. Jaron Lanier Lesson. eed linear (or linearithmic) algorithm to keep pace with Moore's law. 24 25 : Example algorithm. Divide array into two halves. Recursively sort each half. Merge two halves to make sorted whole. 26 27 3

Merging Merging Merging. Combine two pre-sorted lists into a sorted whole. How to merge efficiently? Use an auxiliary array. Merging. Combine two pre-sorted lists into a sorted whole. How to merge efficiently? Use an auxiliary array. String[] aux = new String[]; // merge into auxiliary array int i = lo, j = mid; for (int k = 0; k < ; k++) { if (i == mid) aux[k] = a[j++]; else if (j == hi) aux[k] = a[i++]; else if (a[j].compareto(a[i]) < 0) aux[k] = a[j++]; else aux[k] = a[i++]; // copy back for (int k = 0; k < ; k++) { a[lo + k] = aux[k]; 28 29 : Java Implementation : Mathematical Analysis public class Merge { public static void sort(string[] a) { sort(a, 0, a.length); Analysis. To mergesort array of size, mergesort two subarrays of size / 2, and merge them together using compares. we assume is a power of 2 // Sort a[lo, hi). public static void sort(string[] a, int lo, int hi) { int = hi - lo; if ( <= 1) return; // recursively sort left and right halves int mid = lo + /2; sort(a, lo, mid); sort(a, mid, hi); T( / 2) T( / 4) T( / 4) T() T( / 2) T( / 4) T( / 4) 2 ( / 2) 4 ( / 4) // merge sorted halves (see previous slide) T( / 2 k ) log 2... lo mid hi T(2) T(2) T(2) T(2) T(2) T(2) T(2) T(2) / 2 (2) 10 11 12 13 14 15 16 17 18 19 log 2 30 31 : Mathematical Analysis Challenge 2 Mathematical analysis. analysis worst comparisons log 2 Q. A credit card company sorts 10 million customer account numbers, for use with binary search. Using mergesort, what kind of computer is needed? average log 2 best 1/2 log 2 Validation. Theory agrees with observations. A. Toaster B. Cell phone C. Your laptop D. Supercomputer E. Google server farm 10,000 20 million 50 million actual 120 thousand 460 million 1,216 million predicted 133 thousand 485 million 1,279 million 32 33 4

Challenge 3 : Lesson Q. What's the fastest way to sort 1 million 32-bit integers? Lesson. Great algorithms can be more powerful than supercomputers. Computer Compares Per Second Insertion laptop 10 7 3 centuries 3 hours super 10 12 2 weeks instant = 1 billion 34 35 Insertion Sort: Empirical Analysis Insertion Sort: Observation Data analysis. Plot # comparisons vs. input size on log-log scale. 100000 Observe and tabulate running time for various values of. Data source: random numbers between 0 and 1. Machine: Apple G5 1.8GHz with 1.5GB memory running OS X. Timing: Skagen wristwatch. 10000 Actual 1000 Fitted 100 5,000 6.2 million 0.13 seconds 10,000 25 million 0.43 seconds 10 20,000 99 million 1.5 seconds 1 1000 10000 100000 1000000 400 million 5.6 seconds Input Size 80,000 1600 million 23 seconds slope Hypothesis. # comparisons grows quadratically with input size ~ 2 / 4. 49 50 Insertion Sort: Prediction and Verification Insertion Sort: Mathematical Analysis Experimental hypothesis. # comparisons ~ 2 /4. Mathematical analysis. Prediction. 400 million comparisons for =. Analysis Stddev Observations. 401.3 million 399.7 million 5.595 sec 5.573 sec Agrees. Worst Average Best 2 / 2 2 / 4-1/6 3/2-401.6 million 5.648 sec 400.0 million 5.632 sec Validation. Theory agrees with observations. Prediction. 10 billion comparisons for = 200,000. Actual Predicted 401.3 million 400 million Observation. 200,000 9.9997 billion 10.000 billion 200,000 9.997 billion 145 seconds Agrees. 51 52 5

: Preliminary Hypothesis : Prediction and Verification Experimental hypothesis. umber of comparisons ~ 20. Experimental hypothesis. umber of comparisons ~ 20. Prediction. 80 million comparisons for = 4 million. 1000 100 Insertion sort Observations. 4 million 4 million 82.7 million 82.7 million 3.13 sec 3.25 sec Agrees. 4 million 82.7 million 3.22 sec 10 1 Prediction. 400 million comparisons for = 20 million. 0.1 Observations. 1000 10000 100000 1000000 Input Size 20 million 50 million 460 million 1216 million 17.5 sec 45.9 sec ot quite. 53 54 6