data structures
play

Data structures Exercise session Week 1 I. Introduction Two kinds - PowerPoint PPT Presentation

Data structures Exercise session Week 1 I. Introduction Two kinds of types in Java Primitive types int, char, boolean, fmoat, double, etc. Object types Integer, Character, String, etc. Java generics Before generics you had to


  1. Data structures Exercise session – Week 1

  2. I. Introduction

  3. Two kinds of types in Java • Primitive types int, char, boolean, fmoat, double, etc. • Object types Integer, Character, String, etc.

  4. Java generics Before generics you had to write: reverse(Integer[] arr) reverse(String[] arr) reverse(Float[] arr) … Go Gotcha! ! Now you can write: E must be an object type! reverse(E[] arr)

  5. Two ways to reverse • functional < E > E[] reverse(E[] str) • in-place <E> void reverse(E[] str)

  6. Which way is the better way?

  7. IsPalindrome? Using functional reverse, it’s one line reverse (chars).equals(chars) (where chars is a Character[] )

  8. Time Complexity

  9. How many times does result++ run? fo for (int j = 1; j <= n; j++) result++;

  10. How many times does result++ run? fo for (int i = 1; i <= n; i ++) fo for (int j = 1; j <= n; j++) result++;

  11. Typical growth functions

  12. ”Big-O” time complexity A function t(n) is classifjed as O( f(n) ) , for some function f(n) , if there exists some +ve c and n’ , such that t(n) <= c * f(n) when n >= n’

  13. Exercise! Arrange the following functions in order of complexity: n 4 , log n, n log n, 4n, 3n 3 , 5n 2 + n.

  14. How many times does result++ run? fo for (int i = 1; i <= n; i ++) fo for (int j = 1; j <= i; j++) result++;

  15. How many times does result++ run? fo for (int j = 1; j <= n; j *= 2) result++;

  16. How many times does result++ run? fo for (int i = 1; i <= n; i *= 2) fo for (int j = 1; j <= n; j++) result++;

  17. How many times does result++ run? fo for (int i = 1; i <= n; i *= 2) fo for (int j = ; j <= i; j++) result++;

  18. Run times for binary search?! Inpu put s t size n e n Ex Exec ecuti tion ti time me 10 8.9 100 17.2 1000 49.5 10000 52.2 100000 60.1 1000000 70.5 10000000 199.0

  19. Still O(log n)! Why? 10 * log( 10 * log(n), Ex Exec ecuti tion ti time me 33.21 8.9 66.43 17.2 99.65 49.5 132.87 52.2 166.09 60.1 199.31 70.5 232.53 199.0

  20. Reading • Weiss, 1.5.8 - Restrictions on Generics • Weiss, 2 – Algorithm Analysis

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