Structured Problem Solving - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

Structured Problem Solving

Description:

1. www.hope.ac.uk Deanery of Business & Computer Sciences. Structured ... Search for a name in a telephone directory. Is a linear search appropriate ? No ! ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 34
Provided by: BrianFa8
Category:

less

Transcript and Presenter's Notes

Title: Structured Problem Solving


1
Structured Problem Solving
  • Data Structures 2
  • Operations on arrays

2
Quick question
  • Essentials1 is Beer
  • Essentials4 does not exist

3
Another question
  • Array1..5 of integer

4
Final question
  • Array 1..151..7 of integer

5
Programming with arrays
  • We use arrays in programs and carry out
    operations on them
  • Assignment
  • Retrieval
  • Initialisation
  • Searching
  • Sorting

6
Operations on arrays assignment
  • Scores4 29

7
Operations on arrays retrieval
  • What would be the output of
  • System.out.println(Scores3Scores10)

62
8
Example of retrieval
  • Write a fragment of Java that would display the
    third name in the array.
  • System.out.println(PlayOffTeams3)

9
Example of retrieval
  • Write a fragment of Java that would display all
    the names in the array.

10
Example of retrieval
  • for( i 1 ilt4 i)
  • System.out.println(PlayOffTeamsi)

11
Searching and sorting arrays
12
Java Array
  • int X new int99999
  • int i
  • for (i0 ilt 99999 i)
  • Xi i2
  • X0 gets 0
  • X1 gets 2
  • X2 gets 4 etc

13
Searching an array
  • Example
  • How does hole in the wall find your account from
    your credit card details ?
  • Need to search through the array of bank records

14
Different kinds of search available
  • Linear Search
  • Binary search

15
Linear Search
  • Searching for a bank record
  • 1,000 bank records
  • Fastest find in -
  • 1 comparison
  • Slowest find in -
  • 1,000 comparisons
  • Average find in
  • 500 comparisons

16
  • Search for a name in a telephone directory
  • Is a linear search appropriate ?
  • No !
  • A better solution in this case is a binary search

17
The Binary Search
  • Search for Evans in the array of names which have
    been sorted into alphabetical order

Figure 12
18
  • 1 Ball
  • 2 Davies
  • 3 Evans
  • 4 Galt
  • 5 Hurst
  • 6 Martin
  • 7 Mason
  • 8 Moore
  • 9 Perkins
  • 10 Stephens

See pages 37, 38 in booklet
19
  • 1 Ball
  • 2 Davies
  • 3 Evans
  • 4 Galt
  • 5 Hurst
  • 6 Martin
  • 7 Mason
  • 8 Moore
  • 9 Perkins
  • 10 Stephens

See pages 37, 38 in booklet
20
  • 1 Ball
  • 2 Davies
  • 3 Evans
  • 4 Galt
  • 5 Hurst
  • 6 Martin
  • 7 Mason
  • 8 Moore
  • 9 Perkins
  • 10 Stephens

See pages 37, 38 in booklet
21
Sorting
  • We need to sort to
  • be able to use the fast binary search
  • produce reports in required order

22
Sorting
  • Many different ways of carrying out a sort on an
    array.
  • We shall examine EXCHANGE SORT (Also known as the
    BUBBLE SORT)
  • We shall look at others

23
Exchange Sort
  • Simple
  • Slow

24
See pages 41, 42 in booklet
  • 1 Davies
  • 2 Martin
  • 3 Perkins
  • 4 Evans
  • 5 Mason
  • 6 Ball
  • 7 Stephens
  • 8 Moore
  • 9 Hurst
  • 10 Galt

Swap
25
See pages 41, 42 in booklet
  • 1 Davies
  • 2 Martin
  • 3 Evans
  • 4 Perkins
  • 5 Mason
  • 6 Ball
  • 7 Stephens
  • 8 Moore
  • 9 Hurst
  • 10 Galt

Swap
26
See pages 41, 42 in booklet
  • 1 Davies
  • 2 Martin
  • 3 Evans
  • 4 Mason
  • 5 Perkins
  • 6 Ball
  • 7 Stephens
  • 8 Moore
  • 9 Hurst
  • 10 Galt

Swap
27
See pages 41, 42 in booklet
  • 1 Davies
  • 2 Martin
  • 3 Evans
  • 4 Mason
  • 5 Ball
  • 6 Perkins
  • 7 Stephens
  • 8 Moore
  • 9 Hurst
  • 10 Galt

Swap
28
  • 1 Davies
  • 2 Martin
  • 3 Evans
  • 4 Mason
  • 5 Ball
  • 6 Perkins
  • 7 Moore
  • 8 Stephens
  • 9 Hurst
  • 10 Galt

See pages 41, 42 in booklet
Swap
29
  • 1 Davies
  • 2 Martin
  • 3 Evans
  • 4 Mason
  • 5 Ball
  • 6 Perkins
  • 7 Moore
  • 8 Hurst
  • 9 Stephens
  • 10 Galt

See pages 41, 42 in booklet
Swap
30
See pages 41, 42 in booklet
  • 1 Davies
  • 2 Martin
  • 3 Evans
  • 4 Mason
  • 5 Ball
  • 6 Perkins
  • 7 Moore
  • 8 Hurst
  • 9 Galt
  • 10 Stephens

At the end of this first pass, Stephens is in the
correct position alphabetically.
This is repeated for a second pass starting at
position 1 and again and again until the array is
completely sorted.
31
Java Exchange Sort
Consider an array of 10,000 surnames, called
names, indexed from 0 to 9,999. for (i1
ilt9999 i) for (j0 jlt 9999 j) if
(namesj gt namesj1) temp
namesj namesj namesj1 namesj1
temp
32
  • Please refer to page 43 of the booklet plus the
    exercise posted on the web site and on the H
    drive.

33
Sorting Demos
  • http//www.cs.ubc.ca/spider/harrison/Java/sorting-
    demo.html
  • http//www.cs.hope.edu/alganim/animator/Animator.
    html
Write a Comment
User Comments (0)
About PowerShow.com