Top Banner
1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time-Complexity Analysis, Memory Model, Object Oriented Programming
89

1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

1

Gentle Introduction to Programming

Session 5: Sorting, Searching, Time-Complexity Analysis, Memory Model,

Object Oriented Programming

Page 2: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

2

Admin.

• Please come on time after the first break• Who goes to the Mathematics' summer introduction course?

Page 3: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

3

Review

• Recursive vs. Iterative• Guest lecture: Prof. Benny Chor • Arrays

• Arrays in memory• Initialization and usage• foreach, filter • Arrays as functions arguments• Multi-dimensional arrays• References to array

• Sorting, searching• Binary search

Page 4: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

4

Today• Home work review• Sorting, searching and time-complexity analysis

• Binary search• Bubble sort, Merge sort

• Scala memory model• Object-oriented programming (OOP)

• Classes and Objects• Functional Objects (Rational Numbers example)

• Home work

Page 5: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

Decimal Binary

• We want to print the binary representation of a decimal number

• Examples:• 0 -> 0• 8 -> 1000• 12 -> 1100• 31 -> 11111

Page 6: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

Decimal Binary Recursively

• The conversion of a number d from decimal to binary:• If d is 0 or 1 – write d to the left and stop, else:• If d is even, write ‘0’ to the left• If d is odd, write ‘1’ to the left• Repeat recursively with floor(d/2)

Page 7: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

Example: d = 14

d Output at the end of stage Action at the end of stage

14 0 Insert 0 to left of output; divide d by 2

7 10 Insert 1 to left of output; divide d by 2 and round down

3 110 Insert 1 to left of output; divide d by 2 and round down

1 1110 Insert 1 to left of output; return.

Page 8: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

Solution Dec2Bin.scala

Page 9: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

9

Exercise 1

Write a program that gets 10 numbers from the user.It then accepts another number and checks to see ifthat number was one of the previous ones.

Example 1:Please enter 10 numbers:1 2 3 4 5 6 7 8 9 10Please enter a number to search for: 8Found it!

Example 2:Please enter 10 numbers:1 2 3 4 5 6 7 8 9 10Please enter a number to search for: 30Sorry, it’s not there

Page 10: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

10

Solution FindNumber.scala

Page 11: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

11

Exercise 2

• Implement a function that accepts two integer arrays and returns true if they are equal, false otherwise. The arrays are of the same size

• Write a program that accepts two arrays of integers from the user and checks for equality

Page 12: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

12

SolutionCompareArrays.scala

Page 13: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

13

Solution (main)CompareArrays.scala

Page 14: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

14

Today• Home work review• Sorting, searching and time-complexity analysis

• Binary search• Bubble sort, Merge sort

• Scala memory model• Object-oriented programming (OOP)

• Classes and Objects• Functional Objects (Rational Numbers example)

• Home work

Page 15: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

15

Sort

• We would like to sort the elements in an array in an ascending order

7 2 8 5 4 2 4 5 7 8sort

Page 16: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

16

What is Sorting Good For?

• Finding a number in an array• Consider a large array (of length n) and

multiple queries on whether a given number exists in the array (and what is its position in it)

• Naive solution: given a number, traverse the array and search for it• Not efficient ~ n/2 steps for each search operation

• Can we do better?• Sort the array as a preliminary step. Now search can

be performed much faster!

Page 17: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

17

Binary Search• Input:

• A sorted array of integers A

• An integer query q

• Output:• -1 if q is not a member of A

• The index of q in A otherwise

• Algorithm:• Check the middle element of A

• If it is equal to q, return its index

• If it is >= q, search for q in A[0,…,middle-1]

• If it is < q, search for q in A[middle+1,...,end]

Page 18: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

18

Example

0 1 2 3 4 5 6 7 8 9-5 -3 0 4 8 11 22 56 57 97

index

value

http://www.youtube.com/watch?v=ZrN6J8No080

Page 19: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

19

Code – Binary Search

Page 20: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

20

Code – Usage

Page 21: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

21

So, How Fast is it?• Worst case analysis

• Size of the inspected array:

n n/2 n/4 ….. 1

