CS 171: Introduction to Computer Science II Simple Sorting (cont.) + - - PowerPoint PPT Presentation

cs 171 introduction to computer science ii simple sorting
SMART_READER_LITE
LIVE PREVIEW

CS 171: Introduction to Computer Science II Simple Sorting (cont.) + - - PowerPoint PPT Presentation

CS 171: Introduction to Computer Science II Simple Sorting (cont.) + Interface Simple Sorting (cont.) + Interface Li Xiong Today Simple sorting algorithms (cont.) Bubble sort Selection sort Insertion sort Interface Interface


slide-1
SLIDE 1

CS 171: Introduction to Computer Science II Simple Sorting (cont.) + Interface Simple Sorting (cont.) + Interface

Li Xiong

slide-2
SLIDE 2

Today

Simple sorting algorithms (cont.)

Bubble sort Selection sort Insertion sort

Interface Interface

slide-3
SLIDE 3

Sorting problem

slide-4
SLIDE 4

Two useful sorting abstractions

slide-5
SLIDE 5

=

Analysis of Bubble Sort

Number of comparisons? Number of swaps?

  • =

Number of swaps? best case: worst cast: average:

=

slide-6
SLIDE 6

Selection Sort

1. Keep track of the index of the smallest number in each round. 2. Swap the smallest number towards the beginning of the array. beginning of the array. 3. Repeat the above two steps.

slide-7
SLIDE 7

Selection Sort Implementation

slide-8
SLIDE 8
  • Selection Sort

Number of comparisons?

  • Number of swaps?
slide-9
SLIDE 9

Card Sorting Exercise

How do you sort a hand of poker cards?

slide-10
SLIDE 10

Insertion Sort

Idea

Assume the left portion of the array is partially sorted (however, unlike selection sort, the elements are not necessarily in their final positions) positions) For each remaining element on the right portion, insert it to the left portion (similar to insertion in an ordered array). Repeat until done.

slide-11
SLIDE 11
slide-12
SLIDE 12
slide-13
SLIDE 13

Insertion Sort Implementation

slide-14
SLIDE 14

Insertion Sort

Online demo

http://www.sorting-algorithms.com/insertion-sort

Romanian dance demo

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

slide-15
SLIDE 15

Insertion Sort

Number of comparisons? Number of s?

slide-16
SLIDE 16

Insertion sort

Best case

N-1 comparisons 0 exchanges

Worst case

~N2/2 comparisons ~N2/2 exchanges

Average case

~N2/4 comparisons ~N2/4 exchanges

slide-17
SLIDE 17

Summary

are comparison based. Both have an average comparison cost of Later we will learn several faster sorting algorithms, with a typical cost of

slide-18
SLIDE 18

Hw2

Implement Bubble Sort Compare the runtime for bubble sort, selection sort, and insertion sort

slide-19
SLIDE 19

Java’s Sorting Methods

Primitive Type Arrays:

  • ……

……

slide-20
SLIDE 20

Java’s Sorting Methods

Object Type Arrays: ……

  • ……

Comparator is used to define how to compare two

  • bjects (i.e. which is bigger / smaller).

int compare compare compare compare(T o1, T o2) boolean equals equals equals equals(Object obj)

slide-21
SLIDE 21

Today

Simple sorting algorithms (cont.)

Bubble sort Selection sort Insertion sort

Interface Interface

slide-22
SLIDE 22

Insertion Sort Implementation

slide-23
SLIDE 23
slide-24
SLIDE 24
slide-25
SLIDE 25

Callback Mechanism: Interface

slide-26
SLIDE 26

Abstract Classes and Interfaces

Abstract class and abstract methods Interfaces

slide-27
SLIDE 27

Superclasses and Subclasses

slide-28
SLIDE 28

Abstract Classes and Abstract Methods

  • ! "

#$%& ' ' '( '( '" ! " ' )# *%

  • +
  • '
  • !!

