View
218
Download
0
Category
Preview:
Citation preview
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Data Structures 2
Stewart Blakeway
blakews@hope.ac.uk
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Aims of the Presentation
• To demonstrate Arrays in Java– Defining the Array object– Determining the Range– Assigning a value to an element– Sorting the Array– Searching the Array
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Quick question – What is Shopping[1] and Shopping[4] ?
• Shopping[1] is Crisps• Shopping[4] does not exist
3
0 Wine1 Crisps2 Nuts3 Bread
Shopping
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE • int[] cars = new int[7];
4
Another question
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PEint[][] iceCreamSales = new int[7][15];
5
Final question
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
What we have done before in data structures
• A record is a collection of different data types
• An array is a fixed size collection of same data types– Single-dimension– Multi-dimension
• A sequence is a variable size collection of same data types– Stack– Queue
6
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Operations on arrays: assignment
• Scores[4] = 29;
7
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Operations on arrays: retrieval
• What would be the output of:
System.out.println(Scores[3]+Scores[10]);
8
62
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Example of retrieval
• Write a fragment of Java that would display the third name in the array.
System.out.println(PlayOffTeams[3]);
9
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Example of retrieval
• Write a fragment of Java that would display all the names in the array.
10
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Example of retrieval
int i;for (i = 1; i<=4; i++) { System.out.println(PlayOffTeams[i]); }
11
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Initialisation
• When arrays are created they should be initialised, otherwise they will possibly contain random numbers or NULL values
12
The array Scores would want to start out life like this – each element contains 0
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Initialisation
• We can initialise Scores in Java like this:
int j;for (j=1; j<=10; j++) { Scores[j] = 0; }
13
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Searching an array
• Example: – How does a “hole in the wall” find your account
from your credit card details ?
• Need to search through the array of bank records
15
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Different kinds of search available
• Linear Search• Binary search
16
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Linear Search• Start at the beginning and examine each in
turn until a match found or end of records reached
– Fastest find in - • 1 comparison
– Slowest find in -• 1,000 comparisons
– Average find in - • 500 comparisons
17
for simplicity, we will assume that each search takes 1ms
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
• Search for a national insurance number
• Is a linear search appropriate ?
• No !
• A better solution in this case is a binary search
18
Linear Search
Approximately 59 million people in the UK = Average Search Time of: 48.77 weeksMaximum Search Time of: 1y 43 weeks
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
• Think of a number between 1 and 1000
19
Binary Search
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
• Think of a number between 1 and 1000• I can get that number in 10 yes/no questions
– is it less than 500? No– is it less than 750? No– is it less than 825? Yes– ... keep going
20
Binary Search
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
• Search for Evans in the array of names which have been sorted into alphabetical order
21
Binary Search
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
• 1 Ball• 2 Davies• 3 Evans• 4 Galt• 5 Hurst• 6 Martin• 7 Mason• 8 Moore• 9 Perkins• 10 Stephens
22
See pages 37, 38 in booklet
Searching for Evans
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
• 1 Ball• 2 Davies• 3 Evans• 4 Galt• 5 Hurst• 6 Martin• 7 Mason• 8 Moore• 9 Perkins• 10 Stephens
23
See pages 37, 38 in booklet
Searching for Evans
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
• 1 Ball• 2 Davies• 3 Evans• 4 Galt• 5 Hurst• 6 Martin• 7 Mason• 8 Moore• 9 Perkins• 10 Stephens
24
See pages 37, 38 in booklet
Searching for Evans
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
• 1 Ball• 2 Davies• 3 Evans• 4 Galt• 5 Hurst• 6 Martin• 7 Mason• 8 Moore• 9 Perkins• 10 Stephens
25
See pages 37, 38 in booklet
Searching for Evans
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
• 1 Ball• 2 Davies• 3 Evans• 4 Galt• 5 Hurst• 6 Martin• 7 Mason• 8 Moore• 9 Perkins• 10 Stephens
26
See pages 37, 38 in booklet
Searching for Evans
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
• 1 Ball• 2 Davies• 3 Evans• 4 Galt• 5 Hurst• 6 Martin• 7 Mason• 8 Moore• 9 Perkins• 10 Stephens
27
See pages 37, 38 in booklet
Searching for Evans
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
• 1 Ball• 2 Davies• 3 Evans• 4 Galt• 5 Hurst• 6 Martin• 7 Mason• 8 Moore• 9 Perkins• 10 Stephens
28
Found Evans !
Searching for Evans
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Sorting
• We need to sort to– be able to use the fast binary search which depends
on the array being in ascending or descending order– produce reports in required order
29
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
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
30
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
• 1 Davies• 2 Martin• 3 Perkins• 4 Evans• 5 Mason• 6 Ball• 7 Stephens• 8 Moore• 9 Hurst• 10 Galt
32
See pages 42, 43 in booklet
No swap
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
• 1 Davies• 2 Martin• 3 Perkins• 4 Evans• 5 Mason• 6 Ball• 7 Stephens• 8 Moore• 9 Hurst• 10 Galt
33
See pages 42, 43 in booklet
No swap
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
• 1 Davies• 2 Martin
• 3 Perkins• 4 Evans• 5 Mason• 6 Ball• 7 Stephens• 8 Moore• 9 Hurst• 10 Galt
34
See pages 42, 43 in booklet
Swap
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
• 1 Davies• 2 Martin• 3 Evans
• 4 Perkins• 5 Mason• 6 Ball• 7 Stephens• 8 Moore• 9 Hurst• 10 Galt
35
See pages 42, 43 in booklet
Swap
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
• 1 Davies• 2 Martin• 3 Evans• 4 Mason
• 5 Perkins• 6 Ball• 7 Stephens• 8 Moore• 9 Hurst• 10 Galt
36
See pages 42, 43 in booklet
Swap
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
• 1 Davies• 2 Martin• 3 Evans• 4 Mason• 5 Ball• 6 Perkins• 7 Stephens• 8 Moore• 9 Hurst• 10 Galt
37
See pages 42, 43 in booklet
No Swap
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
• 1 Davies• 2 Martin• 3 Evans• 4 Mason• 5 Ball• 6 Perkins
• 7 Stephens• 8 Moore• 9 Hurst• 10 Galt
38
See pages 42, 43 in booklet
Swap
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
• 1 Davies• 2 Martin• 3 Evans• 4 Mason• 5 Ball• 6 Perkins• 7 Moore
• 8 Stephens• 9 Hurst• 10 Galt
39
See pages 42, 43 in booklet
Swap
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
• 1 Davies• 2 Martin• 3 Evans• 4 Mason• 5 Ball• 6 Perkins• 7 Moore• 8 Hurst
• 9 Stephens• 10 Galt
40
See pages 42, 43 in booklet
Swap
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
• 1 Davies• 2 Martin• 3 Evans• 4 Mason• 5 Ball• 6 Perkins• 7 Moore• 8 Hurst• 9 Galt
• 10 Stephens
41
See pages 42, 43 in booklet
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.
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Java Exchange Sort
42
Consider an array of 10,000 surnames, called names, indexed from 0 to 9,999.
for (i=1; i<=9999; i++)
{
for (j=0; j< 9999; j++)
{
if (names[j] > names[j+1])
{
temp = names[j];
names[j] = names[j+1];
names[j+1] = temp;
}
}
}
Recommended