• Each step is very fast (a small constant number of operations)

• There are log2(n) such steps

• So it takes ~ log2(n) steps per search

• Much faster then ~ n

Page 22: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

22

Bubble Sort

Page 23: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

23

Bubble Sort Example

7 2 8 5 4

2 7 8 5 4

2 7 8 5 4

2 7 5 8 4

2 7 5 4 8

2 7 5 4 8

2 5 7 4 8

2 5 4 7 8

2 7 5 4 8

2 5 4 7 8

2 4 5 7 8

2 5 4 7 8

2 4 5 7 8

2 4 5 7 8

(done)

Page 24: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

24

Another Example

http://www.youtube.com/watch?v=myKlT30nl5Y

Page 25: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

25

Bubble Sort

Page 26: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

26

Orders of Growth

• Suppose n is a parameter that measures the size of a problem (the size of its input)

• R(n) measures the amount of resources needed to compute a solution procedure of size n

• Two common resources are space, measured by the number of deferred operations, and time, measured by the number of primitive steps

The worst-case over all inputs of size n!

Page 27: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

27

Orders of Growth

• Want to estimate the “order of growth” of R(n):

R1(n)=100n2

R2(n)=2n2+10n+2

R3(n) = n2Are all the same in the sense that if we multiply the input by a factor of 2, the resource consumption increases by a factor of 4

Order of growth is proportional to n2

Page 28: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

28

Summary

• Trying to capture the nature of processes

• Quantify various properties of processes:• Number of steps a process takes (Time Complexity)• Amount of Space a process uses (Space Complexity)

• You will encounter these issues in many courses throughout your studies

• We shall focus on the (intuitive) time complexity analysis of various sorting algorithms

Page 29: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

29

Examples

• Find a maximum in a general array

• Find the maximum in a sorted array

• Find the 5th largest element in a sorted array

• Answer n Fibonacci quarries, each limited by MAX

• Find an element in a general array

• Find an element in a sorted array

Page 30: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

30

Bubble Sort Time ComplexityArray of size n

n iterationsi iterations

constant

(n-1 + n-2 + n-3 + …. + 1) * const ~ ½ * n2

Page 31: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

31

The Idea Behind Marge Sort• A small list will take fewer steps to sort than

a large list

• Fewer steps are required to construct a sorted list from two sorted lists than two unsorted lists

Page 32: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

32

Marge Sort Algorithm• If the array is of length 0 or 1, then it is

already sorted. Otherwise:

• Divide the unsorted array into two sub-arrays of about half the size

• Sort each sub-array recursively by re-applying merge sort

• Merge the two sub-arrays back into one sorted array

Page 33: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

33

Merge Sort Example

http://en.wikipedia.org/wiki/Merge_sort

Page 34: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

34

Marge Sort Time Complexity• If the array is of length 0 or 1, then it is already sorted. Otherwise:

• Divide the unsorted array into two sub-arrays of about half the size

• Sort each sub-array recursively by re-applying merge sort

• Merge the two sub-arrays back into one sorted array

n + 2 * (n/2) + 22 * n/22 + 23 * n/23 + … + 2log(n) * n/2log(n) =

n + n + … + n = n * log(n)

log(n)

Page 35: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

35

Today• Home work review• Sorting, searching and time-complexity analysis

• Binary search• Bubble sort, Merge sort

• Scala memory model• Object-oriented programming (OOP)

• Classes and Objects• Functional Objects (Rational Numbers example)

• Home work

Page 36: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

36

Passing Arguments to Functions

• When a function is called, arguments’ values are attached to function’s formal parameters by order, and an assignment occurs before execution

• Values are copied to formal parameters

• “Call by value”

• Function’s parameters are defined as vals

Page 37: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

37

Passing Arguments to Functions

• A reference is also passed by value• Example: arrays• Objects (?)• This explains why we can change an array’s

content within a function

4 5 6 7 8 9a

Page 38: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

38

Local Names

• Arguments names do not matter!

• Local variable name hides in-scope variables with the same name

Different x!

Page 39: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

39

Everything is an Object (in Scala)

• In Java: primitives vs. objects• In Scala everything is an Object• But: special treatment for primitives• Why do we care?

val x = 5

var y = x

y = 6

val ar1 = Array(1,2,3)

val ar2 = ar1

ar2(0) = 4?

Page 40: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

40

?

val x = 5

var y = x

y = 6

val ar1 = Array(1,2,3)

val ar2 = ar1

ar2(0) = 4

Page 41: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

41

Memory Image 1

val x = 5

var y = x

y = 6

x 5

y 5y

6

Page 42: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

42

Memory Image 2

val ar1 = Array(1,2,3)

val ar2 = ar1

ar2(0) = 4

ar1 1 2 3

ar2

4

Page 43: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

43

Scala Memory Model

• Based on Java…• Stack: local variables and arguments, every

function uses a certain part of the stack• Stack variables “disappear” when scope ends

• Heap: global variables and object, scope independent• Garbage Collector

• Partial description

Page 44: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

44

How to Change a Variable via Functions?

• The arguments are passed as vals thus can not be changed

• So how can a method change an outer variable?• By its return value• By accessing heap-based memory (e.g., arrays)

Page 45: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

45

Today• Home work review• Sorting, searching and time-complexity analysis

• Binary search• Bubble sort, Merge sort

• Scala memory model• Object-oriented programming (OOP)

• Classes and Objects• Functional Objects (Rational Numbers example)

• Home work

Page 47: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

47

Singletone Objects

• All programs written so far in this course are Signletone objects

• File start with the reserved word object

• Contain functions that can be used elsewhere

• Application: singeltone object with a main function

• (Actually singeltone objects are more then that)• (Java programmers: think of it as a holder of static

methods)

Page 49: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

49

Object-Oriented Programming (OOP)

• Represent problem-domain entities using a computer language

• When building a software in a specific domain, describe the different components of the domain as types and variables

• Thus we can take another step up in abstraction

Page 50: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

50

Class as a BlueprintA class is a blueprint of objects

Page 52: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

52

Classes as Data Types

• Classes define types that are a composition of other types and have unique functionality

• An instance of a class is named an object• Every instance may contain:

• Data members / fields• Methods• Constructors

• Instances are accessed only through reference

Page 53: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

53

Examples

• String• Members: all private• Methods: length, replace, startsWith, substring,…• Constructors: String(), String(String),…• http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html

• Array• Members: all private• Methods: length, filter, update,…• Constructors: initiate with 1-9 dimensions• http://www.scala-lang.org/docu/files/api/scala/Array.html

Page 54: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

54

Car Example

• Members: 4 wheels, steering wheel, horn, color,…

• Every car instance has its own

• Methods: drive, turn left, honk, repaint,…

• Constructors: Car(String color), Car(Array[Wheels], Engine,…), …

Page 55: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

55

Today• Home work review• Sorting, searching and time-complexity analysis

• Binary search• Bubble sort, Merge sort

• Scala memory model• Object-oriented programming (OOP)

• Classes and Objects• Functional Objects (Rational Numbers example)

• Home work

Page 56: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

56

Rational Numbers

• A rational number is a number that can be expressed as a ration n/d (n,d integers, d not 0)

• Examples: 1/2, 2/3, 112/239, 2/1

• Not an approximation

Page 57: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

57

Specification• Add, subtract, multiply, divide

• println should work smoothly• Immutable (result of an operation is a new rational number)

• It should feel like native language support

Page 58: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

58

Constructing a Rational

• How client programmer will create a new Rational object?

Class parameters

Page 59: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

59

Constructing a Rational• The Scala compiler will compile any code placed in

the class body, which isn’t part of a field or a method definition, into the primary constructor

?

Page 60: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

60

Reimplementing toString

• toString method• A more useful implementation of toString would

print out the values of the Rational’s numerator and denominator

• override the default implementation

Page 61: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

61

Usage

• Now we can remove the debug println…

Page 62: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

62

Checking Preconditions

• Ensure the data is valid when the object is constructed

• Use require

Page 63: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

63

Define “add” Method

• Immutable• Define add:

Page 64: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

64