' '!! ',!! ',!!! '"%!

,

  • !

! ', ',-!.! '/! '/-! '0! '0! +% 1+2% , &% 3%415%!

slide-29
SLIDE 29

abstract method in abstract class

public abstract void method(); If a class contains abstract methods, it must be declared abstract If a subclass of an abstract superclass does not

  • If a subclass of an abstract superclass does not

implement all the abstract methods, the subclass must be declared abstract

slide-30
SLIDE 30

Instance cannot be created from abstract class

An abstract class cannot be instantiated using the new

  • perator

You can still define its constructors, which are invoked in the constructors of its subclasses For instance, the constructors of GeometricObject are

  • For instance, the constructors of GeometricObject are

invoked in the Circle class and the Rectangle class.

slide-31
SLIDE 31

superclass of abstract class may be concrete

A subclass can be abstract even if its superclass is concrete For example, the Object class is concrete, but its subclasses, such as GeometricObject, may be abstract

slide-32
SLIDE 32

abstract class as type

You cannot create an instance from an abstract class using the new operator, but an abstract class can be used as a data type

! "#

  • $ ! "#

! "#

slide-33
SLIDE 33

Review questions

Which of the following declares an abstract method in an abstract Java class?

  • A. public abstract method();
  • A. public abstract method();
  • B. public abstract void method();
  • C. public void abstract Method();
  • D. public void method() {}
  • E. public abstract void method() {}
slide-34
SLIDE 34

Review questions

Which of the following statements regarding abstract methods are true?

  • A. An abstract class can have instances created using

the constructor of the abstract class. the constructor of the abstract class.

  • B. An abstract class can be extended.
  • C. A subclass of a non-abstract superclass can be

abstract.

  • D. An abstract class can be used as a data type.
slide-35
SLIDE 35

Review questions

Suppose A is an abstract class, B is a concrete subclass of A, and both A and B have a default

  • constructor. Which of the following is correct?

A a = new A(); A a = new A(); A a = new B(); B b = new A(); B b = new B();

slide-36
SLIDE 36

Interfaces

What is an interface? Why is an interface useful? How do you define an interface? How do you use an interface? How do you use an interface?

slide-37
SLIDE 37

What is an interface? Why is an interface useful?

An interface is a classlike construct that contains

  • nly constants and abstract methods

In many ways, an interface is similar to an abstract class, but the intent of an interface is to specify behavior for objects specify behavior for objects

Specify objects that are comparable, edible, cloneable using appropriate interfaces such as % and

A class that implements an interface need to implement all the abstract methods

define Orange and Chicken classes that implement % interface

slide-38
SLIDE 38

Interface is a Special Class

Like an abstract class, you cannot create an instance from an interface using the new

  • perator

You can create an instance from a class that implements an interface implements an interface You can use an interface as a data type for a variable, as the result of casting, and so on.

slide-39
SLIDE 39

Define an Interface

& ' ( ) $& *

& % (

  • & % (

+,, - )! ,+ & .$ )!/% *

slide-40
SLIDE 40

Omitting Modifiers in Interfaces

All data fields are public final static (constants) in an interface All methods are public abstract in an interface

& /" (

0 "

67!

& /" ( 0 "

  • 0 "

1 *

67!

0 " 1 *

slide-41
SLIDE 41

The Comparable Interface

++ /) ++ 1$ 2$

2$ 1$

  • & (

& / *

slide-42
SLIDE 42

String and Date Classes

Many classes (e.g., String and Date) in the Java library implement Comparable to define a natural order for the objects

  • & .$

( ++ & - ( ++

  • ++

* ++ *

slide-43
SLIDE 43

Declaring Classes to Implement Comparable

  • ,
  • 89
  • %*,
  • 3$ $" ! 3$4 5

3$ $6 ! 3$7 8 .&9$" $6 %*,

slide-44
SLIDE 44

Callback Mechanism: Interface