Title: Structured Problem Solving
1Structured Problem Solving
- Data Structures 2
- Operations on arrays
2Quick question
- Essentials1 is Beer
- Essentials4 does not exist
3Another question
4Final question
- Array 1..151..7 of integer
5Programming with arrays
- We use arrays in programs and carry out
operations on them - Assignment
- Retrieval
- Initialisation
- Searching
- Sorting
6Operations on arrays assignment
7Operations on arrays retrieval
- What would be the output of
- System.out.println(Scores3Scores10)
62
8Example of retrieval
- Write a fragment of Java that would display the
third name in the array. - System.out.println(PlayOffTeams3)
9Example of retrieval
- Write a fragment of Java that would display all
the names in the array.
10Example of retrieval
- for( i 1 ilt4 i)
-
- System.out.println(PlayOffTeamsi)
-
11Searching and sorting arrays
12Java Array
- int X new int99999
- int i
- for (i0 ilt 99999 i)
-
- Xi i2
-
- X0 gets 0
- X1 gets 2
- X2 gets 4 etc
13Searching 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
14Different kinds of search available
- Linear Search
- Binary search
15Linear 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
17The 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
21Sorting
- We need to sort to
- be able to use the fast binary search
- produce reports in required order
22Sorting
- 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
23Exchange Sort
24See 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
25See 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
26See 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
27See 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
30See 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.
31Java 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.
33Sorting Demos
- http//www.cs.ubc.ca/spider/harrison/Java/sorting-
demo.html
- http//www.cs.hope.edu/alganim/animator/Animator.
html