Add Fields• n, d are in scope in the add method• Access then only on the object on which add

was invoked

Page 65: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

65

Test Add, Access Fields

Page 66: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

66

Self Reference (this)

• Define method lessThan:

• Define method max:

Page 67: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

67

Auxiliary Constructors• Constructors other then the primary

• Example: a rational number with a denominator of 1 (e.g., 5/1 5)

• We would like to do: new Rational(5)• Auxiliary constructor first action: invoke

another constructor of the same class

• The primary constructor is thus the single point of entry of a class

Page 68: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

68

Revised Rational

Page 69: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

69

Private Fields and Methods

• 66/42 = 11/7• To normalize divide the numerator and

denominator by their greatest common divisor (gcd)• gcd(66,42) = 6 (66/6)/(42/6) = 11/7• No need for Rational clients to be aware of this• Encapsulation

Page 70: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

70

Off Topic: Calculate gcd• gcd(a,b) = g

• a = n * g• b = m * g• gcd(n,m)=1(otherwise g is not the gcd)• a = t * b + r = t * m * g + r g is a divisor of r

• gcd(a,b) = gcd(b,a%b)• The Euclidean algorithm: repeat iteratively:

if (b == 0) return aelse repeat using a b, b a%b

• http://en.wikipedia.org/wiki/Euclidean_algorithm

Page 71: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

71

Correctness• Example:

gcd(40,24) gcd(24,16) gcd(16,8) gcd(8,0) 8

• Prove: g = gcd(a,b) = gcd(b,a%b)= g1• g1 is a divisor of a ( g1 ≤ g)• There is no larger divisor of a ( g1 ≥ g)

• ≤ : a = t * b + r a = t * h * g1 + v * g1 g1 is a divisor of a

• ≥ : assume g > g1 a = t * b + r g is a divisor of b and r contradiction

Page 72: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

72

Implementation

Page 73: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

73

Revised Rational

Page 74: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

74

Defining Operators• Why not use natural arithmetic operators?

• Replace add by the usual mathematical symbol

• Operator precedence will be kept

• All operations are method calls

Page 75: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

75

Revised Rational

Page 76: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

76

Usage

Page 77: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

77

Method Overloading• Now we can add and multiply rational numbers!• What about mixed arithmetic?

• r * 2 won’t work • r * new Rational(2) is not nice

• Add new methods for mixed addition and multiplication

• Method overloading• The compiler picks the correct overloaded

method

Page 78: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

78

Usage

• The * method invoked is determined in each case by the type of the right operand

Page 79: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

79

Revised Rational

Page 80: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

80

Implicit Conversions

• 2 * r 2.*(r) method call on 2 (Int) Int class contains no multiplication method that takes a Rational argument

• Create an implicit conversion that automatically converts integers to rational numbers when needed

Page 81: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

81

Companion Object

Page 82: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

82

Revised Rational

• Define implicit conversion in Rational.scala, after defining object Rational

Page 83: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

83

In Eclipse

• In Rational.scala:• Companion object

(object Rational)• Rational class (class

Rational)

• Place the main method in another file

Page 84: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

84

Summary• Customize classes so that they are natural

to use• fields, methods, primary constructor• Method overriding• Self reference (this)• Define several constructors• Encapsulation• Define operators as method• Method overloading• Implicit conversions, companion object

Page 85: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

85

Today• Home work review• Sorting, searching and time-complexity analysis

• Binary search• Bubble sort, Merge sort

• Scala memory model• Object-oriented programming (OOP)

• Classes and Objects• Functional Objects (Rational Numbers example)

• Home work

Page 86: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

86

Exercise 1

• Read, understand and implement selection/insertion sort algorithm• http://en.wikipedia.org/wiki/Selection_sort• http://en.wikipedia.org/wiki/Insertion_sort

Page 87: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

87

Exercise 2• Implement class Complex so it is natural to use

complex numbers• Examples:

Page 88: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

88

Want More Exercises?

Page 89: 1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.

89

Exercise (tough!)

• Read, understand and implement quick sort algorithm• http://en.wikipedia.org/wiki/Quicksort