08 a sorting iv
play

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


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

  2. Bucket Sort Puzzlers 1 Bucket Sort 2 Puzzlers CS1102S: Data Structures and Algorithms 08 A: Sorting IV 2

  3. Bucket Sort Recall: A Counter-Example Puzzlers Bucket Sort 1 Bucket Sort Recall: A Counter-Example Bucket Sort 2 Puzzlers CS1102S: Data Structures and Algorithms 08 A: Sorting IV 3

  4. Bucket Sort Recall: A Counter-Example Puzzlers 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

  5. Bucket Sort Recall: A Counter-Example Puzzlers 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

  6. Bucket Sort Recall: A Counter-Example Puzzlers 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

  7. Bucket Sort Recall: A Counter-Example Puzzlers 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

  8. Bucket Sort Recall: A Counter-Example Puzzlers 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

  9. Bucket Sort Previous Puzzler: The Last Laugh Puzzlers 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

  10. Bucket Sort Previous Puzzler: The Last Laugh Puzzlers 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

  11. Bucket Sort Previous Puzzler: The Last Laugh Puzzlers 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

  12. Bucket Sort Previous Puzzler: The Last Laugh Puzzlers 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

  13. Bucket Sort Previous Puzzler: The Last Laugh Puzzlers 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

  14. Bucket Sort Previous Puzzler: The Last Laugh Puzzlers 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

  15. Bucket Sort Previous Puzzler: The Last Laugh Puzzlers 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 extends MoneyPrinter { D o l l a r P r i n t e r 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

  16. Bucket Sort Previous Puzzler: The Last Laugh Puzzlers 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend