programming in java: lecture 9 -...

Post on 07-Aug-2020

4 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

2

Searching

Finding a particular element Linear search Association List

(key,value) pairs

3

Linear Search

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

5

Binary Search

6

Sorting

Insertion sort Selection sort

7

Insertion Sort

8

Insertion Sort

9

Selection Sort

10

Sorting

Comparing is not always simple

implemented on classes

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

11

Multi-dimensional Arrays

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

12

Example

13

Example

14

Example

Team programming

top related