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

data structures
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Data structures

Exercise session – Week 1

slide-2
SLIDE 2
  • I. Introduction
slide-3
SLIDE 3

Two kinds of types in Java

  • Primitive types

int, char, boolean, fmoat, double, etc.

  • Object types

Integer, Character, String, etc.

slide-4
SLIDE 4

Java generics

Before generics you had to write:

reverse(Integer[] arr) reverse(String[] arr) reverse(Float[] arr) …

Now you can write: reverse(E[] arr)

Go Gotcha! ! E must be an object type!

slide-5
SLIDE 5

Two ways to reverse

  • functional

<E> E[] reverse(E[] str)

  • in-place

<E> void reverse(E[] str)

slide-6
SLIDE 6

Which way is the better way?

slide-7
SLIDE 7

IsPalindrome?

Using functional reverse, it’s one line

reverse (chars).equals(chars)

(where chars is a Character[])

slide-8
SLIDE 8

Time Complexity

slide-9
SLIDE 9

How many times does result++ run?

fo for (int j = 1; j <= n; j++) result++;

slide-10
SLIDE 10

How many times does result++ run?

fo for (int i = 1; i <= n; i ++) fo for (int j = 1; j <= n; j++) result++;

slide-11
SLIDE 11

Typical growth functions

slide-12
SLIDE 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’

slide-13
SLIDE 13

Exercise!

Arrange the following functions in

  • rder of complexity:

n4, log n, n log n, 4n, 3n3, 5n2 + n.

slide-14
SLIDE 14

How many times does result++ run?

fo for (int i = 1; i <= n; i ++) fo for (int j = 1; j <= i; j++) result++;

slide-15
SLIDE 15

How many times does result++ run?

fo for (int j = 1; j <= n; j *= 2) result++;

slide-16
SLIDE 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++;

slide-17
SLIDE 17

How many times does result++ run?

fo for (int i = 1; i <= n; i *= 2) fo for (int j = ; j <= i; j++) result++;

slide-18
SLIDE 18

Run times for binary search?!

Inpu put s t size n e n 10 100 1000 10000 100000 1000000 10000000 Ex Exec ecuti tion ti time me 8.9 17.2 49.5 52.2 60.1 70.5 199.0

slide-19
SLIDE 19

Still O(log n)! Why?

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

slide-20
SLIDE 20

Reading

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