08 A: Sorting IV CS1102S: Data Structures and Algorithms Martin - - PowerPoint PPT Presentation

08 a sorting iv
SMART_READER_LITE
LIVE PREVIEW

08 A: Sorting IV CS1102S: Data Structures and Algorithms Martin - - PowerPoint PPT Presentation

Bucket Sort Puzzlers 08 A: Sorting IV CS1102S: Data Structures and Algorithms Martin Henz March 12, 2010 Generated on Friday 12 th March, 2010, 11:26 CS1102S: Data Structures and Algorithms 08 A: Sorting IV 1 Bucket Sort Puzzlers 1


slide-1
SLIDE 1

Bucket Sort Puzzlers

08 A: Sorting IV

CS1102S: Data Structures and Algorithms

Martin Henz

March 12, 2010

Generated on Friday 12th March, 2010, 11:26 CS1102S: Data Structures and Algorithms 08 A: Sorting IV 1

slide-2
SLIDE 2

Bucket Sort Puzzlers

1

Bucket Sort

2

Puzzlers

CS1102S: Data Structures and Algorithms 08 A: Sorting IV 2

slide-3
SLIDE 3

Bucket Sort Puzzlers Recall: A Counter-Example Bucket Sort

1

Bucket Sort Recall: A Counter-Example Bucket Sort

2

Puzzlers

CS1102S: Data Structures and Algorithms 08 A: Sorting IV 3

slide-4
SLIDE 4

Bucket Sort Puzzlers Recall: A Counter-Example Bucket Sort

Counter-example: Sorting Small Distinct Integers

Input Array a of N distinct integers from 1 to M Sorting algorithm int [ ] helper = new int [M] ; for ( int i =0; i < N; i ++) helper [ a [ i ] ] = a [ i ] ; int index = 0; for ( int j =0; j < M; j ++) i f ( helper [ j ] ! = 0 ) a [ index ++] = helper [ j ] ;

CS1102S: Data Structures and Algorithms 08 A: Sorting IV 4

slide-5
SLIDE 5

Bucket Sort Puzzlers Recall: A Counter-Example Bucket Sort

Counter-example: Sorting Small Distinct Integers

int [ ] helper = new int [M] ; for ( int i =0; i < N; i ++) helper [ a [ i ] ] = a [ i ] ; int index = 0; for ( int j =0; j < M; j ++) i f ( helper [ j ] ! = 0 ) a [ index ++] = helper [ j ] ; Analysis Runtime O(M + N)

CS1102S: Data Structures and Algorithms 08 A: Sorting IV 5

slide-6
SLIDE 6

Bucket Sort Puzzlers Recall: A Counter-Example Bucket Sort

A Couple of Ideas

We do not have to store the element in the array; boolean values will do How can we drop the condition “distinct integers”? Instead of boolean values in the array helper, we count the number of occurrences of a given integer

CS1102S: Data Structures and Algorithms 08 A: Sorting IV 6

slide-7
SLIDE 7

Bucket Sort Puzzlers Recall: A Counter-Example Bucket Sort

Bucket Sort

int [ ] helper = new int [M] ; for ( int i =0; i < N; i ++) helper [ a [ i ] ] + + ; int index = 0; for ( int j =0; j < M; j ++) while ( helper [ j ]−− != 0) a [ index ++] = j ;

CS1102S: Data Structures and Algorithms 08 A: Sorting IV 7

slide-8
SLIDE 8

Bucket Sort Puzzlers Recall: A Counter-Example Bucket Sort

Bucket Sort

int [ ] helper = new int [M] ; for ( int i =0; i < N; i ++) helper [ a [ i ] ] + + ; int index = 0; for ( int j =0; j < M; j ++) while ( helper [ j ]−− != 0) a [ index ++] = j ; Analysis Runtime O(M + N)

CS1102S: Data Structures and Algorithms 08 A: Sorting IV 8

slide-9
SLIDE 9

Bucket Sort Puzzlers Previous Puzzler: The Last Laugh This Week’s Puzzler: Printing Money

1

Bucket Sort

2

Puzzlers Previous Puzzler: The Last Laugh This Week’s Puzzler: Printing Money

CS1102S: Data Structures and Algorithms 08 A: Sorting IV 9

slide-10
SLIDE 10

Bucket Sort Puzzlers Previous Puzzler: The Last Laugh This Week’s Puzzler: Printing Money

1

Bucket Sort

2

Puzzlers Previous Puzzler: The Last Laugh This Week’s Puzzler: Printing Money

CS1102S: Data Structures and Algorithms 08 A: Sorting IV 10

slide-11
SLIDE 11

Bucket Sort Puzzlers Previous Puzzler: The Last Laugh This Week’s Puzzler: Printing Money

Previous Puzzler: The Last Laugh

What does the following program print? public class LastLaugh { public static void main ( String [ ] args ) { System . out . p r i n t l n ( ”H” + ” a ” ) ; System . out . p r i n t l n ( ’H ’ + ’a ’ ) ; } }

CS1102S: Data Structures and Algorithms 08 A: Sorting IV 11

slide-12
SLIDE 12

Bucket Sort Puzzlers Previous Puzzler: The Last Laugh This Week’s Puzzler: Printing Money

Previous Puzzler: The Last Laugh

What does the following program print? public class LastLaugh { public static void main ( String [ ] args ) { System . out . p r i n t l n ( ”H” + ” a ” ) ; System . out . p r i n t l n ( ’H ’ + ’a ’ ) ; } } This program prints: Ha 169

CS1102S: Data Structures and Algorithms 08 A: Sorting IV 12

slide-13
SLIDE 13

Bucket Sort Puzzlers Previous Puzzler: The Last Laugh This Week’s Puzzler: Printing Money

Why?

Characters in Java are 16-bit numbers. Therefore, ’H’ + ’a’ performs addition of the corresponding character codes as integers: 72 + 97 = 169. What if I want to use characters as strings? Write: ”” + ’H’ + ’a’ What does this code print: System.out.println(”2 + 2 = ” + 2+2);

CS1102S: Data Structures and Algorithms 08 A: Sorting IV 13

slide-14
SLIDE 14

Bucket Sort Puzzlers Previous Puzzler: The Last Laugh This Week’s Puzzler: Printing Money

Summary

Special treatment of + The + operator performs string concatenation if and only if at least one of its operands is of type String.

CS1102S: Data Structures and Algorithms 08 A: Sorting IV 14

slide-15
SLIDE 15

Bucket Sort Puzzlers Previous Puzzler: The Last Laugh This Week’s Puzzler: Printing Money

New Puzzler: Printing Money

class Money {} class Dollar extends Money {} class MoneyPrinter { public void p r i n t (Money x ) { System . out . p r i n t l n ( ”Money ! ” ) ; } } class D o l l a r P r i n t e r extends MoneyPrinter { public void p r i n t ( Dollar x ) { System . out . p r i n t l n ( ” Dollar ! ” ) ; } }

CS1102S: Data Structures and Algorithms 08 A: Sorting IV 15

slide-16
SLIDE 16

Bucket Sort Puzzlers Previous Puzzler: The Last Laugh This Week’s Puzzler: Printing Money

New Puzzler: Printing Money

Dollar d = new Dollar ( ) ; MoneyPrinter p = new D o l l a r P r i n t e r ( ) ; p . p r i n t ( d ) ;

CS1102S: Data Structures and Algorithms 08 A: Sorting IV 16