programming in java: lecture 9 -...

14
1 Programming in Java: lecture 9 Searching and Sorting Linear and binary search Insertion Sort, Selection Sort Multi-dimensional Arrays Two dimensional arrays Example Slides made for use with ”Introduction to Programming Using Java, Version 5.0” by David J. Eck Some figures are taken from ”Introduction to Programming Using Java, Version 5.0” by David J. Eck Lecture 9 covers Section 7.4 to 7.5

Upload: others

Post on 07-Aug-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming in Java: lecture 9 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture9/slides9.pdf · Insertion Sort, Selection Sort Multi-dimensional Arrays Two dimensional

1

Programming in Java: lecture 9

Searching and Sorting Linear and binary search Insertion Sort, Selection Sort

Multi-dimensional Arrays Two dimensional arrays

Example

Slides made for use with ”Introduction to Programming Using Java, Version 5.0” by David J. EckSome figures are taken from ”Introduction to Programming Using Java, Version 5.0” by David J. EckLecture 9 covers Section 7.4 to 7.5

Page 2: Programming in Java: lecture 9 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture9/slides9.pdf · Insertion Sort, Selection Sort Multi-dimensional Arrays Two dimensional

2

Searching

Finding a particular element Linear search Association List

(key,value) pairs

Page 3: Programming in Java: lecture 9 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture9/slides9.pdf · Insertion Sort, Selection Sort Multi-dimensional Arrays Two dimensional

3

Linear Search

Page 4: Programming in Java: lecture 9 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture9/slides9.pdf · Insertion Sort, Selection Sort Multi-dimensional Arrays Two dimensional

4

Binary Search

Why? Liniear search

1000 items, max 1000 comparisons 1000000 items, max 1000000 comparisons

Binary search 1000 items, 10 comparisons 1000000 items, 20 comparisons Data must be sorted

Page 5: Programming in Java: lecture 9 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture9/slides9.pdf · Insertion Sort, Selection Sort Multi-dimensional Arrays Two dimensional

5

Binary Search

Page 6: Programming in Java: lecture 9 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture9/slides9.pdf · Insertion Sort, Selection Sort Multi-dimensional Arrays Two dimensional

6

Sorting

Insertion sort Selection sort

Page 7: Programming in Java: lecture 9 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture9/slides9.pdf · Insertion Sort, Selection Sort Multi-dimensional Arrays Two dimensional

7

Insertion Sort

Page 8: Programming in Java: lecture 9 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture9/slides9.pdf · Insertion Sort, Selection Sort Multi-dimensional Arrays Two dimensional

8

Insertion Sort

Page 9: Programming in Java: lecture 9 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture9/slides9.pdf · Insertion Sort, Selection Sort Multi-dimensional Arrays Two dimensional

9

Selection Sort

Page 10: Programming in Java: lecture 9 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture9/slides9.pdf · Insertion Sort, Selection Sort Multi-dimensional Arrays Two dimensional

10

Sorting

Comparing is not always simple

implemented on classes

A lot already implemented in Java java.util.Arrays.binarySearch() Collections.sort()

Page 11: Programming in Java: lecture 9 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture9/slides9.pdf · Insertion Sort, Selection Sort Multi-dimensional Arrays Two dimensional

11

Multi-dimensional Arrays

int[][] A; A = new int[3][4]; int[][] A = new int[3][4];

Page 12: Programming in Java: lecture 9 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture9/slides9.pdf · Insertion Sort, Selection Sort Multi-dimensional Arrays Two dimensional

12

Example

Page 13: Programming in Java: lecture 9 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture9/slides9.pdf · Insertion Sort, Selection Sort Multi-dimensional Arrays Two dimensional

13

Example

Page 14: Programming in Java: lecture 9 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture9/slides9.pdf · Insertion Sort, Selection Sort Multi-dimensional Arrays Two dimensional

14

Example

Team programming